Skip to content

Commit 14cd46b

Browse files
[5.x] Ensure Glide treats asset urls starting with the app url as internal assets (statamic#11839)
Co-authored-by: Duncan McClean <duncan@duncanmcclean.com>
1 parent a084bc1 commit 14cd46b

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

src/Assets/AssetContainer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use Statamic\Facades\Stache;
2828
use Statamic\Facades\URL;
2929
use Statamic\Support\Arr;
30-
use Statamic\Support\Str;
3130
use Statamic\Support\Traits\FluentlyGetsAndSets;
3231

3332
class AssetContainer implements Arrayable, ArrayAccess, AssetContainerContract, Augmentable
@@ -139,9 +138,7 @@ public function url()
139138
return null;
140139
}
141140

142-
$url = (string) Str::of($this->disk()->url('/'))
143-
->rtrim('/')
144-
->after(config('app.url'));
141+
$url = rtrim($this->disk()->url('/'), '/');
145142

146143
return ($url === '') ? '/' : $url;
147144
}

src/Tags/Glide.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ private function normalizeItem($item)
279279
return $item;
280280
}
281281

282+
if (Str::startsWith($item, config('app.url'))) {
283+
$item = Str::after($item, config('app.url'));
284+
}
285+
282286
// External URLs are already fine as-is.
283287
if (Str::startsWith($item, ['http://', 'https://'])) {
284288
return $item;

tests/Assets/AssetContainerTest.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,6 @@ public function it_gets_the_url_from_the_disk_config_when_its_relative()
137137
$this->assertEquals('http://localhost/container', $container->absoluteUrl());
138138
}
139139

140-
#[Test]
141-
public function it_gets_the_url_from_the_disk_config_when_its_app_url()
142-
{
143-
config(['filesystems.disks.test' => [
144-
'driver' => 'local',
145-
'root' => __DIR__.'/__fixtures__/container',
146-
'url' => 'http://localhost/container',
147-
]]);
148-
149-
$container = (new AssetContainer)->disk('test');
150-
151-
$this->assertEquals('/container', $container->url());
152-
$this->assertEquals('http://localhost/container', $container->absoluteUrl());
153-
}
154-
155140
#[Test]
156141
public function its_private_if_the_disk_has_no_url()
157142
{

tests/Tags/GlideTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ public function it_outputs_a_data_url()
6767
$this->assertStringStartsWith('data:image/jpeg;base64', (string) Parse::template($tag, ['foo' => 'bar.jpg']));
6868
}
6969

70+
#[Test]
71+
#[DefineEnvironment('absoluteHttpRouteUrlWithoutCache')]
72+
/**
73+
* https://github.com/statamic/cms/pull/11839
74+
*/
75+
public function it_treats_assets_urls_starting_with_the_app_url_as_internal_assets()
76+
{
77+
$this->createImageInPublicDirectory();
78+
79+
$result = (string) Parse::template('{{ glide:foo width="100" }}', ['foo' => 'http://localhost/glide/bar.jpg']);
80+
81+
$this->assertStringStartsWith('/img/glide/bar.jpg', $result);
82+
}
83+
7084
public function relativeRouteUrl($app)
7185
{
7286
$this->configureGlideCacheDiskWithUrl($app, '/glide');
@@ -77,20 +91,25 @@ public function absoluteHttpRouteUrl($app)
7791
$this->configureGlideCacheDiskWithUrl($app, 'http://localhost/glide');
7892
}
7993

94+
public function absoluteHttpRouteUrlWithoutCache($app)
95+
{
96+
$this->configureGlideCacheDiskWithUrl($app, 'http://localhost/glide', false);
97+
}
98+
8099
public function absoluteHttpsRouteUrl($app)
81100
{
82101
$this->configureGlideCacheDiskWithUrl($app, 'https://localhost/glide');
83102
}
84103

85-
private function configureGlideCacheDiskWithUrl($app, $url)
104+
private function configureGlideCacheDiskWithUrl($app, $url, $cache = 'glide')
86105
{
87106
$app['config']->set('filesystems.disks.glide', [
88107
'driver' => 'local',
89108
'root' => public_path('glide'),
90109
'url' => $url,
91110
'visibility' => 'public',
92111
]);
93-
$app['config']->set('statamic.assets.image_manipulation.cache', 'glide');
112+
$app['config']->set('statamic.assets.image_manipulation.cache', $cache);
94113
}
95114

96115
private function createImageInPublicDirectory()

0 commit comments

Comments
 (0)