Example #1
0
 /**
  * Helper function to create a directory if it doesn't exist.
  * @param <type> $path Path to dir to create
  * @return <type>
  */
 private function createFolder($path)
 {
     /* check if folder exists */
     return is_dir($path) or filesystem::createDirectory($path);
 }
Example #2
0
 public function prepare_upload($uploadvar = 'upload')
 {
     if (!arr::get($_FILES, 'mediafile', 'name', $uploadvar)) {
         kohana::log('error', 'Attempted to prepare upload without file');
         return 'Please provide a file to upload';
     }
     $uploadedFile = arr::get(arr::rotate($_FILES['mediafile']), $uploadvar);
     switch ($uploadedFile['error']) {
         case UPLOAD_ERR_INI_SIZE:
             return 'File exceeds upload_max_filesize';
         case UPLOAD_ERR_FORM_SIZE:
             return 'File exceeds MAX_FILE_SIZE';
         case UPLOAD_ERR_PARTIAL:
             return 'File was only partially uploaded';
         case UPLOAD_ERR_NO_FILE:
             return 'No file was uploaded';
         case UPLOAD_ERR_NO_TMP_DIR:
             return 'Missing a temporary folder';
         case UPLOAD_ERR_CANT_WRITE:
             return 'Failed to write file to disk';
         case UPLOAD_ERR_EXTENSION:
             return 'Invalid file extension (type)';
         case UPLOAD_ERR_OK:
             if (!$this->get('file')) {
                 $uploadFilename = $uploadedFile['name'];
                 if (Kohana::config('upload.remove_spaces') === TRUE) {
                     $uploadFilename = preg_replace('/\\s+/', '_', $uploadFilename);
                 }
                 $this->set('file', $uploadFilename);
             }
             if ($this->get('path')) {
                 $path = trim($this->get('path'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
                 $this->set('path', $path);
             }
             if (!is_file($uploadedFile['tmp_name'])) {
                 kohana::log('error', 'Unable to locate file in temporary storage ' . $uploadedFile['tmp_name']);
                 return 'Unable to upload file';
             }
             if (!($mediainfo = MediaLib::getAudioInfo($uploadedFile['tmp_name']))) {
                 kohana::log('error', 'Unable to determine audio info for tmp upload file "' . $uploadedFile['tmp_name'] . '"');
                 return 'Upload is not a valid audio file or format';
             }
             $this->fromArray($mediainfo);
             if (kohana::config('mediafile.upload_to_rate_folders')) {
                 $rate = $this->get('rates');
                 $path = $this->get('path');
                 if (in_array($rate, kohana::config('mediafile.default_rates')) and !strstr($path, $rate . DIRECTORY_SEPARATOR)) {
                     $path .= $this->get('rates') . DIRECTORY_SEPARATOR;
                     $this->set('path', $path);
                 } else {
                     if ($unknownPath = kohana::config('mediafile.unknown_rate_folder')) {
                         $path .= trim($unknownPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
                         $this->set('path', $path);
                     }
                 }
             }
             $directory = $this->filepath();
             if (!$this->get('name') or !$this->get('description')) {
                 $mediafiles = $this->get_resampled();
                 if (!$this->get('name')) {
                     if (isset($mediafiles[0]['name'])) {
                         $this->set('name', $mediafiles[0]['name']);
                     } else {
                         $this->set('name', pathinfo($uploadedFile['name'], PATHINFO_FILENAME));
                     }
                 }
                 if (!$this->get('description')) {
                     if (isset($mediafiles[0]['description'])) {
                         $this->set('description', $mediafiles[0]['description']);
                     }
                 }
             }
             if (!$directory or !filesystem::is_writable($directory) and !filesystem::createDirectory($directory)) {
                 kohana::log('error', 'The configured media dir is not writable, please chmod "' . $directory . '"');
                 return 'Media collection directory is not writable';
             }
             $this->uploaded_file = $uploadedFile;
             break;
         default:
             return 'Upload failed for an unspecified reason';
     }
     return FALSE;
 }