Ejemplo n.º 1
0
 /**
  * @param string $namespace
  * @param FileUpload $fileUpload
  * @return SplFileInfo
  * @throws NotAllowedFileException
  */
 public function upload($namespace, FileUpload $fileUpload)
 {
     $filename = Utils::sanitizeFileName($fileUpload);
     $fileInfo = new SplFileInfo($filename);
     $suffix = $fileInfo->getExtension();
     if (in_array($suffix, $this->blacklist)) {
         throw new NotAllowedFileException("Upload of the file with {$suffix} suffix is not allowed.");
     }
     $destination = $this->storage->save($fileUpload->getTemporaryFile(), "{$namespace}/{$filename}");
     return new SplFileInfo($destination);
 }
Ejemplo n.º 2
0
 /**
  * @param FileUpload $fileUpload
  * @param NULL|string $dir
  * @return SplFileInfo
  * @throws NotAllowedFileException
  */
 public function upload(FileUpload $fileUpload, $dir = NULL)
 {
     $path = $this->basePath . '/' . $this->relativePath;
     if ($dir !== NULL) {
         $path .= '/' . $dir;
     }
     $path = Utils::normalizePath($path);
     Utils::makeDirectoryRecursive($path);
     $filename = Utils::sanitizeFileName($fileUpload);
     $fileInfo = new SplFileInfo($filename);
     $suffix = $fileInfo->getExtension();
     if (in_array($suffix, $this->blacklist)) {
         throw new NotAllowedFileException("Upload of the file with {$suffix} suffix is not allowed.");
     }
     $fileUpload->move($path . '/' . $filename);
     return new SplFileInfo($path . '/' . $filename);
 }
Ejemplo n.º 3
0
 /**
  * @param FileUpload $fileUpload
  * @param NULL|string $dir
  * @return SplFileInfo
  * @throws InvalidArgumentException
  */
 public function upload(FileUpload $fileUpload, $dir = NULL)
 {
     if (!$fileUpload->isImage()) {
         throw new InvalidArgumentException('This is not an image!');
     }
     $path = $this->basePath . '/' . $this->relativePath;
     if ($dir !== NULL) {
         $path .= '/' . $dir;
     }
     $path = Utils::normalizePath($path);
     Utils::makeDirectoryRecursive($path);
     $filename = Utils::sanitizeFileName($fileUpload);
     /** @var \Nette\Utils\Image */
     $image = $fileUpload->toImage();
     if ($this->saveOriginal) {
         $image->save($path . '/orig_' . $filename);
     }
     if ($this->type !== NULL) {
         $filename = str_replace('.' . Utils::getSuffix($filename), '.' . $this->suffix, $filename);
     }
     $image->resize($this->maxSize[0], $this->maxSize[1], Image::SHRINK_ONLY);
     $image->save($path . '/' . $filename, $this->quality, $this->type);
     foreach ($this->dimensions as $prefix => $p) {
         $image->resize($p[0][0], $p[0][1], $p[1]);
         $image->save($path . '/' . $prefix . '_' . $filename, $this->quality, $this->type);
     }
     return new SplFileInfo($path . '/' . $filename);
 }
Ejemplo n.º 4
0
 /**
  * @param string $namespace
  * @param FileUpload $fileUpload
  * @return SplFileInfo
  */
 public function upload($namespace, FileUpload $fileUpload)
 {
     if (!$fileUpload->isImage()) {
         throw new InvalidArgumentException('This is not an image!');
     }
     if (!is_dir($this->tempDir)) {
         Utils::makeDirectoryRecursive($this->tempDir);
     }
     $filename = Utils::sanitizeFileName($fileUpload);
     /** @var \Nette\Utils\Image */
     $image = $fileUpload->toImage();
     if ($this->saveOriginal) {
         $image->save($this->tempDir . '/orig_' . $filename);
         $savedOriginal = ["{$this->tempDir}/orig_{$filename}", "{$namespace}/orig_{$filename}"];
     }
     if ($this->type !== NULL) {
         $filename = str_replace('.' . Utils::getSuffix($filename), '.' . $this->suffix, $filename);
     }
     $image->resize($this->maxSize[0], $this->maxSize[1], Image::SHRINK_ONLY);
     $image->save("{$this->tempDir}/{$filename}", $this->quality, $this->type);
     $filesToSave = [];
     // has to be first
     $filesToSave[] = ["{$this->tempDir}/{$filename}", "{$namespace}/{$filename}"];
     // intently saved at the second position
     if (isset($savedOriginal)) {
         $filesToSave[] = $savedOriginal;
     }
     foreach ($this->dimensions as $prefix => $p) {
         $image->resize($p[0][0], $p[0][1], $p[1]);
         $image->save("{$this->tempDir}/{$prefix}_{$filename}", $this->quality, $this->type);
         $filesToSave[] = ["{$this->tempDir}/{$prefix}_{$filename}", "{$namespace}/{$prefix}_{$filename}"];
     }
     $results = $this->storage->bulkSave($filesToSave);
     // cleanup temp files
     foreach ($filesToSave as $file) {
         if (is_file($file[0])) {
             FileSystem::delete($file[0]);
         }
     }
     // remove complete directory
     if (is_dir($this->tempDir)) {
         FileSystem::delete($this->tempDir);
     }
     return new SplFileInfo($results[0]);
 }