Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/components.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
}

// create url and root
$mediaRoot = dirname($file->mediaRoot());
$mediaRoot = $file->mediaDir();
$template = $mediaRoot . '/{{ name }}{{ attributes }}.{{ extension }}';
$thumbRoot = (new Filename($file->root(), $template, $options))->toString();
$thumbName = basename($thumbRoot);
Expand All @@ -102,7 +102,7 @@
'modifications' => $options,
'original' => $file,
'root' => $thumbRoot,
'url' => dirname($file->mediaUrl()) . '/' . $thumbName,
'url' => $file->mediaUrl($thumbName),
]);
},

Expand Down
27 changes: 23 additions & 4 deletions src/Cms/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,16 @@ public function isReadable(): bool
return $readable[$role][$template] ??= $this->permissions()->can('read');
}

/**
* Returns the absolute path to the media folder for the file and its versions
* @internal
* @since 5.0.0
*/
public function mediaDir(): string
{
return $this->parent()->mediaDir() . '/' . $this->mediaHash();
}

/**
* Creates a unique media hash
* @internal
Expand All @@ -383,10 +393,14 @@ public function mediaHash(): string
/**
* Returns the absolute path to the file in the public media folder
* @internal
*
* @param string|null $filename Optional override for the filename
*/
public function mediaRoot(): string
public function mediaRoot(string|null $filename = null): string
{
return $this->parent()->mediaRoot() . '/' . $this->mediaHash() . '/' . $this->filename();
$filename ??= $this->filename();

return $this->mediaDir() . '/' . $filename;
}

/**
Expand All @@ -402,10 +416,15 @@ public function mediaToken(): string
/**
* Returns the absolute Url to the file in the public media folder
* @internal
*
* @param string|null $filename Optional override for the filename
*/
public function mediaUrl(): string
public function mediaUrl(string|null $filename = null): string
{
return $this->parent()->mediaUrl() . '/' . $this->mediaHash() . '/' . $this->filename();
$url = $this->parent()->mediaUrl() . '/' . $this->mediaHash();
$filename ??= $this->filename();

return $url . '/' . $filename;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Cms/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static function thumb(
=> $media . '/assets/' . $model . '/' . $hash,
// parent files for file model that already included hash
$model instanceof File
=> dirname($model->mediaRoot()),
=> $model->mediaDir(),
// model files
default
=> $model->mediaRoot() . '/' . $hash
Expand Down
13 changes: 11 additions & 2 deletions src/Cms/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,14 +770,23 @@ public function isUnlisted(): bool
}

/**
* Returns the root to the media folder for the page
* Returns the absolute path to the media folder for the page
* @internal
*/
public function mediaRoot(): string
public function mediaDir(): string
{
return $this->kirby()->root('media') . '/pages/' . $this->id();
}

/**
* @see `::mediaDir`
* @internal
*/
public function mediaRoot(): string
{
return $this->mediaDir();
}

/**
* The page's base URL for any files
* @internal
Expand Down
13 changes: 11 additions & 2 deletions src/Cms/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,23 @@ public function is($site): bool
}

/**
* Returns the root to the media folder for the site
* Returns the absolute path to the media folder for the page
* @internal
*/
public function mediaRoot(): string
public function mediaDir(): string
{
return $this->kirby()->root('media') . '/site';
}

/**
* @see `::mediaDir`
* @internal
*/
public function mediaRoot(): string
{
return $this->mediaDir();
}

/**
* The site's base url for any files
* @internal
Expand Down
13 changes: 11 additions & 2 deletions src/Cms/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,23 @@ public function logout(Session|array|null $session = null): void
}

/**
* Returns the root to the media folder for the user
* Returns the absolute path to the media folder for the user
* @internal
*/
public function mediaRoot(): string
public function mediaDir(): string
{
return $this->kirby()->root('media') . '/users/' . $this->id();
}

/**
* @see `::mediaDir`
* @internal
*/
public function mediaRoot(): string
{
return $this->mediaDir();
}

/**
* Returns the media url for the user object
* @internal
Expand Down
48 changes: 46 additions & 2 deletions tests/Cms/File/FileMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,32 @@ public function setUp(): void
parent::setUp();

$this->app = $this->app->clone([
'roots' => [
'index' => self::TMP
],
'options' => [
'content.salt' => 'test'
]
]);
}

public function testMediaDir(): void
{
F::write($root = static::TMP . '/content/test.jpg', 'test');
touch($root, 5432112345);

$file = new File([
'filename' => 'test.jpg',
'parent' => $this->app->site()
]);

$this->assertSame(self::TMP . '/media/site/08756f3115-5432112345', $file->mediaDir());
}

public function testMediaHash(): void
{
F::write($file = static::TMP . '/content/test.jpg', 'test');
touch($file, 5432112345);
F::write($root = static::TMP . '/content/test.jpg', 'test');
touch($root, 5432112345);

$file = new File([
'filename' => 'test.jpg',
Expand All @@ -34,6 +50,20 @@ public function testMediaHash(): void
$this->assertSame('08756f3115-5432112345', $file->mediaHash());
}

public function testMediaRoot(): void
{
F::write($root = static::TMP . '/content/test.jpg', 'test');
touch($root, 5432112345);

$file = new File([
'filename' => 'test.jpg',
'parent' => $this->app->site()
]);

$this->assertSame(self::TMP . '/media/site/08756f3115-5432112345/test.jpg', $file->mediaRoot());
$this->assertSame(self::TMP . '/media/site/08756f3115-5432112345/test-120x.jpg', $file->mediaRoot('test-120x.jpg'));
}

public function testMediaToken(): void
{
$file = new File([
Expand All @@ -43,4 +73,18 @@ public function testMediaToken(): void

$this->assertSame('08756f3115', $file->mediaToken());
}

public function testMediaUrl(): void
{
F::write($root = static::TMP . '/content/test.jpg', 'test');
touch($root, 5432112345);

$file = new File([
'filename' => 'test.jpg',
'parent' => $this->app->site()
]);

$this->assertSame('/media/site/08756f3115-5432112345/test.jpg', $file->mediaUrl());
$this->assertSame('/media/site/08756f3115-5432112345/test-120x.jpg', $file->mediaUrl('test-120x.jpg'));
}
}
31 changes: 30 additions & 1 deletion tests/Cms/File/FileUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@

namespace Kirby\Cms;

use Kirby\Filesystem\F;
use PHPUnit\Framework\Attributes\CoversClass;

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

public function setUp(): void
{
parent::setUp();

$this->app = $this->app->clone([
'roots' => [
'index' => self::TMP
],
'options' => [
'content.salt' => 'test'
]
]);
}

public function testPermalink(): void
{
$page = new Page(['slug' => 'test']);
Expand All @@ -21,7 +37,7 @@ public function testPermalink(): void
$this->assertSame('//@/file/my-file-uuid', $file->permalink());
}

public function testUrl(): void
public function testUrlFixed(): void
{
$file = new File([
'filename' => 'test.pdf',
Expand All @@ -31,4 +47,17 @@ public function testUrl(): void

$this->assertSame($url, $file->url());
}

public function testUrlMedia(): void
{
F::copy(self::FIXTURES . '/test.pdf', $root = self::TMP . '/content/test.pdf');
touch($root, 1234567890);

$file = new File([
'filename' => 'test.pdf',
'parent' => $this->app->site()
]);

$this->assertSame('/media/site/b22a2d4f82-1234567890/test.pdf', $file->url());
}
}
12 changes: 6 additions & 6 deletions tests/Cms/Media/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,23 @@ public function testThumb()

// get file object
$file = $this->app->file('test.jpg');
Dir::make(dirname($file->mediaRoot()));
Dir::make($file->mediaDir());
$this->assertIsFile($file);

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

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

$thumb = Media::thumb($file, $file->mediaHash(), $file->filename());
$this->assertInstanceOf(Response::class, $thumb);
$this->assertNotFalse($thumb->body());
$this->assertSame(200, $thumb->code());
$this->assertSame('image/jpeg', $thumb->type());

$thumbInfo = getimagesize($mediaPath);
$thumbInfo = getimagesize($mediaRoot);
$this->assertSame(64, $thumbInfo[0]);
$this->assertSame(64, $thumbInfo[1]);
}
Expand Down Expand Up @@ -240,7 +240,7 @@ public function testThumbWithIncompleteJobFile()
$file = $this->app->file('test.jpg');

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

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

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

$this->expectException(\Exception::class);
$this->expectExceptionMessage('The file does not exist');
Expand Down
Loading