diff --git a/eZ/Bundle/EzPublishRestBundle/Resources/config/routing.yml b/eZ/Bundle/EzPublishRestBundle/Resources/config/routing.yml index bcb0f38e719..51a90059d64 100644 --- a/eZ/Bundle/EzPublishRestBundle/Resources/config/routing.yml +++ b/eZ/Bundle/EzPublishRestBundle/Resources/config/routing.yml @@ -218,8 +218,7 @@ ezpublish_rest_binaryContent_getImageVariation: _controller: ezpublish_rest.controller.binary_content:getImageVariation methods: [GET] requirements: - imageId: \d+-\d+ - + imageId: \d+-\d+(-\d+)? # Views diff --git a/eZ/Publish/Core/FieldType/Image/ImageStorage.php b/eZ/Publish/Core/FieldType/Image/ImageStorage.php index ffb2e7624d9..45c006bb96a 100644 --- a/eZ/Publish/Core/FieldType/Image/ImageStorage.php +++ b/eZ/Publish/Core/FieldType/Image/ImageStorage.php @@ -94,7 +94,7 @@ public function storeFieldData(VersionInfo $versionInfo, Field $field, array $co ); } - $field->value->externalData['imageId'] = $versionInfo->contentInfo->id . '-' . $field->id; + $field->value->externalData['imageId'] = $this->buildImageId($versionInfo, $field); $field->value->externalData['uri'] = $binaryFile->uri; $field->value->externalData['id'] = $binaryFile->id; $field->value->externalData['mime'] = $this->IOService->getMimeType($binaryFile->id); @@ -131,7 +131,7 @@ public function storeFieldData(VersionInfo $versionInfo, Field $field, array $co public function getFieldData(VersionInfo $versionInfo, Field $field, array $context) { if ($field->value->data !== null) { - $field->value->data['imageId'] = $versionInfo->contentInfo->id . '-' . $field->id; + $field->value->data['imageId'] = $this->buildImageId($versionInfo, $field); $binaryFile = $this->IOService->loadBinaryFile($field->value->data['id']); $field->value->data['id'] = $binaryFile->id; $field->value->data['fileSize'] = $binaryFile->size; @@ -176,4 +176,18 @@ public function getIndexData(VersionInfo $versionInfo, Field $field, array $cont // @todo: Correct? return null; } + + /** + * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo + * @param \eZ\Publish\SPI\Persistence\Content\Field $field + * + * @return string + */ + private function buildImageId(VersionInfo $versionInfo, Field $field) + { + return sprintf( + '%s-%s-%s', + $versionInfo->contentInfo->id, $field->id, $versionInfo->versionNo + ); + } } diff --git a/eZ/Publish/Core/REST/Server/Controller/BinaryContent.php b/eZ/Publish/Core/REST/Server/Controller/BinaryContent.php index f13e483176b..6e99d8ce23c 100644 --- a/eZ/Publish/Core/REST/Server/Controller/BinaryContent.php +++ b/eZ/Publish/Core/REST/Server/Controller/BinaryContent.php @@ -19,6 +19,11 @@ */ class BinaryContent extends RestController { + /** + * @var \eZ\Publish\SPI\Variation\VariationHandler + */ + protected $imageVariationHandler; + /** * Construct controller. * @@ -33,7 +38,10 @@ public function __construct(VariationHandler $imageVariationHandler) * Returns data about the image variation $variationIdentifier of image field $fieldId. * Will generate the alias if it hasn't been generated yet. * - * @param mixed $imageId + * @param mixed $imageId A custom ID that identifies the image field. + * Until v6.9, the format is {contentId}-{fieldId}. + * since v6.9, the format is {contentId}-{fieldId}-{versionNumber}. + * If the version number isn't specified, the default one is used. * @param string $variationIdentifier * * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException @@ -42,13 +50,8 @@ public function __construct(VariationHandler $imageVariationHandler) */ public function getImageVariation($imageId, $variationIdentifier) { - $idArray = explode('-', $imageId); - if (count($idArray) != 2) { - throw new Exceptions\NotFoundException("Invalid image ID {$imageId}"); - } - list($contentId, $fieldId) = $idArray; - - $content = $this->repository->getContentService()->loadContent($contentId); + list($contentId, $fieldId, $versionNumber) = $this->parseImageId($imageId); + $content = $this->repository->getContentService()->loadContent($contentId, null, $versionNumber); $fieldFound = false; /** @var $field \eZ\Publish\API\Repository\Values\Content\Field */ @@ -68,26 +71,42 @@ public function getImageVariation($imageId, $variationIdentifier) throw new Exceptions\NotFoundException("Image file {$field->value->id} doesn't exist"); } - $versionInfo = $this->repository->getContentService()->loadVersionInfo($content->contentInfo); - try { - $variation = $this->imageVariationHandler->getVariation($field, $versionInfo, $variationIdentifier); + $variation = $this->imageVariationHandler->getVariation($field, $content->getVersionInfo(), $variationIdentifier); - if ($content->contentInfo->mainLocationId === null) { + if ($content->contentInfo->mainLocationId === null || $versionNumber !== $content->contentInfo->currentVersionNo) { return $variation; - } else { - return new CachedValue( - $variation, - array('locationId' => $content->contentInfo->mainLocationId) - ); } + + return new CachedValue( + $variation, + array('locationId' => $content->contentInfo->mainLocationId) + ); } catch (InvalidVariationException $e) { throw new Exceptions\NotFoundException("Invalid image variation $variationIdentifier"); } } /** - * @var \eZ\Publish\SPI\Variation\VariationHandler + * Parses an imageId string into contentId, fieldId and versionNumber. + * + * @param string $imageId Either {contentId}-{fieldId} or {contentId}-{fieldId}-{versionNumber} + * + * @return array An array with 3 keys: contentId, fieldId and versionNumber. + * If the versionNumber wasn't set, it is returned as null. + * + * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException If the imageId format is invalid */ - protected $imageVariationHandler; + private function parseImageId($imageId) + { + $idArray = explode('-', $imageId); + + if (count($idArray) == 2) { + return array_merge($idArray, [null]); + } elseif (count($idArray) == 3) { + return $idArray; + } + + throw new Exceptions\NotFoundException("Invalid image ID {$imageId}"); + } }