Example #1
1
 /**
  * @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());
         }
     }
 }
Example #2
0
File: File.php Project: venne/files
 /**
  * @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));
         }
     }
 }