Skip to content

Commit

Permalink
fix: Load thumbnail paths deferred if viable.
Browse files Browse the repository at this point in the history
  • Loading branch information
das-peter committed Jul 10, 2023
1 parent c53b7fc commit c2e8988
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/GraphQL/AssetType/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public function build(&$config)
'type' => Type::string(),
'args' => [
'thumbnail' => ['type' => Type::string()],
'format' => ['type' => Type::string()]
'format' => ['type' => Type::string()],
'deferred' => ['type' => Type::boolean(), 'defaultValue' => true],
],
'resolve' => [$resolver, 'resolvePath'],
],
Expand Down
16 changes: 12 additions & 4 deletions src/GraphQL/FieldHelper/AssetFieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public function getVideoThumbnail(Asset\Video $asset, string | Video\Thumbnail\C
return null;
}

public function getImageDocumentThumbnail(Asset $asset, string | Image\Thumbnail\Config $thumbNailConfig, string $thumbNailFormat = null): mixed
public function getImageDocumentThumbnail(Asset $asset, string | Image\Thumbnail\Config $thumbNailConfig, string $thumbNailFormat = null, $deferred = false): mixed
{
$thumb = null;

if ($asset instanceof Asset\Document || $asset instanceof Asset\Video) {
$thumb = $asset->getImageThumbnail($thumbNailConfig);
} elseif ($asset instanceof Asset\Image) {
$thumb = $asset->getThumbnail($thumbNailConfig, false);
$thumb = $asset->getThumbnail($thumbNailConfig, $deferred);
}
if (isset($thumb, $thumbNailFormat) && method_exists($thumb, 'getAsFormat') && !($asset instanceof Asset\Video)) {
$thumb = $thumb->getAsFormat($thumbNailFormat);
Expand All @@ -57,12 +57,20 @@ public function getImageDocumentThumbnail(Asset $asset, string | Image\Thumbnail
return $thumb;
}

public function getAssetThumbnail(Asset $asset, string | Image\Thumbnail\Config | Video\Thumbnail\Config $thumbNailConfig, string $thumbNailFormat = null): mixed
/**
* @param \Pimcore\Model\Asset $asset
* @param string|\Pimcore\Model\Asset\Image\Thumbnail\Config|\Pimcore\Model\Asset\Video\Thumbnail\Config $thumbNailConfig
* @param string|null $thumbNailFormat
* @param bool $deferred Taken in account _if viable_.
*
* @return mixed
*/
public function getAssetThumbnail(Asset $asset, string | Image\Thumbnail\Config | Video\Thumbnail\Config $thumbNailConfig, string $thumbNailFormat = null, bool $deferred = false): mixed
{
if (($asset instanceof Asset\Video) && (is_string($thumbNailConfig) || $thumbNailConfig instanceof Video\Thumbnail\Config)) {
return $this->getVideoThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
} else {
return $this->getImageDocumentThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
return $this->getImageDocumentThumbnail($asset, $thumbNailConfig, $thumbNailFormat, $deferred);
}
}

Expand Down
22 changes: 6 additions & 16 deletions src/GraphQL/Resolver/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,13 @@ public function resolvePath($value = null, $args = [], $context = [], ResolveInf
if (!isset($thumbNailConfig)) {
return $asset->getFullPath();
}
// @TODO: check if this still possible with the new metohd
// before we had:
/*
* if ($asset instanceof Asset\Image) {
* return isset($args['thumbnail']) ? $asset->getThumbnail($args['thumbnail'], $deferredThumbnail) : $asset->getFullPath();
* }
*/

// get thumbnails with the "deferred" option as we don't need the data itself
// only the URL and the generation of the thumbnail should happen later
// which is done during request the image and could be parallelized from the browser
$deferredThumbnail = false;
if (!$resolveInfo || $resolveInfo->fieldName !== 'data') {
$deferredThumbnail = true;
}

return $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat);
return $assetFieldHelper->getAssetThumbnail(
$asset,
$thumbNailConfig,
$thumbNailFormat,
!empty($args['deferred'])
);
}

return Service::resolveCachedValue($value, $resolveInfo);
Expand Down

0 comments on commit c2e8988

Please sign in to comment.