Ejemplo n.º 1
0
 /**
  * Takes an uploaded file and makes an object. It doesn't do the ->insert()
  *
  * @param  Radix $radix  The Radix where this Media belongs
  *
  * @return  \Foolz\Foolslide\Model\Media            A new Media object with the upload data
  * @throws  MediaUploadNoFileException              If there's no file uploaded
  * @throws  MediaUploadMultipleNotAllowedException  If there's multiple uploads
  * @throws  MediaUploadInvalidException             If the file format is not allowed
  */
 protected function p_forgeFromUpload(Request $request, Radix $radix)
 {
     $config = Hook::forge('Foolz\\Foolslide\\Model\\Media::upload.config')->setParams(['ext_whitelist' => ['jpg', 'jpeg', 'gif', 'png'], 'mime_whitelist' => ['image/jpeg', 'image/png', 'image/gif']])->execute()->getParams();
     if (!$request->files->count()) {
         throw new MediaUploadNoFileException(_i('You must upload an image or your image was too large.'));
     }
     if ($request->files->count() !== 1) {
         throw new MediaUploadMultipleNotAllowedException(_i('You can\'t upload multiple images.'));
     }
     /** @var UploadedFile[] $files */
     $files = $request->files->all();
     $max_size = $radix->getValue('max_image_size_kilobytes') * 1024;
     foreach ($files as $file) {
         if (!$file->isValid()) {
             if ($file->getError() === UPLOAD_ERR_INI_SIZE) {
                 throw new MediaUploadInvalidException(_i('The server is misconfigured: the Foolslide upload size should be lower than PHP\'s upload limit.'));
             }
             if ($file->getError() === UPLOAD_ERR_PARTIAL) {
                 throw new MediaUploadInvalidException(_i('You uploaded the file partially.'));
             }
             if ($file->getError() === UPLOAD_ERR_CANT_WRITE) {
                 throw new MediaUploadInvalidException(_i('The image couldn\'t be saved on the disk.'));
             }
             if ($file->getError() === UPLOAD_ERR_EXTENSION) {
                 throw new MediaUploadInvalidException(_i('A PHP extension broke and made processing the image impossible.'));
             }
             throw new MediaUploadInvalidException(_i('Unexpected upload error.'));
         }
         if (mb_strlen($file->getFilename(), 'utf-8') > 64) {
             throw new MediaUploadInvalidException(_i('You uploaded a file with a too long filename.'));
         }
         if (!in_array(strtolower($file->getClientOriginalExtension()), $config['ext_whitelist'])) {
             throw new MediaUploadInvalidException(_i('You uploaded a file with an invalid extension.'));
         }
         if (!in_array(strtolower($file->getMimeType()), $config['mime_whitelist'])) {
             throw new MediaUploadInvalidException(_i('You uploaded a file with an invalid mime type.'));
         }
         if ($file->getClientSize() > $max_size && !$this->getAuth()->hasAccess('media.limitless_media')) {
             throw new MediaUploadInvalidException(_i('You uploaded a too big file. The maxmimum allowed filesize is %s', $radix->getValue('max_image_size_kilobytes')));
         }
     }
     $media = new Media($this->getContext());
     $media->setTempFile($radix, $files['file_image']);
     return $media;
 }