예제 #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)
 {
     if (!empty($_GET['debug'])) {
         WPFB_Sync::PrintDebugTrace("scanning_file:" . $file->GetLocalPathRel());
     }
     $file_path = $file->GetLocalPath();
     $tmp_file = false;
     if (!$file->IsLocal()) {
         $res = WPFB_Admin::SideloadFile($file->GetRemoteUri(), $file_path);
         if ($res['error']) {
             return false;
         }
         $tmp_file = true;
     } elseif (!is_file($file_path) || !is_readable($file_path)) {
         if (WPFB_Core::$settings->remove_missing_files) {
             $file->Remove();
             return true;
         } else {
             $file->file_offline = true;
             $file->file_mtime = 0;
             $file->file_rescan_pending = 1;
             $file->DbSave();
             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)) {
             $thumb = false;
             $pwe = substr($file->GetLocalPath(), 0, strrpos($file->GetLocalPath(), '.') + 1);
             if ($pwe && (file_exists($thumb = $pwe . 'png') || file_exists($thumb = $pwe . 'jpg') || file_exists($thumb = $pwe . 'gif'))) {
                 $file->file_thumbnail = basename($thumb);
                 $dthumb = $file->GetThumbPath(true);
                 if ($dthumb != $thumb) {
                     $dir = dirname($dthumb);
                     if (!is_dir($dir)) {
                         WPFB_Admin::Mkdir($dir);
                     }
                     rename($thumb, $dthumb);
                 }
             }
         }
     }
     $file->file_rescan_pending = 1;
     $file->DBSave();
     WPFB_GetID3::UpdateCachedFileInfo($file);
     return true;
 }
예제 #2
0
 /**
  * 
  * @global type $wpdb
  * @param WPFB_File $file
  * @param type $info
  * @return type
  */
 static function StoreFileInfo($file, $info)
 {
     global $wpdb;
     if (empty($file->file_thumbnail)) {
         if (!empty($info['comments']['picture'][0]['data'])) {
             $cover_img =& $info['comments']['picture'][0]['data'];
         } elseif (!empty($info['id3v2']['APIC'][0]['data'])) {
             $cover_img =& $info['id3v2']['APIC'][0]['data'];
         } else {
             $cover_img = null;
         }
         // TODO unset pic in info?
         if (!empty($cover_img)) {
             $cover = $file->GetLocalPath();
             $cover = substr($cover, 0, strrpos($cover, '.')) . '.jpg';
             file_put_contents($cover, $cover_img);
             $file->CreateThumbnail($cover, true);
             @unlink($cover);
             $cf_changed = true;
         }
     }
     self::cleanInfoByRef($info);
     // set encoding to utf8 (required for getKeywords)
     if (function_exists('mb_internal_encoding')) {
         $cur_enc = mb_internal_encoding();
         mb_internal_encoding('UTF-8');
     }
     $keywords = array();
     self::getKeywords($info, $keywords);
     $keywords = strip_tags(join(' ', $keywords));
     $keywords = str_replace(array('\\n', '
'), '', $keywords);
     $keywords = preg_replace('/\\s\\s+/', ' ', $keywords);
     if (!function_exists('mb_detect_encoding') || mb_detect_encoding($keywords, "UTF-8") != "UTF-8") {
         $keywords = utf8_encode($keywords);
     }
     // restore prev encoding
     if (function_exists('mb_internal_encoding')) {
         mb_internal_encoding($cur_enc);
     }
     // don't store keywords 2 times:
     unset($info['keywords']);
     self::removeLongData($info, 8000);
     $data = empty($info) ? '0' : base64_encode(serialize($info));
     $res = $wpdb->replace($wpdb->wpfilebase_files_id3, array('file_id' => (int) $file->GetId(), 'analyzetime' => time(), 'value' => &$data, 'keywords' => &$keywords));
     unset($data, $keywords);
     $cf_changed = false;
     // TODO: move this cleanup into a callback / should NOT be HERE!
     if ($file->file_rescan_pending) {
         $file->file_rescan_pending = 0;
         $cf_changed = true;
     }
     // delete local temp file
     if ($file->IsRemote() && file_exists($file->GetLocalPath())) {
         @unlink($file->GetLocalPath());
     }
     // TODO END;
     if ($cf_changed && !$file->IsLocked()) {
         $file->DbSave(true);
     }
     return $res;
 }