Ejemplo n.º 1
0
 /**
  * Previously in \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::func_copy()
  * copies a source file (from any location) in to the target
  * folder, the latter has to be part of this storage
  *
  * @param FileInterface $file
  * @param Folder $targetFolder
  * @param string $targetFileName an optional destination fileName
  * @param string $conflictMode a value of the DuplicationBehavior enumeration
  *
  * @throws \Exception|Exception\AbstractFileOperationException
  * @throws Exception\ExistingTargetFileNameException
  * @return FileInterface
  */
 public function copyFile(FileInterface $file, Folder $targetFolder, $targetFileName = null, $conflictMode = DuplicationBehavior::RENAME)
 {
     $conflictMode = DuplicationBehavior::cast($conflictMode);
     if ($targetFileName === null) {
         $targetFileName = $file->getName();
     }
     $sanitizedTargetFileName = $this->driver->sanitizeFileName($targetFileName);
     $this->assureFileCopyPermissions($file, $targetFolder, $sanitizedTargetFileName);
     $this->emitPreFileCopySignal($file, $targetFolder);
     // File exists and we should abort, let's abort
     if ($conflictMode->equals(DuplicationBehavior::CANCEL) && $targetFolder->hasFile($sanitizedTargetFileName)) {
         throw new Exception\ExistingTargetFileNameException('The target file already exists.', 1320291064);
     }
     // File exists and we should find another name, let's find another one
     if ($conflictMode->equals(DuplicationBehavior::RENAME) && $targetFolder->hasFile($sanitizedTargetFileName)) {
         $sanitizedTargetFileName = $this->getUniqueName($targetFolder, $sanitizedTargetFileName);
     }
     $sourceStorage = $file->getStorage();
     // Call driver method to create a new file from an existing file object,
     // and return the new file object
     if ($sourceStorage === $this) {
         $newFileObjectIdentifier = $this->driver->copyFileWithinStorage($file->getIdentifier(), $targetFolder->getIdentifier(), $sanitizedTargetFileName);
     } else {
         $tempPath = $file->getForLocalProcessing();
         $newFileObjectIdentifier = $this->driver->addFile($tempPath, $targetFolder->getIdentifier(), $sanitizedTargetFileName);
     }
     $newFileObject = ResourceFactory::getInstance()->getFileObjectByStorageAndIdentifier($this->getUid(), $newFileObjectIdentifier);
     $this->emitPostFileCopySignal($file, $targetFolder);
     return $newFileObject;
 }