diff --git a/src/Imaging/GlideManager.php b/src/Imaging/GlideManager.php index e8bc1d84c8..4600044c2e 100644 --- a/src/Imaging/GlideManager.php +++ b/src/Imaging/GlideManager.php @@ -20,7 +20,7 @@ class GlideManager */ public function server(array $config = []) { - return ServerFactory::create(array_merge([ + $server = ServerFactory::create(array_merge([ 'source' => base_path(), // this gets overridden on the fly by the image generator 'cache' => $this->cacheDisk()->getDriver(), 'response' => new LaravelResponseFactory(app('request')), @@ -29,6 +29,29 @@ public function server(array $config = []) 'presets' => Image::manipulationPresets(), 'watermarks' => public_path(), ], $config)); + + if (config('statamic.assets.image_manipulation.append_original_filename', false)) { + $server + ->setCachePathCallable(function ($path, $params) { + // to avoid having to recreate the getCachePath method from glide server + // we run getCachePath again without this callback function + $customCallable = $this->getCachePathCallable(); + + $this->setCachePathCallable(null); + $cachePath = $this->getCachePath($path, $params); + $this->setCachePathCallable($customCallable); + + // then we append our original filename to the end + $filename = Str::afterLast($cachePath, '/'); + $cachePath = Str::beforeLast($cachePath, '/'); + + $cachePath .= '/'.Str::beforeLast($filename, '.').'/'.Str::of($path)->after('/'); + + return $cachePath; + }); + } + + return $server; } public function cacheDisk() diff --git a/src/Imaging/GlideUrlBuilder.php b/src/Imaging/GlideUrlBuilder.php index 4f65693cc6..4b22442003 100644 --- a/src/Imaging/GlideUrlBuilder.php +++ b/src/Imaging/GlideUrlBuilder.php @@ -37,9 +37,19 @@ public function build($item, $params, $filename = null) switch ($this->itemType()) { case 'url': $path = 'http/'.base64_encode($item); + + if (! $filename) { + $filename = $this->optionallySetFilename(Str::afterLast($item, '/')); + } + break; case 'asset': $path = 'asset/'.base64_encode($this->item->containerId().'/'.$this->item->path()); + + if (! $filename) { + $filename = $this->optionallySetFilename(Str::afterLast($this->item->path(), '/')); + } + break; case 'id': $path = 'asset/'.base64_encode(str_replace('::', '/', $this->item)); @@ -64,4 +74,18 @@ public function build($item, $params, $filename = null) return URL::prependSiteRoot($builder->getUrl($path, $params)); } + + /** + * Should the filename be set based on the config setting + * + * @return bool|string + */ + private function optionallySetFilename(string $filename) + { + if (! config('statamic.assets.image_manipulation.append_original_filename', false)) { + return false; + } + + return $filename; + } } diff --git a/tests/Imaging/GlideUrlBuilderTest.php b/tests/Imaging/GlideUrlBuilderTest.php index 7d04d2a6ed..c60955cde8 100644 --- a/tests/Imaging/GlideUrlBuilderTest.php +++ b/tests/Imaging/GlideUrlBuilderTest.php @@ -4,6 +4,7 @@ use Statamic\Assets\Asset; use Statamic\Assets\AssetContainer; +use Statamic\Facades\Config; use Statamic\Imaging\GlideUrlBuilder; use Tests\TestCase; @@ -80,6 +81,22 @@ public function testFilename() ); } + public function testConfigAddsFilename() + { + Config::set('statamic.assets.image_manipulation.append_original_filename', true); + + $asset = new Asset; + $asset->container((new AssetContainer)->handle('main')); + $asset->path('img/foo.jpg'); + + $encoded = base64_encode('main/img/foo.jpg'); + + $this->assertEquals( + "/img/asset/$encoded/foo.jpg?w=100", + $this->builder->build($asset, ['w' => '100']) + ); + } + public function testMarkWithAsset() { $asset = new Asset;