From ba0dd7cfcbe5e7ac305c56ae90bc33cbdba4987a Mon Sep 17 00:00:00 2001 From: Marco Perberschlager Date: Thu, 24 Oct 2024 10:20:42 +0200 Subject: [PATCH 1/7] Added `deferred` option --- src/GraphQL/AssetType/AssetType.php | 2 ++ src/GraphQL/FieldHelper/AssetFieldHelper.php | 20 +++++++++++++++----- src/GraphQL/Resolver/AssetType.php | 11 +++++++---- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/GraphQL/AssetType/AssetType.php b/src/GraphQL/AssetType/AssetType.php index 5de9d14a..c32ed60d 100644 --- a/src/GraphQL/AssetType/AssetType.php +++ b/src/GraphQL/AssetType/AssetType.php @@ -98,6 +98,7 @@ public function build(&$config) 'args' => [ 'thumbnail' => ['type' => Type::string()], 'format' => ['type' => Type::string()], + 'deferred' => ['type' => Type::boolean(), 'defaultValue' => false], ], 'resolve' => [$resolver, 'resolvePath'], ], @@ -140,6 +141,7 @@ public function build(&$config) 'args' => [ 'thumbnail' => ['type' => Type::nonNull(Type::string())], 'format' => ['type' => Type::string()], + 'deferred' => ['type' => Type::boolean(), 'defaultValue' => false], ], 'resolve' => [$resolver, 'resolveSrcSet'], ], diff --git a/src/GraphQL/FieldHelper/AssetFieldHelper.php b/src/GraphQL/FieldHelper/AssetFieldHelper.php index 020c734f..85d1eed9 100644 --- a/src/GraphQL/FieldHelper/AssetFieldHelper.php +++ b/src/GraphQL/FieldHelper/AssetFieldHelper.php @@ -41,14 +41,19 @@ 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, + bool $deferred = false + ): mixed { $thumb = null; if ($asset instanceof Asset\Document || $asset instanceof Asset\Video) { - $thumb = $asset->getImageThumbnail($thumbNailConfig); + $thumb = $asset->getImageThumbnail($thumbNailConfig, $deferred); } 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 +62,17 @@ 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 + 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 de82cd7c..3f3a20b7 100644 --- a/src/GraphQL/Resolver/AssetType.php +++ b/src/GraphQL/Resolver/AssetType.php @@ -147,13 +147,14 @@ public function resolvePath($value = null, $args = [], $context = [], ResolveInf $asset = $this->getAssetFromValue($value, $context); $thumbNailConfig = $args['thumbnail'] ?? null; $thumbNailFormat = $args['format'] ?? null; + $deferred = $args['deferred'] ?? false; $assetFieldHelper = $this->getGraphQLService()->getAssetFieldHelper(); if (!isset($thumbNailConfig)) { return $asset->getFullPath(); } - return $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat); + return $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat, $deferred); } /** @@ -170,12 +171,13 @@ public function resolveData($value = null, $args = [], $context = [], ResolveInf $asset = $this->getAssetFromValue($value, $context); $thumbNailConfig = $args['thumbnail'] ?? null; $thumbNailFormat = $args['format'] ?? null; + $deferred = $args['deferred'] ?? false; $assetFieldHelper = $this->getGraphQLService()->getAssetFieldHelper(); if (!isset($thumbNailConfig)) { return base64_encode(stream_get_contents($asset->getStream())); } - $thumb = $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat); + $thumb = $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat, $deferred); return $thumb ? base64_encode(stream_get_contents($thumb->getStream())) : base64_encode(stream_get_contents($asset->getStream())); } @@ -194,12 +196,13 @@ public function resolveSrcSet($value = null, $args = [], $context = [], ResolveI $asset = $this->getAssetFromValue($value, $context); $thumbNailConfig = $args['thumbnail'] ?? null; $thumbNailFormat = $args['format'] ?? null; + $deferred = $args['deferred'] ?? null; $assetFieldHelper = $this->getGraphQLService()->getAssetFieldHelper(); if ($asset instanceof Asset\Image) { $mediaQueries = []; - $thumbnail = $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat); - $thumbnailConfig = $asset->getThumbnail($args['thumbnail'])->getConfig(); + $thumbnail = $assetFieldHelper->getAssetThumbnail($asset, $thumbNailConfig, $thumbNailFormat, $deferred); + $thumbnailConfig = $asset->getThumbnail($args['thumbnail'], $deferred)->getConfig(); if ($thumbnailConfig) { foreach ($thumbnailConfig->getMedias() as $key => $val) { $mediaQueries[] = [ From 7db855d1bb589e4735479fbede38a140e62ae429 Mon Sep 17 00:00:00 2001 From: mcop1 Date: Thu, 24 Oct 2024 08:21:59 +0000 Subject: [PATCH 2/7] Apply php-cs-fixer changes --- src/GraphQL/FieldHelper/AssetFieldHelper.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/GraphQL/FieldHelper/AssetFieldHelper.php b/src/GraphQL/FieldHelper/AssetFieldHelper.php index 85d1eed9..93f4e36c 100644 --- a/src/GraphQL/FieldHelper/AssetFieldHelper.php +++ b/src/GraphQL/FieldHelper/AssetFieldHelper.php @@ -46,8 +46,7 @@ public function getImageDocumentThumbnail( string | Image\Thumbnail\Config $thumbNailConfig, string $thumbNailFormat = null, bool $deferred = false - ): mixed - { + ): mixed { $thumb = null; if ($asset instanceof Asset\Document || $asset instanceof Asset\Video) { @@ -67,8 +66,7 @@ public function getAssetThumbnail( string | Image\Thumbnail\Config | Video\Thumbnail\Config $thumbNailConfig, string $thumbNailFormat = null, bool $deferred = false - ): mixed - { + ): mixed { if (($asset instanceof Asset\Video) && (is_string($thumbNailConfig) || $thumbNailConfig instanceof Video\Thumbnail\Config)) { return $this->getVideoThumbnail($asset, $thumbNailConfig, $thumbNailFormat); } else { From b11d3d0d7caaf509d7f1be9972f795df897d0f91 Mon Sep 17 00:00:00 2001 From: Marco Perberschlager Date: Thu, 24 Oct 2024 10:30:13 +0200 Subject: [PATCH 3/7] Fixed argument order --- src/GraphQL/FieldHelper/AssetFieldHelper.php | 23 +++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/GraphQL/FieldHelper/AssetFieldHelper.php b/src/GraphQL/FieldHelper/AssetFieldHelper.php index 85d1eed9..c8579c9f 100644 --- a/src/GraphQL/FieldHelper/AssetFieldHelper.php +++ b/src/GraphQL/FieldHelper/AssetFieldHelper.php @@ -48,14 +48,21 @@ public function getImageDocumentThumbnail( bool $deferred = false ): mixed { - $thumb = null; - - if ($asset instanceof Asset\Document || $asset instanceof Asset\Video) { - $thumb = $asset->getImageThumbnail($thumbNailConfig, $deferred); - } elseif ($asset instanceof Asset\Image) { - $thumb = $asset->getThumbnail($thumbNailConfig, $deferred); - } - if (isset($thumb, $thumbNailFormat) && method_exists($thumb, 'getAsFormat') && !($asset instanceof Asset\Video)) { + match(true) { + $asset instanceof Asset\Document => $thumb = $asset->getImageThumbnail( + $thumbNailConfig, + deferred: $deferred + ), + $asset instanceof Asset\Video => $thumb = $asset->getImageThumbnail($thumbNailConfig), + $asset instanceof Asset\Image => $thumb = $asset->getThumbnail($thumbNailConfig, $deferred), + default => $thumb = null + }; + + if ( + !($asset instanceof Asset\Video) && + isset($thumb, $thumbNailFormat) && + method_exists($thumb, 'getAsFormat') + ) { $thumb = $thumb->getAsFormat($thumbNailFormat); } From c616f122fea34ab4e9ccad2354217c345a888e28 Mon Sep 17 00:00:00 2001 From: Marco Perberschlager Date: Thu, 24 Oct 2024 10:38:30 +0200 Subject: [PATCH 4/7] Fix phpstan/sonar --- src/GraphQL/FieldHelper/AssetFieldHelper.php | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/GraphQL/FieldHelper/AssetFieldHelper.php b/src/GraphQL/FieldHelper/AssetFieldHelper.php index 932e3224..4169859d 100644 --- a/src/GraphQL/FieldHelper/AssetFieldHelper.php +++ b/src/GraphQL/FieldHelper/AssetFieldHelper.php @@ -47,15 +47,19 @@ public function getImageDocumentThumbnail( string $thumbNailFormat = null, bool $deferred = false ): mixed { - match(true) { - $asset instanceof Asset\Document => $thumb = $asset->getImageThumbnail( - $thumbNailConfig, - deferred: $deferred - ), - $asset instanceof Asset\Video => $thumb = $asset->getImageThumbnail($thumbNailConfig), - $asset instanceof Asset\Image => $thumb = $asset->getThumbnail($thumbNailConfig, $deferred), - default => $thumb = null - }; + $thumb = null; + + if($asset instanceof Asset\Document) { + $thumb = $asset->getImageThumbnail($thumbNailConfig, deferred: $deferred); + } + + if($asset instanceof Asset\Video) { + $thumb = $asset->getImageThumbnail($thumbNailConfig); + } + + if($asset instanceof Asset\Image) { + $thumb = $asset->getThumbnail($thumbNailConfig, $deferred); + } if ( !($asset instanceof Asset\Video) && From ea3267fedbf1b27e313cb00ea1aaddd35c3fca4c Mon Sep 17 00:00:00 2001 From: mcop1 Date: Thu, 24 Oct 2024 08:39:00 +0000 Subject: [PATCH 5/7] Apply php-cs-fixer changes --- src/GraphQL/FieldHelper/AssetFieldHelper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/GraphQL/FieldHelper/AssetFieldHelper.php b/src/GraphQL/FieldHelper/AssetFieldHelper.php index 4169859d..6354b63d 100644 --- a/src/GraphQL/FieldHelper/AssetFieldHelper.php +++ b/src/GraphQL/FieldHelper/AssetFieldHelper.php @@ -49,15 +49,15 @@ public function getImageDocumentThumbnail( ): mixed { $thumb = null; - if($asset instanceof Asset\Document) { + if ($asset instanceof Asset\Document) { $thumb = $asset->getImageThumbnail($thumbNailConfig, deferred: $deferred); } - if($asset instanceof Asset\Video) { + if ($asset instanceof Asset\Video) { $thumb = $asset->getImageThumbnail($thumbNailConfig); } - if($asset instanceof Asset\Image) { + if ($asset instanceof Asset\Image) { $thumb = $asset->getThumbnail($thumbNailConfig, $deferred); } From 35021c3be4037c3bf2e15d24f1f8cd64f4473d25 Mon Sep 17 00:00:00 2001 From: Marco Perberschlager Date: Thu, 24 Oct 2024 10:49:40 +0200 Subject: [PATCH 6/7] Added documentation --- doc/10_GraphQL/04_Query/04_Asset_Queries.md | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/doc/10_GraphQL/04_Query/04_Asset_Queries.md b/doc/10_GraphQL/04_Query/04_Asset_Queries.md index 43e717a2..323027b0 100644 --- a/doc/10_GraphQL/04_Query/04_Asset_Queries.md +++ b/doc/10_GraphQL/04_Query/04_Asset_Queries.md @@ -92,3 +92,35 @@ For details see [filtering documentation page](./10_Filtering.md) ## Localization of Queries Queries can be localized For details see the [localization documentation page](./08_Localization.md). +## Thumbnails + +You can request thumbnails for assets using a simple query like this: + +#### Video assets + +You need to specify the name of the video thumbnail configuration you want to use. +Additionally, you can use the `format` parameter to request a specific format. + +```graphql +query { + getAsset(id:353) { + id, + thumbnail:fullpath(thumbnail: "content"), + } +} +``` + +#### Image assets + +You need to specify the name of the video thumbnail configuration you want to use. +Use the `format` parameter to request a specific format. +Specifying the `deferred` parameter will defer the thumbnail generation until it gets requested for the first time. + +```graphql +query { + getAsset(id:289) { + id, + thumbnail:fullpath(thumbnail: "events_header", deferred:true) + } +} +``` \ No newline at end of file From 7726bc3bea3f905c58eb07f69be17f2d206409c2 Mon Sep 17 00:00:00 2001 From: Marco Perberschlager Date: Thu, 24 Oct 2024 10:50:04 +0200 Subject: [PATCH 7/7] Wording --- doc/10_GraphQL/04_Query/04_Asset_Queries.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/10_GraphQL/04_Query/04_Asset_Queries.md b/doc/10_GraphQL/04_Query/04_Asset_Queries.md index 323027b0..69ff1cc5 100644 --- a/doc/10_GraphQL/04_Query/04_Asset_Queries.md +++ b/doc/10_GraphQL/04_Query/04_Asset_Queries.md @@ -96,7 +96,7 @@ Queries can be localized For details see the [localization documentation page](. You can request thumbnails for assets using a simple query like this: -#### Video assets +#### Video assets thumbnails You need to specify the name of the video thumbnail configuration you want to use. Additionally, you can use the `format` parameter to request a specific format. @@ -110,7 +110,7 @@ query { } ``` -#### Image assets +#### Image assets thumbnails You need to specify the name of the video thumbnail configuration you want to use. Use the `format` parameter to request a specific format.