public function create(BinaryFileCreateStruct $binaryFileCreateStruct) { try { $this->filesystem->writeStream($binaryFileCreateStruct->id, $binaryFileCreateStruct->getInputStream(), array('mimetype' => $binaryFileCreateStruct->mimeType, 'visibility' => AdapterInterface::VISIBILITY_PUBLIC)); } catch (FileExistsException $e) { $this->filesystem->updateStream($binaryFileCreateStruct->id, $binaryFileCreateStruct->getInputStream(), array('mimetype' => $binaryFileCreateStruct->mimeType, 'visibility' => AdapterInterface::VISIBILITY_PUBLIC)); } }
/** * Creates and stores a new BinaryFile based on the BinaryFileCreateStruct $file * * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the target path already exists * * @param BinaryFileCreateStruct $createStruct * * @return BinaryFile The newly created BinaryFile object */ public function create(BinaryFileCreateStruct $createStruct) { if (isset($this->storage[$createStruct->id])) { throw new InvalidArgumentException("\$createFilestruct->id", "file '{$createStruct->id}' already exists"); } $this->data[$createStruct->id] = base64_encode(fread($createStruct->getInputStream(), $createStruct->size)); $binaryFile = new BinaryFile(); $binaryFile->id = $createStruct->id; $binaryFile->mtime = new DateTime(); $binaryFile->size = $createStruct->size; return $this->storage[$binaryFile->id] = $binaryFile; }
public function testCreateOverwritesIfExists() { $stream = fopen('php://memory', 'rb'); $spiBinaryFileCreateStruct = new SPIBinaryFileCreateStruct(); $spiBinaryFileCreateStruct->id = 'prefix/my/file.png'; $spiBinaryFileCreateStruct->mimeType = 'image/png'; $spiBinaryFileCreateStruct->size = 123; $spiBinaryFileCreateStruct->mtime = 1307155200; $spiBinaryFileCreateStruct->setInputStream($stream); $this->filesystem->expects($this->once())->method('writeStream')->with($this->equalTo($spiBinaryFileCreateStruct->id), $this->equalTo($stream), $this->equalTo(array('mimetype' => 'image/png', 'visibility' => 'public')))->will($this->throwException(new FileExistsException('prefix/my/file.png'))); $this->filesystem->expects($this->once())->method('updateStream')->with($this->equalTo($spiBinaryFileCreateStruct->id), $this->equalTo($stream), $this->equalTo(array('mimetype' => 'image/png', 'visibility' => 'public'))); $this->handler->create($spiBinaryFileCreateStruct); }
/** * Creates a BinaryFile object from $localFile * * @throws \Exception When given a non existing / unreadable file * @param string $localFile Path to local file * @param string $repositoryPath The path the file must be stored as * * @return \eZ\Publish\SPI\IO\BinaryFileCreateStruct */ protected function getCreateStructFromLocalFile($localFile, $repositoryPath) { if (!file_exists($localFile) || !is_readable($localFile)) { throw new \Exception("Could not find/read file: {$localFile}"); } $struct = new BinaryFileCreateStruct(); $struct->size = filesize($localFile); $struct->id = $repositoryPath; $struct->mimeType = $this->getMimeTypeFromPath($localFile); $inputStream = fopen($localFile, 'rb'); $struct->setInputStream($inputStream); return $struct; }
/** * Generates SPI BinaryFileCreateStruct object from provided API BinaryFileCreateStruct object * * @param \eZ\Publish\Core\IO\Values\BinaryFileCreateStruct $binaryFileCreateStruct * * @return \eZ\Publish\SPI\IO\BinaryFileCreateStruct */ protected function buildSPIBinaryFileCreateStructObject(BinaryFileCreateStruct $binaryFileCreateStruct) { $spiBinaryCreateStruct = new SPIBinaryFileCreateStruct(); $spiBinaryCreateStruct->id = $this->getPrefixedUri($binaryFileCreateStruct->id); $spiBinaryCreateStruct->size = $binaryFileCreateStruct->size; $spiBinaryCreateStruct->setInputStream($binaryFileCreateStruct->inputStream); $spiBinaryCreateStruct->mimeType = $binaryFileCreateStruct->mimeType; return $spiBinaryCreateStruct; }
/** * Creates and stores a new BinaryFile based on the BinaryFileCreateStruct $file * * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException If the target path already exists * * @param \eZ\Publish\SPI\IO\BinaryFileCreateStruct $createStruct * * @return \eZ\Publish\SPI\IO\BinaryFile The newly created BinaryFile object */ public function create(BinaryFileCreateStruct $createStruct) { if ($this->exists($createStruct->id)) { throw new InvalidArgumentException("\$createFilestruct->id", "file '" . $this->getStoragePath($createStruct->id) . "' already exists"); } $storagePath = $this->getStoragePath($createStruct->id); $scope = $this->getScope($storagePath); $clusterHandler = $this->getClusterHandler(); $this->getLegacyKernel()->runCallback(function () use($createStruct, $storagePath, $scope, $clusterHandler) { $clusterHandler->fileStoreContents($storagePath, fread($createStruct->getInputStream(), $createStruct->size), $scope, $createStruct->mimeType); }, false, false); return $this->load($createStruct->id); }
/** * Creates and stores a new BinaryFile based on the BinaryFileCreateStruct $file * * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException If the target path already exists * @throws \RuntimeException If the directory in $createStruct->spiBinaryFileIdi exists but is a file * * @param \eZ\Publish\SPI\IO\BinaryFileCreateStruct $createStruct * * @return \eZ\Publish\SPI\IO\BinaryFile The newly created BinaryFile object */ public function create(BinaryFileCreateStruct $createStruct) { if ($this->exists($createStruct->id)) { throw new InvalidArgumentException("\$createFilestruct->id", "file '{$createStruct->id}' already exists"); } $storagePath = $this->getStoragePath($createStruct->id); $outputDirectory = dirname($createStruct->id); if (file_exists($outputDirectory) && !is_dir($outputDirectory)) { throw new RuntimeException("Directory {$outputDirectory} exists but is a file"); } if (!file_exists($outputDirectory)) { $this->createFolderStructure($outputDirectory); } $outputStream = fopen($storagePath, 'wb'); stream_copy_to_stream($createStruct->getInputStream(), $outputStream); return $this->load($createStruct->id); }