Ejemplo n.º 1
0
 /**
  * 1. Checks if file is actually present and readable on file system and either sets file offline or completly removes it
  * 2. If file hash not set, calculate it
  * 3. Try to generate a thumbnail if it does not exists
  * 4. Update ID3 info (with _async_ RPC if enabled)
  *
  * @param WPFB_File $file
  * @param bool      $forced_refresh_thumb
  *
  * @return bool
  */
 static function ScanFile($file, $forced_refresh_thumb = false, $allow_async = true)
 {
     $forced_refresh_thumb = $forced_refresh_thumb || $file->file_rescan_pending > 1;
     $file->file_rescan_pending = max($file->file_rescan_pending, $forced_refresh_thumb ? 2 : 1);
     if (!$file->TryScanLock()) {
         WPFB_Core::LogMsg("ERROR: ScanFile {$file} locking failed!", 'sync');
         return false;
     }
     $file_path = $file->GetLocalPath();
     if (!$file->IsLocal()) {
         $res = WPFB_Admin::SideloadFile($file, $file_path);
         if ($res['error']) {
             WPFB_Core::LogMsg("ERROR: ScanFile({$file}) download {$file->GetRemoteUri()} failed {$res['error']}!", 'sync');
             $file->file_rescan_pending = 0;
             $file->DbSave(true);
             return false;
         }
     } elseif (!is_file($file_path) || !is_readable($file_path)) {
         if (WPFB_Core::$settings->remove_missing_files) {
             WPFB_Core::LogMsg("ScanFile({$file}) removing missing file!", 'sync');
             $file->Remove();
             return true;
         } else {
             $file->file_offline = true;
             $file->file_mtime = 0;
             $file->DbSave(true);
             return true;
         }
     }
     if (empty($file->file_hash)) {
         $file->file_hash = WPFB_Admin::GetFileHash($file->GetLocalPath());
     }
     if (empty($file->file_thumbnail) || $forced_refresh_thumb || !is_file($file->GetThumbPath())) {
         $file->Lock(true);
         $file->CreateThumbnail();
         // this only deltes old thumb if success
         $file->Lock(false);
         if (WPFB_Core::$settings->base_auto_thumb && (empty($file->file_thumbnail) || !is_file($file->GetThumbPath()))) {
             $pwe = substr($file->GetLocalPath(), 0, strrpos($file->GetLocalPath(), '.') + 1);
             if ($pwe && (file_exists($thumb = $pwe . 'png') || file_exists($thumb = $pwe . 'thumb.png') || file_exists($thumb = $pwe . 'jpg') || file_exists($thumb = $pwe . 'thumb.jpg') || file_exists($thumb = $pwe . 'gif') || file_exists($thumb = $pwe . 'thumb.gif'))) {
                 $file->file_thumbnail = basename($thumb);
                 $dest_thumb = $file->GetThumbPath(true);
                 if ($dest_thumb != $thumb) {
                     $dir = dirname($dest_thumb);
                     if (!is_dir($dir)) {
                         WPFB_Admin::Mkdir($dir);
                     }
                     rename($thumb, $dest_thumb);
                 }
             }
         }
     }
     $file->DBSave(true);
     // the UpdateCachedFileInfo/StoreFileInfo will delete the file if necessary! (no need of $tmp_file value!)
     if (!WPFB_GetID3::UpdateCachedFileInfo($file)) {
         WPFB_Core::LogMsg("ScanFile({$file}) file scan failed!", 'sync');
         return false;
     }
     return true;
 }