scanLocal.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/local/bin/php
  2. <?php
  3. # Command-line parameters
  4. global $argc;
  5. global $argv;
  6. $SUB_DIR = '';
  7. if (($argc > 1) && (strlen($argv[1]) > 0)) {
  8. $SUB_DIR = trim($argv[1]);
  9. }
  10. $DELAY_DAYS=30;
  11. if (isset($_ENV['DELAY_DAYS'])) {
  12. $DELAY_DAYS = $_ENV['DELAY_DAYS'];
  13. }
  14. $VIDEO_DIR=$_ENV['HOME'] . '/bin/video';
  15. if (isset($_ENV['VIDEO_DIR'])) {
  16. $VIDEO_DIR = $_ENV['VIDEO_DIR'];
  17. }
  18. $BASE_LOCAL='';
  19. if (isset($_ENV['BASE_LOCAL'])) {
  20. $BASE_LOCAL = $_ENV['BASE_LOCAL'];
  21. } else {
  22. $BASE_LOCAL = trim(shell_exec($VIDEO_DIR . '/mediaPath'));
  23. }
  24. # Allow usage with absolute local paths
  25. if (substr($SUB_DIR, 0, 1) == '/') {
  26. $SUB_DIR = preg_replace('/^' . preg_quote($BASE_LOCAL, '/') . '\//', '', $SUB_DIR);
  27. }
  28. # Usage checks
  29. if (strlen($SUB_DIR) < 1 || $DELAY_DAYS < 1) {
  30. die('Usage: ' . $argv[0] . " sub_directory\n");
  31. }
  32. # Sanity checks
  33. $LOCAL = $BASE_LOCAL . '/' . $SUB_DIR;
  34. if (!file_exists($LOCAL) || !is_dir($LOCAL)) {
  35. die('Invalid local directory: ' . $LOCAL . "\n");
  36. }
  37. # Open the DB connection
  38. try {
  39. $dbh = new PDO('pgsql:dbname=backup', $_ENV{'USER'});
  40. } catch (PDOException $e) {
  41. die('DB error: ' . $e->getMessage() . "\n");
  42. }
  43. # Grab the file list -- limit files by mtime, but include all directories
  44. $FIND=tempnam(sys_get_temp_dir(), 'scanLocal-find');
  45. exec('cd ' . escapeshellarg($BASE_LOCAL) . ' && find ' . escapeshellarg($SUB_DIR) . ' -type f -mtime +' . escapeshellarg($DELAY_DAYS) . ' > ' . escapeshellarg($FIND));
  46. exec('cd ' . escapeshellarg($BASE_LOCAL) . ' && find ' . escapeshellarg($SUB_DIR) . ' -type d >> ' . escapeshellarg($FIND));
  47. # Sort
  48. $SORT=tempnam(sys_get_temp_dir(), 'scanLocal-sort');
  49. exec('cat ' . escapeshellarg($FIND) . ' | sort > ' . escapeshellarg($SORT));
  50. unlink($FIND);
  51. unset($FIND);
  52. # Filter junk
  53. exec('grep -v "\/\._" ' . escapeshellarg($SORT) .
  54. ' | grep -v "\/\.DS_Store\$" | grep -v "\/\.git\/"',
  55. $FILES);
  56. unlink($SORT);
  57. unset($SORT);
  58. # Prepare statements
  59. $select = $dbh->prepare('SELECT base, path, type, mtime, hash, hash_time FROM local WHERE base = :base AND path = :path');
  60. $insert = $dbh->prepare('INSERT INTO local (base, path, type, mtime) VALUES (:base, :path, :type, now())');
  61. $mtime = $dbh->prepare('SELECT EXTRACT(EPOCH FROM mtime) AS mtime FROM local WHERE base = :base AND path = :path');
  62. $set_mtime = $dbh->prepare('UPDATE local SET mtime = now() WHERE base = :base AND path = :path');
  63. $hash_time_check = $dbh->prepare('SELECT hash, EXTRACT(EPOCH FROM hash_time) AS hash_time FROM local WHERE base = :base AND path = :path AND (hash_time IS NULL OR EXTRACT(EPOCH FROM hash_time) < :mtime)');
  64. $set_hash = $dbh->prepare('UPDATE local SET hash = :hash, hash_time = now() WHERE base = :base AND path = :path');
  65. # Loop through all the files
  66. foreach ($FILES as $FILE) {
  67. # Construct the absolute path
  68. $PATH = $BASE_LOCAL . '/' . $FILE;
  69. # Does the path exist
  70. $EXISTS = false;
  71. $select->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE));
  72. $result = $select->fetch();
  73. if (!$result) {
  74. $PARTS = pathinfo($PATH);
  75. $EXT = strtolower($PARTS['extension']);
  76. $NAME = strtolower($PARTS['filename']);
  77. unset($PARTS);
  78. $TYPE = 'other';
  79. if (is_dir($PATH)) {
  80. $TYPE = 'folder';
  81. if ($NAME == '.git') {
  82. $TYPE = 'ignored';
  83. }
  84. } else {
  85. if ($EXT == 'm4v' || $EXT == 'mkv' || $EXT == 'mp4' || $EXT == 'mov' ||
  86. $EXT == 'vob' || $EXT == 'iso' || $EXT == 'avi') {
  87. $TYPE = 'video';
  88. } else if ($EXT == 'mp3' || $EXT == 'aac' || $EXT == 'm4a' || $EXT == 'm4b' ||
  89. $EXT == 'm4p' || $EXT == 'wav') {
  90. $TYPE = 'audio';
  91. } else if ($EXT == 'epub' || $EXT == 'pdf') {
  92. $TYPE = 'book';
  93. } else if ($EXT == 'jpg' || $EXT == 'png') {
  94. $TYPE = 'image';
  95. } else if ($EXT == 'gz' || $EXT == 'zip' || $EXT == 'xz') {
  96. $TYPE = 'archive';
  97. } else if ($EXT == 'itc' || $EXT == 'itl' || $EXT == 'strings' || $EXT == 'itdb' ||
  98. $EXT == 'plist' || $EXT == 'ipa' || $EXT == 'ini') {
  99. $TYPE = 'database';
  100. } else if ($EXT == 'clip' || $EXT == 'riff' || $EXT == 'nfo') {
  101. $TYPE = 'metadata';
  102. } else if ($EXT == 'webloc' || $NAME == 'skip' || $NAME == 'season_done' ||
  103. $NAME == 'more_number_formats' || $NAME == 'no_quality_checks' ||
  104. $NAME == 'filler' || $NAME == 'search_name' || $EXT == 'disabled' ||
  105. $NAME == 'must_match' || $EXT == 'fakeshow' || $EXT == 'filler' ||
  106. $NAME == 'excludes' || $NAME == 'search_by_date' || $EXT == 'twopart') {
  107. $TYPE = 'metadata';
  108. } else if ($EXT == 'fake' || $EXT == 'txt' || $EXT == 'json' ||
  109. $EXT == 'bup' || $EXT == 'ifo') {
  110. $TYPE = 'metadata';
  111. } else if ($EXT == 'lastfindrecode' || $NAME == 'placeholder' || $EXT == 'plexignore') {
  112. $TYPE = 'ignored';
  113. } else if ($EXT == 'tmp' || $EXT == 'gitignore' || preg_match('/^\.smbdelete/', $NAME)) {
  114. $TYPE = 'ignored';
  115. }
  116. }
  117. if ($TYPE == 'other') {
  118. die('Unknown file type: ' . $PATH . ': ' . $NAME . '^' . $EXT . "\n");
  119. }
  120. echo 'Adding: ' . $PATH . ': ' . $TYPE . "\n";
  121. $insert->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE, ':type' => $TYPE));
  122. if ($insert->rowCount() != 1) {
  123. print_r($insert->errorInfo());
  124. die('Unable to insert: ' . $PATH . "\n");
  125. }
  126. } else {
  127. $TYPE = $result['type'];
  128. }
  129. # Skip 'ingnored' files
  130. if ($TYPE == 'ignored') {
  131. continue;
  132. }
  133. # Update the mtime as needed
  134. $MTIME = trim(shell_exec('stat -c "%Y" ' . escapeshellarg($PATH)));
  135. $mtime->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE));
  136. $result = $mtime->fetch(PDO::FETCH_ASSOC);
  137. if (!$result && !$result['mtime'] || !$MTIME) {
  138. die('Unable to find mtime for: ' . $PATH . "\n");
  139. } else if ($result['mtime'] < $MTIME) {
  140. $set_mtime->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE));
  141. }
  142. # Update hashes as needed
  143. if ($TYPE != 'folder') {
  144. $hash_time_check->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE, ':mtime' => $MTIME));
  145. $result = $hash_time_check->fetch(PDO::FETCH_ASSOC);
  146. if ($result) {
  147. echo 'Adding hash: ' . $PATH . "\n";
  148. $HASH = trim(shell_exec('md5sum ' . escapeshellarg($PATH) . ' | cut -d " " -f 1'));
  149. if (strlen($HASH) == 32) {
  150. $set_hash->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE, ':hash' => $HASH));
  151. } else {
  152. die('Invalid hash (' . $HASH . ') for file: ' . $PATH . "\n");
  153. }
  154. } else {
  155. echo 'Hash is up-to-date: ' . $PATH . "\n";
  156. }
  157. }
  158. }
  159. unset($FILES);
  160. ?>