-
-
Notifications
You must be signed in to change notification settings - Fork 630
[6.x] Hybrid Glide image caching #14570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
duncanmcclean
wants to merge
8
commits into
6.x
Choose a base branch
from
glide-half-measure-caching
base: 6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2e3d8fa
wip
duncanmcclean 4408ec7
remove unneccessary comments in test
duncanmcclean 05afbd9
fix failing test on windows 🤞
duncanmcclean 697c950
call it `hybrid` instead
duncanmcclean bf202d6
remove query parameters from url to avoid seo issues
duncanmcclean bd2be92
formatting
duncanmcclean a448b59
Merge branch '6.x' into glide-half-measure-caching
duncanmcclean ea457e9
Fix hybrid URL builder mangling asset ID strings
duncanmcclean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| <?php | ||
|
|
||
| namespace Statamic\Imaging; | ||
|
|
||
| use League\Glide\Server; | ||
| use Statamic\Contracts\Assets\Asset; | ||
| use Statamic\Facades\Asset as Assets; | ||
| use Statamic\Facades\URL; | ||
| use Statamic\Support\Str; | ||
|
|
||
| class GlideCachePathResolver | ||
| { | ||
| public function __construct(private Server $server) | ||
| { | ||
| } | ||
|
|
||
| public function resolveForAsset(Asset $asset, array $params): string | ||
| { | ||
| return $this->resolve( | ||
| $asset->basename(), | ||
| $params, | ||
| sourcePathPrefix: $asset->folder(), | ||
| cachePathPrefix: ImageGenerator::assetCachePathPrefix($asset).'/'.$asset->folder(), | ||
| asset: $asset, | ||
| ); | ||
| } | ||
|
|
||
| public function resolveForPath(string $path, array $params): string | ||
| { | ||
| return $this->resolve( | ||
| $path, | ||
| $params, | ||
| sourcePathPrefix: '/', | ||
| cachePathPrefix: 'paths', | ||
| ); | ||
| } | ||
|
|
||
| public function resolveForUrl(string $url, array $params): string | ||
| { | ||
| $parsed = app(RemoteUrlValidator::class)->parse($url); | ||
| $qs = $parsed['query']; | ||
| $path = $parsed['path'].($qs ? '?'.$qs : ''); | ||
|
|
||
| return $this->resolve( | ||
| $path, | ||
| $params, | ||
| sourcePathPrefix: '/', | ||
| cachePathPrefix: 'http', | ||
| ); | ||
| } | ||
|
|
||
| public function resolveForItem($item, array $params): string | ||
| { | ||
| if ($item instanceof Asset) { | ||
| return $this->resolveForAsset($item, $params); | ||
| } | ||
|
|
||
| if (is_string($item) && Str::contains($item, '::')) { | ||
| $asset = Assets::find($item); | ||
|
|
||
| if ($asset) { | ||
| return $this->resolveForAsset($asset, $params); | ||
| } | ||
| } | ||
|
|
||
| if (is_string($item) && URL::isAbsolute($item)) { | ||
| return $this->resolveForUrl($item, $params); | ||
| } | ||
|
|
||
| return $this->resolveForPath($item, $params); | ||
| } | ||
|
|
||
| private function resolve(string $image, array $params, string $sourcePathPrefix, string $cachePathPrefix, ?Asset $asset = null): string | ||
| { | ||
| $origSourcePrefix = $this->server->getSourcePathPrefix(); | ||
| $origCachePrefix = $this->server->getCachePathPrefix(); | ||
| $origDefaults = $this->server->getDefaults(); | ||
|
|
||
| $this->server->setSourcePathPrefix($sourcePathPrefix); | ||
| $this->server->setCachePathPrefix($cachePathPrefix); | ||
| $this->server->setDefaults(ImageGenerator::getDefaultManipulations($asset)); | ||
|
|
||
| try { | ||
| return $this->server->getCachePath($image, $params); | ||
| } finally { | ||
| $this->server->setSourcePathPrefix($origSourcePrefix); | ||
| $this->server->setCachePathPrefix($origCachePrefix); | ||
| $this->server->setDefaults($origDefaults); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?php | ||
|
|
||
| namespace Statamic\Imaging; | ||
|
|
||
| use Exception; | ||
| use Statamic\Contracts\Assets\Asset; | ||
| use Statamic\Facades\Asset as Assets; | ||
| use Statamic\Facades\Glide; | ||
| use Statamic\Facades\URL; | ||
| use Statamic\Support\Str; | ||
|
|
||
| class HybridUrlBuilder extends ImageUrlBuilder | ||
| { | ||
| protected GlideCachePathResolver $resolver; | ||
|
|
||
| protected array $options; | ||
|
|
||
| public function __construct(GlideCachePathResolver $resolver, array $options = []) | ||
| { | ||
| $this->resolver = $resolver; | ||
| $this->options = $options; | ||
| } | ||
|
|
||
| /** | ||
| * Build the URL. | ||
| * | ||
| * @param \Statamic\Contracts\Assets\Asset|string $item | ||
| * @param array $params | ||
| * @return string | ||
| * | ||
| * @throws \Exception | ||
| */ | ||
| public function build($item, $params) | ||
| { | ||
| $this->item = $item; | ||
|
|
||
| if (isset($params['mark']) && $params['mark'] instanceof Asset) { | ||
| $asset = $params['mark']; | ||
| $params['mark'] = 'asset::'.Str::toBase64Url($asset->containerId().'/'.$asset->path()); | ||
| } | ||
|
|
||
| $cachePath = $this->resolver->resolveForItem($item, $params); | ||
|
|
||
| $this->cacheSource($cachePath, $params); | ||
|
|
||
| $urlPath = URL::tidy($this->options['route'].'/'.$cachePath, withTrailingSlash: false); | ||
|
|
||
| return URL::makeRelative(URL::prependSiteUrl($urlPath)); | ||
| } | ||
|
|
||
| private function cacheSource(string $cachePath, array $params): void | ||
| { | ||
| $mapping = match ($this->itemType()) { | ||
| 'asset' => ['type' => 'asset', 'id' => $this->item->id(), 'params' => $params], | ||
| 'url' => ['type' => 'url', 'url' => $this->item, 'params' => $params], | ||
| 'id' => ['type' => 'asset', 'id' => str_replace('/', '::', $this->item), 'params' => $params], | ||
| 'path' => ['type' => 'path', 'path' => $this->item, 'params' => $params], | ||
| default => throw new Exception('Cannot build a hybrid Glide URL without a URL, path, or asset.'), | ||
| }; | ||
|
|
||
| $mappingKey = 'hybrid::'.$cachePath; | ||
|
|
||
| Glide::cacheStore()->forever($mappingKey, $mapping); | ||
|
|
||
| // Add to the asset manifest so clearAsset() cleans up the mapping too. | ||
| if ($mapping['type'] === 'asset') { | ||
| $manifestKey = ImageGenerator::assetCacheManifestKey( | ||
| Assets::find($mapping['id']) | ||
| ); | ||
|
|
||
| $manifest = Glide::cacheStore()->get($manifestKey, []); | ||
| $manifest[] = $mappingKey; | ||
| Glide::cacheStore()->forever($manifestKey, array_unique($manifest)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.