scanLocal.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #!/usr/local/bin/php
  2. <?php
  3. # Debug
  4. $DEBUG = false;
  5. if ($_ENV['DEBUG']) {
  6. $DEBUG = true;
  7. }
  8. # Includes
  9. require_once 'opendrive.php';
  10. # Command-line parameters
  11. global $argc;
  12. global $argv;
  13. $SUB_DIR = '';
  14. if (($argc > 1) && (strlen($argv[1]) > 0)) {
  15. $SUB_DIR = trim($argv[1]);
  16. }
  17. # Wait DELAY_DAYS before considering a file
  18. $DELAY_DAYS = 30;
  19. if (isset($_ENV['DELAY_DAYS'])) {
  20. $DELAY_DAYS = $_ENV['DELAY_DAYS'];
  21. }
  22. # Delete only if specified
  23. $DELETE = false;
  24. if ($_ENV['DELETE']) {
  25. $DELETE = true;
  26. }
  27. # Build our local base path
  28. $VIDEO_DIR=$_ENV['HOME'] . '/bin/video';
  29. if (isset($_ENV['VIDEO_DIR'])) {
  30. $VIDEO_DIR = $_ENV['VIDEO_DIR'];
  31. }
  32. $BASE_LOCAL='';
  33. if (isset($_ENV['BASE_LOCAL'])) {
  34. $BASE_LOCAL = $_ENV['BASE_LOCAL'];
  35. } else {
  36. $BASE_LOCAL = trim(shell_exec($VIDEO_DIR . '/mediaPath'));
  37. }
  38. # Allow usage with absolute local paths
  39. if (substr($SUB_DIR, 0, 1) == '/') {
  40. $SUB_DIR = preg_replace('/^' . preg_quote($BASE_LOCAL, '/') . '\//', '', $SUB_DIR);
  41. }
  42. # Usage checks
  43. if (strlen($SUB_DIR) < 1 || $DELAY_DAYS < 1) {
  44. die('Usage: ' . $argv[0] . " sub_directory\n");
  45. }
  46. # Sanity checks
  47. $LOCAL = $BASE_LOCAL . '/' . $SUB_DIR;
  48. if (!file_exists($LOCAL) || !is_dir($LOCAL)) {
  49. die('Invalid local directory: ' . $LOCAL . "\n");
  50. }
  51. # Open the DB connection
  52. $dbh = dbOpen();
  53. # Look for files in the DB that no longer exist or have changed types
  54. $delete = $dbh->prepare('DELETE FROM files WHERE base = :base AND path = :path');
  55. $missing = $dbh->prepare('SELECT base, path, type FROM files');
  56. $missing->execute();
  57. while ($row = $missing->fetch()) {
  58. $trigger = false;
  59. $file = $row['base'] . '/' . $row['path'];
  60. if ($row['type'] == 'folder') {
  61. # Ensure the path exists and is a folder
  62. if (!is_dir($file)) {
  63. echo 'Missing/changed folder: ' . $file . "\n";
  64. $trigger = true;
  65. }
  66. } else if ($row['type'] == 'ignored') {
  67. # Silently delete missing ignored files of any type
  68. if (!file_exists($file)) {
  69. if ($DEBUG) {
  70. echo 'Missing ignored file: ' . $file . "\n";
  71. }
  72. $trigger = true;
  73. }
  74. } else {
  75. # Ensure the path exists and is a regular file
  76. if (!is_file($file)) {
  77. echo 'Missing file: ' . $file . "\n";
  78. $trigger = true;
  79. }
  80. }
  81. # Delete if global DELETE is enabled
  82. if ($DELETE && $trigger) {
  83. $delete->execute(array(':base' => $row['base'], ':path' => $row['path']));
  84. }
  85. unset($trigger);
  86. unset($file);
  87. }
  88. unset($delete);
  89. # Grab the file list -- limit files by mtime, but include all directories
  90. $FIND=tempnam(sys_get_temp_dir(), 'scanLocal-find');
  91. exec('cd ' . escapeshellarg($BASE_LOCAL) . ' && find ' . escapeshellarg($SUB_DIR) .
  92. ' -type f -mtime +' . escapeshellarg($DELAY_DAYS) . ' > ' . escapeshellarg($FIND));
  93. exec('cd ' . escapeshellarg($BASE_LOCAL) . ' && find ' . escapeshellarg($SUB_DIR) .
  94. ' -type d >> ' . escapeshellarg($FIND));
  95. # Sort and injest
  96. exec('cat ' . escapeshellarg($FIND) . ' | sort ', $FILES);
  97. unlink($FIND);
  98. unset($FIND);
  99. # Loop through all the files we found
  100. $select = $dbh->prepare('SELECT base, path, type, mtime, hash, hash_time FROM files WHERE base = :base AND path = :path');
  101. $insert = $dbh->prepare('INSERT INTO files (base, path, type, mtime) VALUES (:base, :path, :type, now())');
  102. $check_mtime = $dbh->prepare('SELECT EXTRACT(EPOCH FROM mtime) AS mtime FROM files WHERE base = :base AND path = :path');
  103. $set_mtime = $dbh->prepare('UPDATE files SET mtime = now() WHERE base = :base AND path = :path');
  104. $hash_time_check = $dbh->prepare('SELECT hash, EXTRACT(EPOCH FROM hash_time) AS hash_time FROM files WHERE base = :base AND path = :path AND (hash_time IS NULL OR EXTRACT(EPOCH FROM hash_time) < :mtime)');
  105. $set_hash = $dbh->prepare('UPDATE files SET hash = :hash, hash_time = now() WHERE base = :base AND path = :path');
  106. foreach ($FILES as $file) {
  107. # Construct the absolute path
  108. $path = $BASE_LOCAL . '/' . $file;
  109. # Is the path in the DB
  110. $select->execute(array(':base' => $BASE_LOCAL, ':path' => $file));
  111. $result = $select->fetch();
  112. if (!$result) {
  113. $parts = pathinfo($path);
  114. $EXT = strtolower($parts['extension']);
  115. $NAME = strtolower($parts['filename']);
  116. unset($parts);
  117. $TYPE = 'other';
  118. if (preg_match('/\/\.git(\/.*)?$/', $path)) {
  119. $TYPE = 'ignored';
  120. } else if (preg_match('/\/\._/', $path)) {
  121. $TYPE = 'ignored';
  122. } else if ($EXT == 'lastfindrecode' || $NAME == 'placeholder' || $EXT == 'plexignore') {
  123. $TYPE = 'ignored';
  124. } else if ($EXT == 'tmp' || $EXT == 'gitignore' || $EXT == 'ds_store' ||
  125. preg_match('/^\.smbdelete/', $NAME)) {
  126. $TYPE = 'ignored';
  127. } else if (is_dir($path)) {
  128. $TYPE = 'folder';
  129. } else if ($EXT == 'm4v' || $EXT == 'mkv' || $EXT == 'mp4' || $EXT == 'mov' ||
  130. $EXT == 'vob' || $EXT == 'iso' || $EXT == 'avi') {
  131. $TYPE = 'video';
  132. } else if ($EXT == 'mp3' || $EXT == 'aac' || $EXT == 'm4a' || $EXT == 'm4b' ||
  133. $EXT == 'm4p' || $EXT == 'wav') {
  134. $TYPE = 'audio';
  135. } else if ($EXT == 'epub' || $EXT == 'pdf') {
  136. $TYPE = 'book';
  137. } else if ($EXT == 'jpg' || $EXT == 'png') {
  138. $TYPE = 'image';
  139. } else if ($EXT == 'gz' || $EXT == 'zip' || $EXT == 'xz') {
  140. $TYPE = 'archive';
  141. } else if ($EXT == 'itc' || $EXT == 'itl' || $EXT == 'strings' || $EXT == 'itdb' ||
  142. $EXT == 'plist' || $EXT == 'ipa' || $EXT == 'ini') {
  143. $TYPE = 'database';
  144. } else if ($EXT == 'clip' || $EXT == 'riff' || $EXT == 'nfo') {
  145. $TYPE = 'metadata';
  146. } else if ($EXT == 'webloc' || $NAME == 'skip' || $NAME == 'season_done' ||
  147. $NAME == 'more_number_formats' || $NAME == 'no_quality_checks' ||
  148. $NAME == 'filler' || $NAME == 'search_name' || $EXT == 'disabled' ||
  149. $NAME == 'must_match' || $EXT == 'fakeshow' || $EXT == 'filler' ||
  150. $NAME == 'excludes' || $NAME == 'search_by_date' || $EXT == 'twopart') {
  151. $TYPE = 'metadata';
  152. } else if ($EXT == 'fake' || $EXT == 'txt' || $EXT == 'json' ||
  153. $EXT == 'bup' || $EXT == 'ifo') {
  154. $TYPE = 'metadata';
  155. }
  156. if ($TYPE == 'other') {
  157. die('Unknown file type: ' . $path . ': ' . $NAME . '^' . $EXT . "\n");
  158. }
  159. if ($DEBUG) {
  160. echo 'Adding: ' . $path . ': ' . $TYPE . "\n";
  161. }
  162. $insert->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':type' => $TYPE));
  163. if ($insert->rowCount() != 1) {
  164. print_r($insert->errorInfo());
  165. die('Unable to insert: ' . $path . "\n");
  166. }
  167. } else {
  168. $TYPE = $result['type'];
  169. }
  170. # Skip 'ingnored' files
  171. if ($TYPE == 'ignored') {
  172. continue;
  173. }
  174. # Update the mtime as needed
  175. $mtime = trim(shell_exec('stat -c "%Y" ' . escapeshellarg($path)));
  176. $check_mtime->execute(array(':base' => $BASE_LOCAL, ':path' => $file));
  177. $result = $check_mtime->fetch(PDO::FETCH_ASSOC);
  178. if (!$result && !$result['mtime'] || !$mtime) {
  179. die('Unable to find mtime for: ' . $path . "\n");
  180. } else if ($result['mtime'] < $mtime) {
  181. $set_mtime->execute(array(':base' => $BASE_LOCAL, ':path' => $file));
  182. }
  183. # Update hashes as needed
  184. if ($TYPE != 'folder') {
  185. $hash_time_check->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':mtime' => $mtime));
  186. $result = $hash_time_check->fetch(PDO::FETCH_ASSOC);
  187. if ($result) {
  188. if ($DEBUG) {
  189. echo 'Adding hash: ' . $path . "\n";
  190. }
  191. $hash = trim(shell_exec('md5sum ' . escapeshellarg($path) . ' | cut -d " " -f 1'));
  192. if (strlen($hash) == 32) {
  193. $set_hash->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':hash' => $hash));
  194. } else {
  195. die('Invalid hash (' . $hash . ') for file: ' . $path . "\n");
  196. }
  197. unset($hash);
  198. } else {
  199. if ($DEBUG) {
  200. echo 'Hash is up-to-date: ' . $path . "\n";
  201. }
  202. }
  203. } else {
  204. if ($DEBUG) {
  205. echo 'No hash for folder: ' . $path . "\n";
  206. }
  207. }
  208. unset($mtime);
  209. unset($NAME);
  210. unset($EXT);
  211. unset($path);
  212. }
  213. unset($FILES);
  214. unset($select);
  215. unset($insert);
  216. unset($check_mtime);
  217. unset($set_mtime);
  218. unset($hash_time_check);
  219. unset($set_hash);
  220. # Update priorities
  221. $priority = $dbh->prepare('UPDATE files SET priority = :priority WHERE path LIKE :path');
  222. $priority->execute(array(':priority' => 100, ':path' => 'Movies/%'));
  223. $priority->execute(array(':priority' => 50, ':path' => 'iTunes/%'));
  224. $priority->execute(array(':priority' => -50, ':path' => 'Backups/%'));
  225. $priority->execute(array(':priority' => -100, ':path' => 'TV/%'));
  226. unset($priority);
  227. # Cleanup
  228. unset($dbh);
  229. ?>