/** * @ORM\PreFlush() */ public function preUpload() { if ($this->file) { if ($this->file instanceof FileUpload) { $basename = $this->file->getSanitizedName(); $basename = $this->suggestName($this->getFilePath(), $basename); $this->setName($basename); } else { $basename = trim(Strings::webalize($this->file->getBasename(), '.', FALSE), '.-'); $basename = $this->suggestName(dirname($this->file->getPathname()), $basename); $this->setName($basename); } if ($this->_oldPath && $this->_oldPath !== $this->path) { @unlink($this->getFilePathBy($this->_oldProtected, $this->_oldPath)); } if ($this->file instanceof FileUpload) { $this->file->move($this->getFilePath()); } else { copy($this->file->getPathname(), $this->getFilePath()); } return $this->file = NULL; } if (($this->_oldPath || $this->_oldProtected !== NULL) && ($this->_oldPath != $this->path || $this->_oldProtected != $this->protected)) { $oldFilePath = $this->getFilePathBy($this->_oldProtected !== NULL ? $this->_oldProtected : $this->protected, $this->_oldPath ?: $this->path); if (file_exists($oldFilePath)) { rename($oldFilePath, $this->getFilePath()); } } }
public function savaOriginImage($namespace, FileUpload $file) { if (!$file->isOk() || !$file->isImage()) { throw new ImageStorageException("invalid image"); } $i = 1; $name = $file->getSanitizedName(); $path = $this->getPath($namespace, $name); while (file_exists($path)) { $name = $i . '-' . $file->getSanitizedName(); $path = $this->getPath($namespace, $name); $i++; } $file->move($path); return $name; }
/** * Process form file upload from FileUpload wrapper class. * * @param FileUpload $file * @return string */ public static function upload(FileUpload $file) { $filename = $file->getSanitizedName(); $folder = date('Y'); $file->move(self::getUploadDir() . $folder . '/' . $filename); return $folder . '/' . $filename; }
/** * @param FileUpload $file * @return string */ public function save(FileUpload $file) { do { $filename = Strings::random(5) . '-' . $file->getSanitizedName(); $targetFile = $this->getAbsolutePath() . '/' . $filename; } while (file_exists($targetFile)); $image = $file->toImage(); $image->save($targetFile); return $filename; }
/** * @param FileUpload $file * @return mixed|void * @throws NotAllowedFileException */ public function save(FileUpload $file) { $filename = $file->getSanitizedName(); $parts = explode('.', $filename); $last = end($parts); if (in_array($last, $this->notAllowed)) { throw new NotAllowedFileException("File with {$last} suffix is not allowed."); } $file->move($this->dir . '/' . $filename); }
protected function processImageUpload(Nette\Http\FileUpload $upload, $targetDir, $id) { $dir = 'upload' . DIRECTORY_SEPARATOR . $targetDir . DIRECTORY_SEPARATOR . $id; if (!file_exists($this->rootDir . DIRECTORY_SEPARATOR . $dir)) { Nette\Utils\FileSystem::createDir($this->rootDir . DIRECTORY_SEPARATOR . $dir); } $path = $dir . DIRECTORY_SEPARATOR . $upload->getSanitizedName(); $upload->move($this->rootDir . DIRECTORY_SEPARATOR . $path); return $path; }
/** * @param FileUpload $file * @return mixed|void */ public function save(FileUpload $file) { $filename = $file->getSanitizedName(); /** @var \Nette\Image */ $image = $file->toImage(); $image->save($this->dir . '/' . $filename); foreach ($this->dimensions as $prefix => $dimension) { $image->resize($dimension[0], $dimension[1], Image::SHRINK_ONLY); $image->save($this->dir . '/' . $prefix . '_' . $filename); } }
/** * @param FileUpload $fileUpload * @return string */ public static function sanitizeFileName(FileUpload $fileUpload) { $filename = $fileUpload->getSanitizedName(); $filename = Strings::lower($filename); $fileInfo = new \SplFileInfo($filename); $suffix = $fileInfo->getExtension(); $basename = $fileInfo->getBasename(".{$suffix}"); $hash = md5($fileUpload->getContents()); $hash = Strings::substring($hash, 0, 9); return Strings::substring($basename, 0, 50) . "_{$hash}.{$suffix}"; }
$values->avatar = $this->_uploadTitleImage($values->avatar); if (is_file("www/" . $this->avatar_path . $values->old_avatar)) { unlink($this->www_dir . "/www/" . $this->avatar_path . $values->old_avatar); } $this->hlavne_menu->uloz(["avatar" => $values->avatar], $values->id); } else { throw new Database\DriverException('Pre titulný obrázok nebol použitý obrázok a tak nebol uložený!' . $e->getMessage()); } } else { throw new Database\DriverException('Pri pokuse o uloženie došlo k chybe! Pravdepodobná príčina je:' . $this->presenter->upload_error[$values->avatar->error] . $e->getMessage()); } } catch (Database\DriverException $e) {
/** * @param FileUpload $file * @return Image * @throws \Nette\InvalidArgumentException */ public function upload(FileUpload $file) { if (!$file->isOk() || !$file->isImage()) { throw new Nette\InvalidArgumentException(); } do { $name = Random::generate() . '.' . $file->getSanitizedName(); } while (file_exists($path = $this->imagesDir . "/" . $this->namespace . $this->originalPrefix . "/" . $name)); $file->move($path); $this->onUploadImage($path, $this->namespace); $this->namespace = NULL; return new Image($path); }
public function __construct(FileUpload $file) { if (!$file->isImage()) { throw new NotImageUploadedException(); } $this->id = Uuid::uuid4(); $pathInfo = pathinfo($file->getSanitizedName()); if (!isset($pathInfo['extension'])) { throw new FileNameException('Filename must have extension'); } $this->extension = $pathInfo['extension']; $this->originalName = $pathInfo['filename']; $imgSize = $file->getImageSize(); $this->width = $imgSize[0]; $this->height = $imgSize[1]; $this->fileSize = $file->getSize(); $this->uploadedAt = new \DateTime('now'); }
/** * @param FileUpload * @param string|NULL * @param array|string|NULL * @return string filepath (namespace/file.ext) * @throws ImageStorageException */ public function upload(FileUpload $image, $namespace = NULL, $mimeTypes = NULL) { if (!$image->isOk()) { throw new ImageStorageException('File is broken'); } if (!$image->isImage()) { $contentType = $image->getContentType(); $isValid = FALSE; if (isset($mimeTypes)) { $mimeTypes = is_array($mimeTypes) ? $mimeTypes : explode(',', $mimeTypes); $isValid = in_array($contentType, $mimeTypes, TRUE); } if (!$isValid) { throw new ImageStorageException('File must be image, ' . $contentType . ' given'); } } $sanitizedName = $image->getSanitizedName(); $ext = pathinfo($sanitizedName, PATHINFO_EXTENSION); $sanitizedName = pathinfo($sanitizedName, PATHINFO_FILENAME) . '.' . Strings::lower($ext); $file = $this->generateFilePath($sanitizedName, $namespace); $path = $this->getPath($file); $image->move($path); return $file; }
/** * @param \Nette\Http\FileUpload $fileUpload * @return string */ protected function upload(FileUpload $fileUpload) { $fileName = $fileUpload->getSanitizedName(); while (file_exists($this->uploadPath . '/' . $fileName)) { $fileName = Random::generate() . $fileUpload->getSanitizedName(); } $uploadPath = $this->uploadPath . '/' . $fileName; $fileUpload->move($uploadPath); return $uploadPath; }
/** * save file and return path * * @param Nette\Http\FileUpload $image * @return string * @throws \Nette\Utils\UnknownImageFileException */ public function saveImage(Nette\Http\FileUpload $image) { $filename = $image->getSanitizedName(); $path = $this->dir; $filename = $this->getFileName($path, $filename); $image->move("{$path}/{$filename}"); $filePath = "{$this->uri}/{$filename}"; $image = Image::fromFile("{$path}/{$filename}"); $image->resize(150, 150, Image::EXACT); $image->save("{$path}/{$filename}"); return $filePath; }
/** * @param FileUpload $fileUpload * @param string $namespace * @param callable $callback * @return string Absolute name */ public function saveUpload(FileUpload $fileUpload, $namespace = NULL, callable $callback = NULL) { if (!$fileUpload->isOk() || !$fileUpload->isImage()) { return NULL; } $image = $this->createImage(); $image->setNamespace($namespace); $image->setName($fileUpload->getSanitizedName()); if ($callback) { $callback($image); } $image->saveUpload($fileUpload); return (string) $image; }
/** * Uložení nahraného souboru. * @param \Nette\Http\FileUpload $file * @return mixed Vlastní navrátová hodnota. */ public function save(\Nette\Http\FileUpload $file) { return $file->getSanitizedName(); }
/** * @param FileUpload $file * * @return ArrayHash */ private function getFileData(FileUpload $file) { $name = $file->getSanitizedName(); $extension = ''; if (Strings::contains($name, '.')) { $fragments = explode('.', $name); array_shift($fragments); $extension = implode('.', $fragments); } $pairs = $this->storageDao->findPairs([], 'name', [], 'id'); $data = new ArrayHash(); while (true) { $name = Random::generate(10, '0-9A-Za-z'); if (!in_array($name, $pairs)) { $data->name = $name; $data->extension = $extension; break; } } return $data; }
/** * Save uploaded file * * @param FileUpload $file * @param string $path * @return string */ public static function save(FileUpload $file, $path) { if (!$file->isOk()) { return FALSE; } $pathName = self::prepareToSaveFile($file->getSanitizedName(), $path); $file->move($pathName); return basename($pathName); }
/** * @internal * * @ORM\PreFlush() */ public function preFlush() { if ($this->removed) { return; } if ($this->file) { if ($this->file instanceof FileUpload) { $basename = $this->file->getSanitizedName(); $basename = $this->suggestName($basename); $this->setName($basename); } else { $basename = trim(Strings::webalize($this->file->getBasename(), '.', false), '.-'); $basename = $this->suggestName($basename); $this->setName($basename); } if ($this->oldPath && $this->oldPath !== $this->path) { $filePath = $this->getFilePathBy($this->oldProtected, $this->oldPath); if (!is_file($filePath)) { throw new RemoveFileException(sprintf('File \'%s\' does not exist.', $filePath)); } unlink($filePath); if (is_file($filePath)) { throw new RemoveFileException(sprintf('File \'%s\' cannot be removed. Check access rights.', $filePath)); } } if ($this->file instanceof FileUpload) { try { $filePath = $this->getFilePath(); $this->file->move($filePath); } catch (\Nette\InvalidStateException $e) { throw new UploadFileException(); } if (!is_file($filePath)) { throw new RemoveFileException(sprintf('File \'%s\' does not exist.', $filePath)); } } else { $oldFilePath = $this->file->getPathname(); $filePath = $this->getFilePath(); if (!is_file($oldFilePath)) { throw new RenameFileException(sprintf('File \'%s\' does not exist.', $oldFilePath)); } if (is_file($filePath)) { throw new RenameFileException(sprintf('File \'%s\' already exists.', $filePath)); } copy($oldFilePath, $filePath); if (!is_file($filePath)) { throw new RenameFileException(sprintf('File \'%s\' does not exist.', $filePath)); } } $this->size = filesize($this->getFilePath()); $this->mimeType = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->getFilePath()); return $this->file = null; } if (($this->oldPath || $this->oldProtected !== null) && ($this->oldPath !== $this->path || $this->oldProtected !== $this->protected)) { $oldFilePath = $this->getFilePathBy($this->oldProtected !== null ? $this->oldProtected : $this->protected, $this->oldPath !== null ? $this->oldPath : $this->path); $filePath = $this->getFilePath(); if (!is_file($oldFilePath)) { throw new RenameFileException(sprintf('File \'%s\' does not exist.', $oldFilePath)); } if (is_file($filePath)) { throw new RenameFileException(sprintf('File \'%s\' already exists.', $filePath)); } rename($oldFilePath, $filePath); if (is_file($oldFilePath)) { throw new RenameFileException(sprintf('File \'%s\' already exists.', $oldFilePath)); } if (!is_file($filePath)) { throw new RenameFileException(sprintf('File \'%s\' does not exist.', $filePath)); } } }
/** * @param \Nette\Http\FileUpload $file * @param string $filename * @throws IOException * @return string */ public function writeUploaded(Nette\Http\FileUpload $file, $filename = NULL) { if (!$file->isOk()) { throw new IOException("Cannot save corrupted file."); } do { $name = Nette\Utils\Strings::random(10) . '.' . ($filename ?: $file->getSanitizedName()); } while (file_exists($path = $this->dir . DIRECTORY_SEPARATOR . $name)); $file->move($path); return basename($path); }