Example #1
0
 /**
  * @param IFile|IImage|IFileUpload|FileUpload|UploadedFileInterface $file
  * @return IFile|IImage
  */
 public function save($file)
 {
     if ($file instanceof IFile || $file instanceof IImage) {
         return $this->storage->save($file);
     }
     if ($file instanceof UploadedFileInterface || $file instanceof IFileUpload) {
         return $this->storage->saveUploaded($file);
     }
     if ($file instanceof FileUpload) {
         $netteUpload = \vojtabiberle\MediaStorage\Bridges\Nette\Http\FileUpload::createFromNetteFileUpload($file);
         return $this->storage->saveUploaded($netteUpload);
     }
     throw new NoFileUploadedException('Can\'not save object of type "' . get_class($file) . '"');
 }
 public function handleUpload(Form $form, $values)
 {
     try {
         if (is_array($values['upload'])) {
             /** @var FileUpload $fileUpload */
             foreach ($values['upload'] as $fileUpload) {
                 $file = \vojtabiberle\MediaStorage\Bridges\Nette\Http\FileUpload::createFromNetteFileUpload($fileUpload);
                 $this->manager->save($file);
                 $this->presenter->flashMessage('File "' . $file->getName() . '" was uploaded');
             }
         } else {
             throw new NoFileUploadedException('No files was uploaded');
         }
     } catch (RuntimeException $e) {
         $this->presenter->flashMessage($e->getMessage(), 'error');
     }
 }
 public function handleUpload(Form $form, $values)
 {
     try {
         if (is_array($values['upload'])) {
             /** @var FileUpload $fileUpload */
             foreach ($values['upload'] as $fileUpload) {
                 $file = \vojtabiberle\MediaStorage\Bridges\Nette\Http\FileUpload::createFromNetteFileUpload($fileUpload);
                 $file = $this->manager->save($file);
                 $info = new \stdClass();
                 $info->fileUID = $file->getUID();
                 //TODO: Configurable size here?
                 if ($file->isImage()) {
                     $link = $this->presenter->link(':MediaStorage:Images:', ['name' => $file, 'size' => 'size-grid-thumbnail']);
                     $fullLink = $this->presenter->link(':MediaStorage:Images:', ['name' => $file]);
                 } else {
                     $link = $this->presenter->link(':MediaStorage:Icons:', ['name' => $file->getIconName(), 'size' => 'size-grid-thumbnail']);
                     $fullLink = $this->presenter->link(':MediaStorage:Files:', ['name' => $file]);
                 }
                 $info->fileLink = $link;
                 $info->fileFullLink = $fullLink;
                 $info->fileName = $file->getName();
                 $info->fileIsImage = $file->isImage();
                 $info->fileDeleteLink = $this->presenter->link(':MediaStorage:Media:delete', [$file->getUID()]);
                 $this->presenter->sendResponse(new JsonResponse($info));
             }
         } else {
             $info = new \stdClass();
             $info->error = true;
             $info->message = 'No files was uploaded';
             $this->presenter->sendResponse(new JsonResponse($info));
         }
     } catch (RuntimeException $e) {
         $info = new \stdClass();
         $info->error = true;
         $info->message = $e->getMessage();
         $this->presenter->sendResponse(new JsonResponse($info));
     }
 }