| 28 | | // First delete any videos that do not exist anymore |
| | 28 | // Scan for new videos first, so we can relocate any that moved before they get deleted |
| | 29 | foreach ($video_dirs as $path) { |
| | 30 | $files = array(); |
| | 31 | $qpath = escapeshellarg($path); |
| | 32 | exec("find -L $qpath -name '.*' -prune -o -type f -print", $files, $retval); |
| | 33 | foreach ($files as $file) { |
| | 34 | if ( in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), $Known_Exts) === FALSE ) |
| | 35 | continue; |
| | 36 | // Check readable status |
| | 37 | if (!is_readable($file)) |
| | 38 | continue; |
| | 39 | // Strip the directory off of the filename |
| | 40 | $fullpath = $file; |
| | 41 | $file = preg_replace('#^'.preg_quote($path).'/*#', '', $file); |
| | 42 | // Already exists at this location? |
| | 43 | $exists = $db->query_col(' |
| | 44 | SELECT COUNT(1) |
| | 45 | FROM videometadata |
| | 46 | WHERE filename = ?', |
| | 47 | $file); |
| | 48 | if ($exists != 0) { |
| | 49 | continue; |
| | 50 | } |
| | 51 | // Already exists at some other location? (file got moved?) |
| | 52 | $hashes = array(); |
| | 53 | $qpath = escapeshellarg($fullpath); |
| | 54 | exec("mythhash $qpath", $hashes, $retval); |
| | 55 | $hash = $hashes[0]; // should only be one line anyway |
| | 56 | if ($hash != '') { |
| | 57 | $id = $db->query_col('SELECT intid FROM videometadata WHERE hash = ?', $hash); |
| | 58 | // If it already exists, just move it |
| | 59 | if ($id != 0) { |
| | 60 | $db->query('UPDATE videometadata SET filename = ? WHERE intid = ?', $file, $id); |
| | 61 | continue; |
| | 62 | } |
| | 63 | } |
| | 64 | // Add to the database |
| | 65 | $filename = basename($file); |
| | 66 | $title = filenametotitle($filename); |
| | 67 | $db->query('INSERT INTO videometadata ( title, filename, host, showlevel, browse, hash ) |
| | 68 | VALUES ( ?, ?, ?, 1, ?, ? )', |
| | 69 | strlen($title) > 0 ? $title : $filename, |
| | 70 | $file, |
| | 71 | hostname, |
| | 72 | setting('VideoNewBrowsable', hostname), |
| | 73 | $hash |
| | 74 | ); |
| | 75 | } |
| | 76 | } |
| | 77 | |
| | 78 | // Now that we've matched any orphans with their records in the database, we |
| | 79 | // can delete any remaining videos from the db that do not exist anymore |
| 69 | | // Now scan for any new ones |
| 70 | | foreach ($video_dirs as $path) { |
| 71 | | $files = array(); |
| 72 | | exec("find -L $path -name '.*' -prune -o -type f -print", $files, $retval); |
| 73 | | foreach ($files as $file) { |
| 74 | | if ( in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), $Known_Exts) === FALSE ) |
| 75 | | continue; |
| 76 | | // Check readable status |
| 77 | | if (!is_readable($file)) |
| 78 | | continue; |
| 79 | | // Strip the directory off of the filename |
| 80 | | $file = preg_replace('#^'.preg_quote($path).'/*#', '', $file); |
| 81 | | // Already exists? |
| 82 | | $exists = $db->query_col(' |
| 83 | | SELECT COUNT(1) |
| 84 | | FROM videometadata |
| 85 | | WHERE filename = ?', |
| 86 | | $file); |
| 87 | | if ($exists != 0) { |
| 88 | | continue; |
| 89 | | } |
| 90 | | // Add to the database |
| 91 | | $filename = basename($file); |
| 92 | | $title = filenametotitle($filename); |
| 93 | | $db->query('INSERT INTO videometadata ( title, filename, host, showlevel, browse ) |
| 94 | | VALUES ( ?, ?, ?, 1, ? )', |
| 95 | | strlen($title) > 0 ? $title : $filename, |
| 96 | | $file, |
| 97 | | hostname, |
| 98 | | setting('VideoNewBrowsable', hostname) |
| 99 | | ); |
| 100 | | } |
| 101 | | } |
| 102 | | |