Exemplo n.º 1
0
 public function find_or_create_by_episode_id_and_episode_asset_id($episode_id, $episode_asset_id)
 {
     if (!($file = self::find_by_episode_id_and_episode_asset_id($episode_id, $episode_asset_id))) {
         $file = new MediaFile();
         $file->episode_id = $episode_id;
         $file->episode_asset_id = $episode_asset_id;
         $file->save();
     }
     return $file;
 }
 public function actionUpload($model_id, $object_id, $tag)
 {
     if ($object_id == 0) {
         $object_id = 'tmp_' . Yii::app()->user->id;
     }
     $model = new MediaFile('insert');
     $model->object_id = $object_id;
     $model->model_id = $model_id;
     $model->tag = $tag;
     if ($model->save()) {
         $this->sendFilesAsJson($model);
     } else {
         echo CJSON::encode(['textStatus' => $model->error]);
     }
 }
Exemplo n.º 3
0
 public function run()
 {
     $file = new MediaFile('search', 'local');
     $file->getDbCriteria()->mergeWith(['condition' => 'target_api IS NOT NULL AND status=:status', 'params' => ['status' => MediaFile::STATUS_ACTIVE]]);
     $files = $file->findAll();
     foreach ($files as $file) {
         $local_api = $file->getApi();
         if ($local_api instanceof LocalApi) {
             $file->setApi($file->target_api);
             $file->convertFromLocal($local_api);
             $file->target_api = null;
             $file->save();
         }
     }
 }
Exemplo n.º 4
0
 protected function saveGallery($gallery)
 {
     $album = new MediaAlbum();
     $album->title = $gallery['title'];
     $album->model_id = 'sherdog.com';
     $album->object_id = $gallery['id'];
     $album->save(false);
     foreach ($gallery['imgs'] as $img) {
         $file = new MediaFile('create', 'remote');
         $file->model_id = get_class($album);
         $file->object_id = $album->id;
         $file->tag = 'images';
         $file->title = $img['title'];
         $file->remote_id = $img['img'];
         $file->save(false);
     }
 }
Exemplo n.º 5
0
 private function upload($tmpfile, $shortname, $description = '', $replace = false)
 {
     try {
         $audioInfo = MediaScanner::getAudioInfo($tmpfile);
     } catch (Exception $e) {
         message::set('File error: ' . $e->getMessage());
         return FALSE;
     }
     $destfile = Media::getMediaFilename($shortname, $audioInfo['rates'][0], FALSE);
     // Get whatever the proper name should be
     $dir = dirname($destfile);
     /* can we write to the target folder? */
     if (!filesystem::is_writable($dir)) {
         message::set('The path ' . $dir . ' is not writable!');
         return FALSE;
     }
     $this->createFolder($dir);
     if (!is_writable(dirname($destfile)) or file_exists($destfile) and !is_writable($destfile)) {
         message::set(dirname($destfile) . ' is not writable');
         return FALSE;
     }
     try {
         move_uploaded_file($tmpfile, $destfile);
     } catch (Exception $e) {
         message::set('Unable to move uploaded file into ' . $destfile . '. ' . $e->getMessage());
         return FALSE;
     }
     // See if this is in the DB
     $mediaFile = Doctrine::getTable('MediaFile')->findOneByFile($shortname);
     if ($mediaFile) {
         // Note that this is a bit dangerous and could use improvement.
         // We assume that all other properties in the file we just found match the file already uploaded.
         // That means if someone uploads the wrong audio file, it kinda messes things up big time.
         if (!in_array($audioInfo['byterate'], (array) $mediaFile['registry']['rates'])) {
             Kohana::log('debug', 'Updating ' . $shortname . "...");
             $registry = (array) $mediaFile['registry'];
             $registry['rates'][] = $audioInfo['byterate'];
             $mediaFile['registry'] = $registry;
             $mediaFile['description'] = strlen($description) > 0 ? $description : $mediaFile['description'];
             $mediaFile->save();
         } else {
             if (strcmp($mediaFile['description'], $description)) {
                 $mediaFile['description'] = $description;
                 $mediaFile->save();
             } else {
                 Kohana::log('debug', 'SKIPPED DB UPDATE - Nothing to update on ' . $shortname . " with sample rate " . $audioInfo['byterate'] . "... ");
             }
         }
         message::set('Successfully updated audio file in the system.', 'success');
         url::redirect(Router_Core::$controller . '/index');
     } else {
         // NEW FILE! Do lots of stuff
         // Save info about file
         $mediaFile = new MediaFile();
         $mediaFile['file'] = $shortname;
         $mediaFile['path'] = dirname($mediaFile['file']);
         // We track the path separately to ease searching
         $mediaFile['account_id'] = 1;
         // See if we know this filename, description & category from the XML info
         if (isset($descriptions[$shortname])) {
             $mediaFile['description'] = $descriptions[$shortname];
         } else {
             if (strlen($description) > 0) {
                 $mediaFile['description'] = $description;
             } else {
                 $mediaFile['description'] = 'Unknown';
             }
         }
         Kohana::log('debug', 'Adding ' . $mediaFile['file'] . " to the database.");
         $mediaFile['registry'] += $audioInfo;
         $mediaFile->save();
         message::set('Successfully added audio file to the system.', 'success');
         url::redirect(Router_Core::$controller . '/index');
     }
 }
Exemplo n.º 6
0
 protected function saveGallery($gallery)
 {
     $user = User::model()->findByAttributes(['email' => '*****@*****.**']);
     $album = new MediaAlbum();
     $album->title = $gallery['title'];
     $album->model_id = get_class($user);
     $album->object_id = $user->id;
     $album->source = 'sherdog.com';
     $album->source_id = $gallery['id'];
     $album->status = MediaAlbum::STATUS_ACTIVE;
     if (!$album->save()) {
         throw new CException(json_encode($album->getErrors()));
     }
     $order = 0;
     foreach ($gallery['imgs'] as $img) {
         if (!$img['path']) {
             continue;
         }
         $file = new MediaFile('insert', 'local');
         $file->model_id = get_class($album);
         $file->object_id = $album->id;
         $file->tag = 'files';
         $file->title = $img['title'];
         $file->remote_id = $img['path'];
         $file->order = ++$order;
         $file->getApi()->need_upload = false;
         if (!$file->save()) {
             throw new CException(json_encode($file->getErrors()));
         }
     }
 }
Exemplo n.º 7
0
 public static function scan($soundPath, $fileTypes)
 {
     set_time_limit(0);
     // TODO: Make this a queued event to scan all files. Only possible once.
     /*
      * Load everything into memory that we know about our existing sound files.
      * This may seem expensive but it shouldn't be - the info is tiny and a full
      * rescan will require all this data anyway.
      */
     // Get the list of known files already in the system
     $results = Doctrine_Query::create()->select('m.mediafile_id, m.file, m.registry')->from('MediaFile m')->execute(NULL, Doctrine::HYDRATE_ARRAY);
     $listedFiles = array();
     foreach ($results as $result) {
         $listedFiles[$result['file']] = array('mediafile_id' => $result['mediafile_id'], 'registry' => $result['registry'], 'path' => $soundPath . $result['file']);
     }
     kohana::log('debug', 'Found ' . count($listedFiles) . ' listed files');
     if (version_compare(PHP_VERSION, '5.2.3', '<')) {
         $knownFiles = array();
         foreach ($listedFiles as $idx => $file) {
             if (self::filterKnownFiles($file)) {
                 $knownFiles[$idx] = $file;
             }
         }
         kohana::log('debug', 'foreach: Of ' . count($listedFiles) . ' listed, kept ' . count($knownFiles));
     } else {
         $knownFiles = array_filter($listedFiles, "MediaScanner::filterKnownFiles");
         kohana::log('debug', 'filter: Of ' . count($listedFiles) . ' listed, kept ' . count($knownFiles));
     }
     // TODO: Fix this. Download descriptions from the web?
     if (file_exists(MODPATH . 'mediamanager-1.0' . DIRECTORY_SEPARATOR . 'audio_descriptions.ini')) {
         $fp = fopen(MODPATH . 'mediamanager-1.0' . DIRECTORY_SEPARATOR . 'audio_descriptions.ini', 'r');
         while ($row = fgetcsv($fp)) {
             $descriptions[$row[0]] = $row[1];
         }
     } else {
         $descriptions = array();
     }
     /*
      * Now compare what we know with what we find on disk and add any new stuff
      */
     // Initialize iterator
     $dir_iterator = new RecursiveDirectoryIterator($soundPath);
     $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
     // Read in a list of files already registered in the system
     $fileTypeMatch = '/^.+\\.(' . implode('|', $fileTypes) . ')$/i';
     $regex = new RegexIterator($iterator, $fileTypeMatch, RecursiveRegexIterator::GET_MATCH);
     kohana::log('debug', 'Starting foreach for MediaScanner');
     $starttime = microtime(TRUE);
     foreach ($regex as $fileinfo) {
         $filename = $fileinfo[0];
         $shortname = str_replace($soundPath, '', self::NormalizeFSNames($filename));
         $framerate = basename(dirname($filename));
         // Is an existing one?
         if (isset($knownFiles[$shortname])) {
             $mediafile_id = $knownFiles[$shortname]['mediafile_id'];
             $registry = (array) $knownFiles[$shortname]['registry'];
             if (!in_array($framerate, (array) $registry['rates'])) {
                 //$info = self::getAudioInfo($filename);
                 $registry['rates'][] = $framerate;
                 //$registry = arr::merge((array)$registry, $info);
                 Doctrine_Query::create()->update('MediaFile m')->set('m.registry', '?', serialize($registry))->where('m.mediafile_id = ?', $mediafile_id)->execute();
                 kohana::log('debug', 'Updating ' . $filename . ' with sample rate ' . $framerate . '...');
                 // Add to list of "known" files
                 $knownFiles[$shortname]['registry'] = $registry;
             }
         } else {
             kohana::log('debug', $filename . ' is a new file');
             // NEW FILE! Do lots of stuff
             $mediaFile = new MediaFile();
             $mediaFile['file'] = $shortname;
             $mediaFile['path'] = dirname($mediaFile['file']);
             // We track the path separately to ease searching
             $mediaFile['account_id'] = 1;
             // See if we know this filename, description & category from the XML info
             if (isset($descriptions[$shortname])) {
                 $mediaFile['description'] = $descriptions[$shortname];
             } else {
                 $mediaFile['description'] = 'Unknown';
             }
             try {
                 $mediaFile['registry'] += self::getAudioInfo($filename);
                 $mediaFile->save();
                 // Add to list of "known" files
                 $knownFiles[$mediaFile['file']] = array('mediafile_id' => $mediaFile['mediafile_id'], 'registry' => $mediaFile['registry'], 'path' => $soundPath . $mediaFile['file']);
             } catch (Exception $e) {
                 kohana::log('debug', 'Unable to save audio info: ' . $e->getMessage());
             }
         }
     }
     $endtime = microtime(TRUE);
     kohana::log('debug', 'scan foreach took ' . ($endtime - $starttime) . ' msec');
     Kohana::log('debug', 'Finished scanning sound files in ' . $soundPath);
 }