/** * @covers \eZ\Publish\Core\IO\Handler::update */ public function testUpdate() { $this->urlFopenPrecheck(); $firstPath = 'images/update-before.gif'; $secondPath = 'images/update-after.png'; $struct = $this->getCreateStructFromLocalFile($this->imageInputPath, $firstPath); $binaryFile = $this->IOHandler->create($struct); self::assertTrue($this->IOHandler->exists($firstPath)); self::assertFalse($this->IOHandler->exists($secondPath)); self::assertEquals(md5_file($this->imageInputPath), md5(fread($this->IOHandler->getFileResource($firstPath), $binaryFile->size)), "Failed asserting that file contents was updated\n"); $newFilePath = __DIR__ . DIRECTORY_SEPARATOR . 'ezplogo2.png'; $updateStruct = new BinaryFileUpdateStruct(); $updateStruct->id = $secondPath; $updateStruct->setInputStream(fopen($newFilePath, 'rb')); $updateStruct->size = filesize($newFilePath); $updatedFile = $this->IOHandler->update($firstPath, $updateStruct); self::assertFalse($this->IOHandler->exists($firstPath), "{$firstPath} should not exist"); self::assertTrue($this->IOHandler->exists($secondPath), "{$secondPath} should exist"); self::assertEquals($updateStruct->size, $updatedFile->size); self::assertEquals(md5_file($newFilePath), md5(fread($this->IOHandler->getFileResource($secondPath), $updatedFile->size))); }
/** * Updates the file identified by $path with data from $updateFile * * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException If the source path doesn't exist * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException If the target path already exists * * @param string $spiBinaryFileId * @param \eZ\Publish\SPI\IO\BinaryFileUpdateStruct $updateFileStruct * * @return \eZ\Publish\SPI\IO\BinaryFile The updated BinaryFile */ public function update($spiBinaryFileId, BinaryFileUpdateStruct $updateFileStruct) { if (!$this->exists($spiBinaryFileId)) { throw new NotFoundException('BinaryFile', $spiBinaryFileId); } $sourceStoragePath = $this->getStoragePath($spiBinaryFileId); if (!isset($updateFileStruct->id) || $updateFileStruct->id == $spiBinaryFileId) { $returnSpiBinaryFileId = $spiBinaryFileId; $destinationStoragePath = $this->getStoragePath($spiBinaryFileId); } else { if ($this->exists($updateFileStruct->id)) { throw new InvalidArgumentException("\$updateFileStruct->id", "File '{$updateFileStruct->id}' already exists"); } $returnSpiBinaryFileId = $updateFileStruct->id; $destinationStoragePath = $this->getStoragePath($returnSpiBinaryFileId); } // contents if ($updateFileStruct->getInputStream() !== null) { $outputStream = fopen($sourceStoragePath, 'wb'); stream_copy_to_stream($updateFileStruct->getInputStream(), $outputStream); fclose($outputStream); } // path if ($destinationStoragePath != $sourceStoragePath) { $destinationStorageDirectory = dirname($destinationStoragePath); if (!file_exists($destinationStorageDirectory)) { $this->createFolderStructure($destinationStorageDirectory); } rename($sourceStoragePath, $destinationStoragePath); } clearstatcache(true, $sourceStoragePath); clearstatcache(true, $destinationStoragePath); return $this->load($returnSpiBinaryFileId); }
/** * Updates the file identified by $path with data from $updateFile * * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException If the source path doesn't exist * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException If the target path already exists * * @param string $spiBinaryFileId * @param \eZ\Publish\SPI\IO\BinaryFileUpdateStruct $updateFileStruct * * @return \eZ\Publish\SPI\IO\BinaryFile The updated BinaryFile */ public function update($spiBinaryFileId, BinaryFileUpdateStruct $updateFileStruct) { if (!$this->exists($spiBinaryFileId)) { throw new NotFoundException('BinaryFile', $spiBinaryFileId); } $destinationPath = $updateFileStruct->id; if (isset($updateFileStruct->id) && $updateFileStruct->id !== $spiBinaryFileId) { if ($this->exists($updateFileStruct->id)) { throw new InvalidArgumentException("\$updateFileStruct->id", "Destination ID '" . $this->getStoragePath($updateFileStruct->id) . "' already exists"); } $updateFileStruct->id = $this->getStoragePath($updateFileStruct->id); } $storagePath = $this->getStoragePath($spiBinaryFileId); $clusterHandler = $this->getClusterHandler(); $this->getLegacyKernel()->runCallback(function () use($storagePath, $updateFileStruct, $clusterHandler) { // path if ($updateFileStruct->id !== null && $updateFileStruct->id != $storagePath) { $clusterHandler->fileMove($storagePath, $updateFileStruct->id); $storagePath = $updateFileStruct->id; } $resource = $updateFileStruct->getInputStream(); if ($resource !== null) { $binaryUpdateData = fread($resource, $updateFileStruct->size); $clusterFile = eZClusterFileHandler::instance($storagePath); $metaData = $clusterFile->metaData; $scope = isset($metaData['scope']) ? $metaData['scope'] : false; $dataType = isset($metaData['datatype']) ? $metaData['datatype'] : false; $clusterFile->storeContents($binaryUpdateData, $scope, $dataType); // Unfortunately required due to insufficient handling of in memory cache in eZDFSFileHandler... if (isset($GLOBALS['eZClusterInfo'][$storagePath])) { unset($GLOBALS['eZClusterInfo'][$storagePath]); } } }, false, false); return $this->load($destinationPath); }
/** * Updates the file identified by $path with data from $updateFile * * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the source path doesn't exist * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the target path already exists * * @param string $path * @param \eZ\Publish\Core\IO\Values\BinaryFileUpdateStruct $updateFileStruct * * @return \eZ\Publish\Core\IO\Values\BinaryFile The updated BinaryFile */ public function update($id, BinaryFileUpdateStruct $updateFileStruct) { if (!isset($this->storage[$id])) { throw new NotFoundException('BinaryFile', $id); } // path if ($updateFileStruct->id !== null && $updateFileStruct->id != $id) { if (isset($this->storage[$updateFileStruct->id])) { throw new InvalidArgumentException("\$updateFileStruct->id", "file '{$updateFileStruct->id}' already exists"); } $oldId = $id; $newId = $updateFileStruct->id; $this->storage[$newId] = $this->storage[$oldId]; $this->data[$newId] = $this->data[$oldId]; $this->storage[$newId]->id = $newId; unset($this->storage[$oldId]); unset($this->data[$oldId]); $id = $newId; } $resource = $updateFileStruct->getInputStream(); if ($resource !== null) { $this->data[$id] = base64_encode(fread($resource, $updateFileStruct->size)); if ($updateFileStruct->size !== null) { $this->storage[$id]->size = $updateFileStruct->size; } } if ($updateFileStruct->mtime !== null && $updateFileStruct->mtime !== $this->storage[$id]->mtime) { $this->storage[$id]->mtime = $updateFileStruct->mtime; } return $this->storage[$id]; }