Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Lazy loaded MediaFile metadata properties #1933

Merged
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
39 changes: 28 additions & 11 deletions packages/framework/src/Support/Filesystem/MediaFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ class MediaFile extends ProjectFile
/** @var array<string> The default extensions for media types */
final public const EXTENSIONS = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'];

public readonly int $length;
public readonly string $mimeType;
public readonly string $hash;
protected readonly int $length;
protected readonly string $mimeType;
protected readonly string $hash;

public function __construct(string $path)
{
parent::__construct($this->getNormalizedPath($path));

$this->length = $this->findContentLength();
$this->mimeType = $this->findMimeType();
$this->hash = $this->findHash();
}

/**
Expand Down Expand Up @@ -101,16 +97,22 @@ public function toArray(): array

public function getContentLength(): int
{
$this->ensureInstanceIsBooted('length');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically we could do something like $this->getBootedProperty() but I think this is much more readable and understandable.


return $this->length;
}

public function getMimeType(): string
{
$this->ensureInstanceIsBooted('mimeType');

return $this->mimeType;
}

public function getHash(): string
{
$this->ensureInstanceIsBooted('hash');

return $this->hash;
}

Expand All @@ -124,6 +126,7 @@ public static function getCacheBustKey(string $file): string

protected function getNormalizedPath(string $path): string
{
// Ensure we are working with a relative project path
$path = Hyde::pathToRelative($path);

// Normalize paths using output directory to have source directory prefix
Expand All @@ -134,10 +137,6 @@ protected function getNormalizedPath(string $path): string
// Normalize the path to include the media directory
$path = static::sourcePath(trim_slashes(Str::after($path, Hyde::getMediaDirectory())));

if (Filesystem::missing($path)) {
throw new FileNotFoundException($path);
}

return $path;
}

Expand Down Expand Up @@ -183,4 +182,22 @@ protected function findHash(): string
{
return Filesystem::hash($this->getPath(), 'crc32');
}

protected function ensureInstanceIsBooted(string $property): void
{
if (! isset($this->$property)) {
$this->boot();
}
}

protected function boot(): void
{
if (Filesystem::missing($this->getPath())) {
throw new FileNotFoundException($this->getPath());
}

$this->length = $this->findContentLength();
$this->mimeType = $this->findMimeType();
$this->hash = $this->findHash();
}
}
2 changes: 1 addition & 1 deletion packages/framework/tests/Feature/Support/MediaFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testMediaFilePathHandling()
public function testMediaFileExceptionHandling()
{
$this->expectException(FileNotFoundException::class);
MediaFile::make('non_existent_file.txt');
MediaFile::make('non_existent_file.txt')->getContentLength();
}

public function testMediaDirectoryCustomization()
Expand Down
20 changes: 15 additions & 5 deletions packages/framework/tests/Unit/Support/MediaFileUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ public function testConstructorWithVariousInputFormats()
public function testConstructorSetsProperties()
{
$file = new MediaFile('foo.txt');
$this->assertNotNull($file->length);
$this->assertNotNull($file->mimeType);
$this->assertNotNull($file->hash);
$this->assertNotNull($file->getContentLength());
$this->assertNotNull($file->getMimeType());
$this->assertNotNull($file->getHash());
}

public function testNormalizePathWithAbsolutePath()
Expand Down Expand Up @@ -271,7 +271,8 @@ public function testGetHashReturnsHash()
$this->assertSame(hash('crc32', 'Hello World!'), MediaFile::make('foo.txt')->getHash());
}

public function testExceptionIsThrownWhenConstructingFileThatDoesNotExist()
/** @dataProvider bootableMethodsProvider */
public function testExceptionIsThrownWhenBootingFileThatDoesNotExist(string $bootableMethod)
{
$this->mockFilesystem->shouldReceive('missing')
->with(Hyde::path('_media/foo'))
Expand All @@ -280,7 +281,7 @@ public function testExceptionIsThrownWhenConstructingFileThatDoesNotExist()
$this->expectException(FileNotFoundException::class);
$this->expectExceptionMessage('File [_media/foo] not found.');

MediaFile::make('foo');
MediaFile::make('foo')->$bootableMethod();
}

public function testExceptionIsNotThrownWhenConstructingFileThatDoesExist()
Expand Down Expand Up @@ -389,4 +390,13 @@ public function testOutputPathWithLeadingSlash()
{
$this->assertSame(Hyde::sitePath('media/foo'), MediaFile::outputPath('/foo'));
}

public static function bootableMethodsProvider(): array
{
return [
['getContentLength'],
['getMimeType'],
['getHash'],
];
}
}