Skip to content

Commit

Permalink
Merge ezsystems#1951 from branch '6.8' into 6.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Dunogier committed Apr 13, 2017
2 parents 90265d8 + faebef9 commit bd310d7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
3 changes: 1 addition & 2 deletions eZ/Bundle/EzPublishRestBundle/Resources/config/routing.yml
Expand Up @@ -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

Expand Down
18 changes: 16 additions & 2 deletions eZ/Publish/Core/FieldType/Image/ImageStorage.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
}
}
57 changes: 38 additions & 19 deletions eZ/Publish/Core/REST/Server/Controller/BinaryContent.php
Expand Up @@ -19,6 +19,11 @@
*/
class BinaryContent extends RestController
{
/**
* @var \eZ\Publish\SPI\Variation\VariationHandler
*/
protected $imageVariationHandler;

/**
* Construct controller.
*
Expand All @@ -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
Expand All @@ -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 */
Expand All @@ -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}");
}
}

0 comments on commit bd310d7

Please sign in to comment.