Skip to content

Commit

Permalink
[4.x] Add option to append original filename to Glide URLs (statamic#…
Browse files Browse the repository at this point in the history
…8661)

* Add option to append original filename to Glide URLS

* Prepend filename

* typehits and comments

* Add test

* 🍺

* Change format of image path

* Change true to a boolean

---------

Co-authored-by: Jason Varga <jason@pixelfear.com>
Co-authored-by: Duncan McClean <duncan@duncanmcclean.com>
  • Loading branch information
3 people authored Nov 22, 2023
1 parent 58cdd01 commit 30413a0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Imaging/GlideManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand All @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions src/Imaging/GlideUrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}
}
17 changes: 17 additions & 0 deletions tests/Imaging/GlideUrlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Statamic\Assets\Asset;
use Statamic\Assets\AssetContainer;
use Statamic\Facades\Config;
use Statamic\Imaging\GlideUrlBuilder;
use Tests\TestCase;

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 30413a0

Please sign in to comment.