Skip to content

Commit 0791516

Browse files
committed
File: Differentiate between mediaRoot/mediaPath
1 parent 3595efb commit 0791516

File tree

7 files changed

+107
-16
lines changed

7 files changed

+107
-16
lines changed

config/components.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
}
7777

7878
// create url and root
79-
$mediaRoot = dirname($file->mediaRoot());
79+
$mediaRoot = $file->mediaRoot();
8080
$template = $mediaRoot . '/{{ name }}{{ attributes }}.{{ extension }}';
8181
$thumbRoot = (new Filename($file->root(), $template, $options))->toString();
8282
$thumbName = basename($thumbRoot);
@@ -102,7 +102,7 @@
102102
'modifications' => $options,
103103
'original' => $file,
104104
'root' => $thumbRoot,
105-
'url' => dirname($file->mediaUrl()) . '/' . $thumbName,
105+
'url' => $file->mediaUrl($thumbName),
106106
]);
107107
},
108108

src/Cms/File.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,23 @@ public function mediaHash(): string
383383
/**
384384
* Returns the absolute path to the file in the public media folder
385385
* @internal
386+
*
387+
* @param string|null $filename Optional override for the filename
388+
*/
389+
public function mediaPath(string|null $filename = null): string
390+
{
391+
$filename ??= $this->filename();
392+
393+
return $this->mediaRoot() . '/' . $filename;
394+
}
395+
396+
/**
397+
* Returns the absolute path to the media folder for the file and its versions
398+
* @internal
386399
*/
387400
public function mediaRoot(): string
388401
{
389-
return $this->parent()->mediaRoot() . '/' . $this->mediaHash() . '/' . $this->filename();
402+
return $this->parent()->mediaRoot() . '/' . $this->mediaHash();
390403
}
391404

392405
/**
@@ -402,10 +415,15 @@ public function mediaToken(): string
402415
/**
403416
* Returns the absolute Url to the file in the public media folder
404417
* @internal
418+
*
419+
* @param string|null $filename Optional override for the filename
405420
*/
406-
public function mediaUrl(): string
421+
public function mediaUrl(string|null $filename = null): string
407422
{
408-
return $this->parent()->mediaUrl() . '/' . $this->mediaHash() . '/' . $this->filename();
423+
$url = $this->parent()->mediaUrl() . '/' . $this->mediaHash();
424+
$filename ??= $this->filename();
425+
426+
return $url . '/' . $filename;
409427
}
410428

411429
/**

src/Cms/FileActions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ protected static function normalizeProps(array $props): array
362362
*/
363363
public function publish(): static
364364
{
365-
Media::publish($this, $this->mediaRoot());
365+
Media::publish($this, $this->mediaPath());
366366
return $this;
367367
}
368368

src/Cms/Media.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function link(
5353
}
5454

5555
// send the file to the browser
56-
return Response::file($file->publish()->mediaRoot());
56+
return Response::file($file->publish()->mediaPath());
5757
}
5858

5959
// try to generate a thumb for the file
@@ -102,7 +102,7 @@ public static function thumb(
102102
=> $kirby->root('media') . '/assets/' . $model . '/' . $hash,
103103
// parent files for file model that already included hash
104104
$model instanceof File
105-
=> dirname($model->mediaRoot()),
105+
=> $model->mediaRoot(),
106106
// model files
107107
default
108108
=> $model->mediaRoot() . '/' . $hash

tests/Cms/File/FileMediaTest.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public function setUp(): void
1515
parent::setUp();
1616

1717
$this->app = $this->app->clone([
18+
'roots' => [
19+
'index' => self::TMP
20+
],
1821
'options' => [
1922
'content.salt' => 'test'
2023
]
@@ -23,8 +26,8 @@ public function setUp(): void
2326

2427
public function testMediaHash(): void
2528
{
26-
F::write($file = static::TMP . '/content/test.jpg', 'test');
27-
touch($file, 5432112345);
29+
F::write($root = static::TMP . '/content/test.jpg', 'test');
30+
touch($root, 5432112345);
2831

2932
$file = new File([
3033
'filename' => 'test.jpg',
@@ -34,6 +37,33 @@ public function testMediaHash(): void
3437
$this->assertSame('08756f3115-5432112345', $file->mediaHash());
3538
}
3639

40+
public function testMediaPath(): void
41+
{
42+
F::write($root = static::TMP . '/content/test.jpg', 'test');
43+
touch($root, 5432112345);
44+
45+
$file = new File([
46+
'filename' => 'test.jpg',
47+
'parent' => $this->app->site()
48+
]);
49+
50+
$this->assertSame(self::TMP . '/media/site/08756f3115-5432112345/test.jpg', $file->mediaPath());
51+
$this->assertSame(self::TMP . '/media/site/08756f3115-5432112345/test-120x.jpg', $file->mediaPath('test-120x.jpg'));
52+
}
53+
54+
public function testMediaRoot(): void
55+
{
56+
F::write($root = static::TMP . '/content/test.jpg', 'test');
57+
touch($root, 5432112345);
58+
59+
$file = new File([
60+
'filename' => 'test.jpg',
61+
'parent' => $this->app->site()
62+
]);
63+
64+
$this->assertSame(self::TMP . '/media/site/08756f3115-5432112345', $file->mediaRoot());
65+
}
66+
3767
public function testMediaToken(): void
3868
{
3969
$file = new File([
@@ -43,4 +73,18 @@ public function testMediaToken(): void
4373

4474
$this->assertSame('08756f3115', $file->mediaToken());
4575
}
76+
77+
public function testMediaUrl(): void
78+
{
79+
F::write($root = static::TMP . '/content/test.jpg', 'test');
80+
touch($root, 5432112345);
81+
82+
$file = new File([
83+
'filename' => 'test.jpg',
84+
'parent' => $this->app->site()
85+
]);
86+
87+
$this->assertSame('/media/site/08756f3115-5432112345/test.jpg', $file->mediaUrl());
88+
$this->assertSame('/media/site/08756f3115-5432112345/test-120x.jpg', $file->mediaUrl('test-120x.jpg'));
89+
}
4690
}

tests/Cms/File/FileUrlTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,29 @@
22

33
namespace Kirby\Cms;
44

5+
use Kirby\Filesystem\F;
56
use PHPUnit\Framework\Attributes\CoversClass;
67

78
#[CoversClass(File::class)]
89
class FileUrlTest extends ModelTestCase
910
{
11+
public const FIXTURES = __DIR__ . '/fixtures/files';
1012
public const TMP = KIRBY_TMP_DIR . '/Cms.FileUrl';
1113

14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$this->app = $this->app->clone([
19+
'roots' => [
20+
'index' => self::TMP
21+
],
22+
'options' => [
23+
'content.salt' => 'test'
24+
]
25+
]);
26+
}
27+
1228
public function testPermalink(): void
1329
{
1430
$page = new Page(['slug' => 'test']);
@@ -21,7 +37,7 @@ public function testPermalink(): void
2137
$this->assertSame('//@/file/my-file-uuid', $file->permalink());
2238
}
2339

24-
public function testUrl(): void
40+
public function testUrlFixed(): void
2541
{
2642
$file = new File([
2743
'filename' => 'test.pdf',
@@ -31,4 +47,17 @@ public function testUrl(): void
3147

3248
$this->assertSame($url, $file->url());
3349
}
50+
51+
public function testUrlMedia(): void
52+
{
53+
F::copy(self::FIXTURES . '/test.pdf', $root = self::TMP . '/content/test.pdf');
54+
touch($root, 1234567890);
55+
56+
$file = new File([
57+
'filename' => 'test.pdf',
58+
'parent' => $this->app->site()
59+
]);
60+
61+
$this->assertSame('/media/site/b22a2d4f82-1234567890/test.pdf', $file->url());
62+
}
3463
}

tests/Cms/Media/MediaTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,15 @@ public function testThumb()
190190

191191
// get file object
192192
$file = $this->app->file('test.jpg');
193-
Dir::make(dirname($file->mediaRoot()));
193+
Dir::make($file->mediaRoot());
194194
$this->assertIsFile($file);
195195

196196
// create job file
197197
$jobString = '{"width":64,"height":64,"quality":null,"crop":"center","filename":"test.jpg"}';
198-
F::write(dirname($file->mediaRoot()) . '/.jobs/' . $file->filename() . '.json', $jobString);
198+
F::write($file->mediaRoot() . '/.jobs/' . $file->filename() . '.json', $jobString);
199199

200200
// copy to media folder
201-
$file->asset()->copy($mediaPath = $file->mediaRoot());
201+
$file->asset()->copy($mediaPath = $file->mediaPath());
202202

203203
$thumb = Media::thumb($file, $file->mediaHash(), $file->filename());
204204
$this->assertInstanceOf(Response::class, $thumb);
@@ -240,7 +240,7 @@ public function testThumbWithIncompleteJobFile()
240240
$file = $this->app->file('test.jpg');
241241

242242
// create an empty job file
243-
F::write(dirname($file->mediaRoot()) . '/.jobs/' . $file->filename() . '.json', '{}');
243+
F::write($file->mediaRoot() . '/.jobs/' . $file->filename() . '.json', '{}');
244244

245245
$this->expectException(\Kirby\Exception\InvalidArgumentException::class);
246246
$this->expectExceptionMessage('Incomplete thumbnail configuration');
@@ -265,7 +265,7 @@ public function testThumbWhenGenerationFails()
265265

266266
// create a valid job file
267267
$jobString = '{"width":64,"height":64,"quality":null,"crop":"center","filename":"test.jpg"}';
268-
F::write(dirname($file->mediaRoot()) . '/.jobs/' . $file->filename() . '.json', $jobString);
268+
F::write($file->mediaRoot() . '/.jobs/' . $file->filename() . '.json', $jobString);
269269

270270
$this->expectException(\Exception::class);
271271
$this->expectExceptionMessage('File not found');

0 commit comments

Comments
 (0)