From c2e8988cae547f6f12076fad5f9c36c2229f9dc3 Mon Sep 17 00:00:00 2001 From: Peter Philipp Date: Mon, 10 Jul 2023 16:49:45 +0200 Subject: [PATCH] fix: Load thumbnail paths deferred if viable. --- src/GraphQL/AssetType/AssetType.php | 3 ++- src/GraphQL/FieldHelper/AssetFieldHelper.php | 16 ++++++++++---- src/GraphQL/Resolver/AssetType.php | 22 ++++++-------------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/GraphQL/AssetType/AssetType.php b/src/GraphQL/AssetType/AssetType.php index cd53fd47..034bb6f1 100644 --- a/src/GraphQL/AssetType/AssetType.php +++ b/src/GraphQL/AssetType/AssetType.php @@ -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'], ], diff --git a/src/GraphQL/FieldHelper/AssetFieldHelper.php b/src/GraphQL/FieldHelper/AssetFieldHelper.php index dcb335b1..bbabd2e0 100644 --- a/src/GraphQL/FieldHelper/AssetFieldHelper.php +++ b/src/GraphQL/FieldHelper/AssetFieldHelper.php @@ -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); @@ -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); } } diff --git a/src/GraphQL/Resolver/AssetType.php b/src/GraphQL/Resolver/AssetType.php index 62b8a12f..886cc00f 100644 --- a/src/GraphQL/Resolver/AssetType.php +++ b/src/GraphQL/Resolver/AssetType.php @@ -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);