Exemple #1
0
 /**
  * Parses and converts music files
  * @method parse
  * @param id the ID of the media object to parse
  */
 public function parse($params)
 {
     if ($this->ensureLocalhost() === false) {
         return $this->formatOutput(__('You have no access to this method'), self::METHOD_NOT_AVAILABLE);
     }
     if (FormValidation::isDigit($params['id'])) {
         return $this->formatOutput(__('Not a valid numerical value provided for the media ID'), self::INVALID_PARAMETER_VALUE);
     }
     //Fetch media object with provided ID
     $media = Media::find($params['id']);
     //Make sure the requested media object exists
     if (!$media instanceof Media) {
         return $this->formatOutput(__('No media object with the provided ID exists'), self::ITEM_NOT_FOUND);
     }
     if ($media->status === Media::STATUS_FINISHED) {
         return $this->formatOutput(__('This media object has already been parsed'), self::ERROR_IN_REQUEST);
     }
     //Set our media file to the processing status
     $media->save(array('status' => Media::STATUS_PROCESSING));
     //Initialize our parser class
     $parser = new Parser();
     //Our main storage path
     $path = $parser->getMediaDir();
     //Get the path from the file in the temporary Directory
     $filepath = $parser->getTempMediaDir() . '/' . $media->filename;
     //Make sure our root path exists and is writable
     if (!is_dir($path) && !mkdir($path) || !is_writable($path)) {
         return $this->formatOutput(__('Directory ' . $path . ' is not writable, Permission denied'), self::ERROR_IN_REQUEST);
     }
     //Make sure our file exists
     if (!file_exists($filepath)) {
         return $this->formatOutput(__('Media file ' . $filepath . ' does not exist'), self::ERROR_IN_REQUEST);
     }
     //Make sure our file is readable
     if (!is_readable($filepath)) {
         return $this->formatOutput(__('Cannot read media file ' . $filepath . ', Permission denied'), self::ERROR_IN_REQUEST);
     }
     //Since its just an audio file, parse immediately
     $media->status = $parser->parse($media);
     //If the parse succeeded, save it in the database
     if ($media->status == Media::STATUS_FINISHED || $media->status == Media::STATUS_ERROR) {
         $media->save();
     }
     //If the parse failed, the status will be set to the relevant code. Also no need to save the record
     /*
         @TODO
         Perhaps implement some form of converting to mp3 here?
         Or preserve this method for downloading of youtube videos, and converting those to mp3's
     */
     return $this->formatOutput((array) $media);
 }