public function testDoesNotPurgeNotExistingItem()
 {
     $purger = $this->createPurger(array('path/to/file.png'));
     $this->pathGeneratorMock->expects($this->once())->method('getVariationPath')->will($this->returnValue('path/to/file_large.png'));
     $this->ioServiceMock->expects($this->once())->method('exists')->will($this->returnValue(false));
     $this->ioServiceMock->expects($this->never())->method('loadBinaryFile');
     $this->ioServiceMock->expects($this->never())->method('deleteBinaryFile');
     $purger->purge(array('large'));
 }
Exemplo n.º 2
0
 public function find($path)
 {
     try {
         $binaryFile = $this->ioService->loadBinaryFile($path);
     } catch (NotFoundException $e) {
         throw new NotLoadableException("Source image not found in {$path}", 0, $e);
     }
     $mimeType = $binaryFile->mimeType;
     return new Binary($this->ioService->getFileContents($binaryFile), $mimeType, $this->extensionGuesser->guess($mimeType));
 }
Exemplo n.º 3
0
 public function purge(array $aliasNames)
 {
     foreach ($aliasNames as $aliasName) {
         $directory = "_aliases/{$aliasName}";
         $this->io->deleteDirectory($directory);
         if (isset($this->logger)) {
             $this->logger->info("Purging alias directory {$directory}");
         }
     }
 }
 public function testRespondsToIoUri()
 {
     $uri = '/var/test/storage/images/image.png';
     $request = $this->createRequest($uri);
     $event = $this->createEvent($request);
     $binaryFile = new BinaryFile(array('mtime' => new DateTime()));
     $this->ioServiceMock->expects($this->once())->method('loadBinaryFileByUri')->with($uri)->will($this->returnValue($binaryFile));
     $this->eventListener->onKernelRequest($event);
     self::assertTrue($event->hasResponse());
     self::assertEquals(new BinaryStreamResponse($binaryFile, $this->ioServiceMock), $event->getResponse());
 }
 /**
  * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
         return;
     }
     $uri = $event->getRequest()->attributes->get('semanticPathinfo');
     if (!$this->isIoUri($uri)) {
         return;
     }
     // Will throw an API 404 if not found, we can let it pass
     $event->setResponse(new BinaryStreamResponse($this->ioService->loadBinaryFileByUri($uri), $this->ioService));
 }
Exemplo n.º 6
0
 public function find($path)
 {
     try {
         $binaryFile = $this->ioService->loadBinaryFile($path);
         // Treat a MissingBinaryFile as a not loadable file.
         if ($binaryFile instanceof MissingBinaryFile) {
             throw new NotLoadableException("Source image not found in {$path}");
         }
         $mimeType = $this->ioService->getMimeType($path);
         return new Binary($this->ioService->getFileContents($binaryFile), $mimeType, $this->extensionGuesser->guess($mimeType));
     } catch (NotFoundException $e) {
         throw new NotLoadableException("Source image not found in {$path}", 0, $e);
     }
 }
 /**
  * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
         return;
     }
     $uri = $event->getRequest()->attributes->get('semanticPathinfo');
     if (!$this->isIoUri($uri)) {
         return;
     }
     $binaryFile = $this->ioService->loadBinaryFileByUri($uri);
     if ($binaryFile instanceof MissingBinaryFile) {
         throw new NotFoundHttpException("Could not find 'BinaryFile' with identifier '{$uri}'");
     }
     $event->setResponse(new BinaryStreamResponse($binaryFile, $this->ioService));
 }
Exemplo n.º 8
0
 /**
  * Parses the XML from the legacy database.
  *
  * Returns only the data required by the FieldType, nothing more.
  *
  * @param string $xml
  *
  * @return array
  */
 protected function parseLegacyXml($xml)
 {
     $extractedData = array();
     $dom = new \DOMDocument();
     $dom->loadXml($xml);
     $ezimageTag = $dom->documentElement;
     if (!$ezimageTag->hasAttribute('url')) {
         throw new \RuntimeException('Missing attribute "url" in <ezimage/> tag.');
     }
     if (($legacyUrl = $ezimageTag->getAttribute('url')) === '') {
         // Detected XML considered "empty" by the legacy storage
         return null;
     }
     $url = $this->urlRedecorator->redecorateFromTarget($legacyUrl);
     $extractedData['id'] = $this->imageIoService->loadBinaryFileByUri($url)->id;
     if (!$ezimageTag->hasAttribute('filename')) {
         throw new \RuntimeException('Missing attribute "filename" in <ezimage/> tag.');
     }
     $extractedData['fileName'] = $ezimageTag->getAttribute('filename');
     $extractedData['width'] = $ezimageTag->getAttribute('width');
     $extractedData['height'] = $ezimageTag->getAttribute('height');
     $extractedData['mime'] = $ezimageTag->getAttribute('mime_type');
     if (!$ezimageTag->hasAttribute('alternative_text')) {
         throw new \RuntimeException('Missing attribute "alternative_text" in <ezimage/> tag.');
     }
     $extractedData['alternativeText'] = $ezimageTag->getAttribute('alternative_text');
     return $extractedData;
 }
Exemplo n.º 9
0
 /**
  * @param array $fieldIds
  * @param array $context
  *
  * @return boolean
  */
 public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds, array $context)
 {
     /** @var \eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway $gateway */
     $gateway = $this->getGateway($context);
     $fieldXmls = $gateway->getXmlForImages($versionInfo->versionNo, $fieldIds);
     foreach ($fieldXmls as $fieldId => $xml) {
         $storedFiles = $this->extractFiles($xml);
         if ($storedFiles === null) {
             continue;
         }
         if ($this->aliasCleaner) {
             $this->aliasCleaner->removeAliases($this->IOService->getExternalPath($storedFiles['original']));
         }
         foreach ($storedFiles as $storedFilePath) {
             $gateway->removeImageReferences($storedFilePath, $versionInfo->versionNo, $fieldId);
             if ($gateway->countImageReferences($storedFilePath) === 0) {
                 try {
                     $binaryFile = $this->IOService->loadBinaryFile($storedFilePath);
                     $this->IOService->deleteBinaryFile($binaryFile);
                 } catch (NotFoundException $e) {
                     if (isset($this->logger)) {
                         $this->logger->error("Image with id {$storedFilePath} not found");
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 10
0
 public function testNewBinaryCreateStructFromUploadedFile()
 {
     $struct = new BinaryFileCreateStruct();
     $this->publishedIoServiceMock->expects($this->once())->method('newBinaryCreateStructFromUploadedFile')->with(array())->will($this->returnValue($struct));
     $this->draftIoServiceMock->expects($this->never())->method('newBinaryCreateStructFromUploadedFile');
     self::assertEquals($struct, $this->service->newBinaryCreateStructFromUploadedFile(array()));
 }
 /**
  * Purge all variations generated for aliases in $aliasName.
  *
  * @param array $aliasNames
  */
 public function purge(array $aliasNames)
 {
     foreach ($this->imageFileList as $originalImageId) {
         foreach ($aliasNames as $aliasName) {
             $variationImageId = $this->variationPathGenerator->getVariationPath($originalImageId, $aliasName);
             if (!$this->ioService->exists($variationImageId)) {
                 continue;
             }
             $binaryFile = $this->ioService->loadBinaryFile($variationImageId);
             $this->ioService->deleteBinaryFile($binaryFile);
             if (isset($this->logger)) {
                 $this->logger->info("Purging {$aliasName} variation {$variationImageId} for original image {$originalImageId}");
             }
         }
     }
 }
Exemplo n.º 12
0
    /**
     * @param array $fieldIds
     * @param array $context
     *
     * @return boolean
     */
    public function deleteFieldData( VersionInfo $versionInfo, array $fieldIds, array $context )
    {
        /** @var \eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway $gateway */
        $gateway = $this->getGateway( $context );

        $fieldXmls = $gateway->getXmlForImages( $versionInfo->versionNo, $fieldIds );

        foreach ( $fieldXmls as $fieldId => $xml )
        {
            $storedFiles = $gateway->extractFilesFromXml( $xml );
            if ( $storedFiles === null )
            {
                continue;
            }

            if ( $this->aliasCleaner )
            {
                $this->aliasCleaner->removeAliases( $storedFiles['original'] );
            }

            foreach ( $storedFiles as $storedFilePath )
            {
                $gateway->removeImageReferences( $storedFilePath, $versionInfo->versionNo, $fieldId );
                if ( $gateway->countImageReferences( $storedFilePath ) === 0 )
                {
                    $binaryFile = $this->IOService->loadBinaryFileByUri( $storedFilePath );
                    $this->IOService->deleteBinaryFile( $binaryFile );
                }
            }
        }
    }
Exemplo n.º 13
0
 /**
  * @param string[] $paths The paths where the original files are expected to be.
  * @param string[] $filters The imagine filters in effect.
  *
  * @return void
  */
 public function remove(array $paths, array $filters)
 {
     // TODO: $paths may be empty, meaning that all generated images corresponding to $filters need to be removed.
     if (empty($filters)) {
         $filters = array_keys($this->filterConfiguration->all());
     }
     foreach ($paths as $path) {
         foreach ($filters as $filter) {
             $filteredImagePath = $this->getFilePath($path, $filter);
             if (!$this->ioService->exists($filteredImagePath)) {
                 continue;
             }
             $binaryFile = $this->ioService->loadBinaryFile($filteredImagePath);
             $this->ioService->deleteBinaryFile($binaryFile);
         }
     }
 }
 /**
  * Sends the file.
  */
 public function sendContent()
 {
     if (!$this->isSuccessful()) {
         parent::sendContent();
         return;
     }
     if (0 === $this->maxlen) {
         return;
     }
     $out = fopen('php://output', 'wb');
     $in = $this->ioService->getFileInputStream($this->file);
     stream_copy_to_stream($in, $out, $this->maxlen, $this->offset);
     fclose($out);
 }
 /**
  * Deletes all referenced external data
  *
  * @param VersionInfo $versionInfo
  * @param array $fieldIds
  * @param array $context
  *
  * @return boolean
  */
 public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds, array $context)
 {
     if (empty($fieldIds)) {
         return;
     }
     $gateway = $this->getGateway($context);
     $referencedFiles = $gateway->getReferencedFiles($fieldIds, $versionInfo->versionNo);
     $gateway->removeFileReferences($fieldIds, $versionInfo->versionNo);
     $referenceCountMap = $gateway->countFileReferences($referencedFiles);
     foreach ($referenceCountMap as $filePath => $count) {
         if ($count === 0) {
             $binaryFile = $this->IOService->loadBinaryFile($filePath);
             $this->IOService->deleteBinaryFile($binaryFile);
         }
     }
 }
 /**
  * @param string[] $paths The paths where the original files are expected to be.
  * @param string[] $filters The imagine filters in effect.
  */
 public function remove(array $paths, array $filters)
 {
     if (empty($filters)) {
         $filters = array_keys($this->filterConfiguration->all());
     }
     if (empty($paths)) {
         $this->variationPurger->purge($filters);
     }
     foreach ($paths as $path) {
         foreach ($filters as $filter) {
             $filteredImagePath = $this->getFilePath($path, $filter);
             if (!$this->ioService->exists($filteredImagePath)) {
                 continue;
             }
             $binaryFile = $this->ioService->loadBinaryFile($filteredImagePath);
             $this->ioService->deleteBinaryFile($binaryFile);
         }
     }
 }
 /**
  * Deletes all referenced external data
  *
  * @param VersionInfo $versionInfo
  * @param array $fieldIds
  * @param array $context
  *
  * @return boolean
  */
 public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds, array $context)
 {
     if (empty($fieldIds)) {
         return;
     }
     $gateway = $this->getGateway($context);
     $referencedFiles = $gateway->getReferencedFiles($fieldIds, $versionInfo->versionNo);
     $gateway->removeFileReferences($fieldIds, $versionInfo->versionNo);
     $referenceCountMap = $gateway->countFileReferences($referencedFiles);
     foreach ($referenceCountMap as $filePath => $count) {
         if ($count === 0) {
             try {
                 $binaryFile = $this->IOService->loadBinaryFile($filePath);
                 $this->IOService->deleteBinaryFile($binaryFile);
             } catch (NotFoundException $e) {
                 if (isset($this->logger)) {
                     $filePath = $this->IOService->getInternalPath($filePath);
                     $this->logger->error("BinaryFile with ID {$filePath} not found");
                 }
             }
         }
     }
 }
Exemplo n.º 18
0
 public function removeAliases($originalPath)
 {
     $this->innerAliasCleaner->removeAliases($this->ioService->getExternalPath($originalPath));
 }
 /**
  * Deletes the binary file stored in the field.
  *
  * @param $content
  *
  * @return mixed
  */
 protected function deleteStoredFile($content)
 {
     return $this->ioService->deleteBinaryFile($this->ioService->loadBinaryFile($content->fields[1]->value->externalData['id']));
 }
Exemplo n.º 20
0
 public function newBinaryCreateStructFromUploadedFile(array $uploadedFile)
 {
     return $this->publishedIOService->newBinaryCreateStructFromUploadedFile($uploadedFile);
 }
Exemplo n.º 21
0
 public function removeAliases($originalPath)
 {
     $this->innerAliasCleaner->removeAliases($this->ioService->loadBinaryFileByUri($this->urlRedecorator->redecorateFromTarget($originalPath)));
 }