Skip to content

Commit c48a731

Browse files
committed
Begin adding back support for fileless media file instances
1 parent eefd67f commit c48a731

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

packages/framework/src/Support/Filesystem/MediaFile.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ public function __construct(string $path)
4141
{
4242
parent::__construct($this->getNormalizedPath($path));
4343

44-
$this->length = $this->findContentLength();
45-
$this->mimeType = $this->findMimeType();
46-
$this->hash = $this->findHash();
44+
if (Filesystem::exists($this->getAbsolutePath())) {
45+
[$contentLength, $mimeType, $hash] = $this->getMetadata();
46+
47+
$this->length = $contentLength;
48+
$this->mimeType = $mimeType;
49+
$this->hash = $hash;
50+
}
4751
}
4852

4953
/**
@@ -172,10 +176,6 @@ protected function getNormalizedPath(string $path): string
172176
// Normalize the path to include the media directory
173177
$path = static::sourcePath(trim_slashes(Str::after($path, Hyde::getMediaDirectory())));
174178

175-
if (Filesystem::missing($path)) {
176-
throw new FileNotFoundException($path);
177-
}
178-
179179
return $path;
180180
}
181181

@@ -220,4 +220,17 @@ protected function findHash(): string
220220
{
221221
return Filesystem::hash($this->getPath(), 'crc32');
222222
}
223+
224+
protected function getMetadata(): array
225+
{
226+
if (Filesystem::missing($this->getAbsolutePath())) {
227+
throw new FileNotFoundException($this->getPath(), "Cannot get metadata for a file that does not exist on disk. (File [{$this->getPath()}] not found)");
228+
}
229+
230+
$contentLength = $this->findContentLength();
231+
$mimeType = $this->findMimeType();
232+
$hash = $this->findHash();
233+
234+
return [$contentLength, $mimeType, $hash];
235+
}
223236
}

packages/framework/tests/Feature/Support/MediaFileTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,13 @@ public function testMediaFilePathHandling()
7878
$this->assertSame('_media/subfolder/nested_file.txt', $mediaFile->getPath());
7979
}
8080

81-
public function testMediaFileExceptionHandling()
81+
public function testCanCreateInMemoryMediaFileInstances()
8282
{
83-
$this->expectException(FileNotFoundException::class);
84-
MediaFile::make('non_existent_file.txt');
83+
$mediaFile = MediaFile::make('non_existent_file.txt');
84+
85+
$this->assertInstanceOf(MediaFile::class, $mediaFile);
86+
87+
$this->assertSame('non_existent_file.txt', $mediaFile->getAbsolutePath());
8588
}
8689

8790
public function testMediaDirectoryCustomization()

packages/framework/tests/Unit/Support/MediaFileUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public function testExceptionIsThrownWhenConstructingFileThatDoesNotExist()
288288
->andReturn(true);
289289

290290
$this->expectException(FileNotFoundException::class);
291-
$this->expectExceptionMessage('File [_media/foo] not found.');
291+
$this->expectExceptionMessage('Cannot get metadata for a file that does not exist on disk. (File [_media/foo] not found)');
292292

293293
MediaFile::make('foo');
294294
}

0 commit comments

Comments
 (0)