Skip to content

Commit

Permalink
fix import when existing file has uppercase extension
Browse files Browse the repository at this point in the history
  • Loading branch information
frasmage committed Jun 25, 2022
1 parent 0719530 commit 400260b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/MediaUploader.php
Expand Up @@ -668,13 +668,12 @@ public function import(string $disk, string $directory, string $filename, string
$model->disk = $disk;
$model->directory = $directory;
$model->filename = $filename;
$model->extension = $this->verifyExtension($extension);
$model->extension = $this->verifyExtension($extension, false);

if (!$storage->has($model->getDiskPath())) {
throw FileNotFoundException::fileNotFound($model->getDiskPath());
}


$model->mime_type = $this->verifyMimeType(
$this->inferMimeType($storage, $model->getDiskPath())
);
Expand Down Expand Up @@ -785,12 +784,13 @@ private function verifySource(): void
private function inferMimeType(Filesystem $filesystem, string $path): string
{
try {
return $filesystem->mimeType($path);
$mime = $filesystem->mimeType($path);
} catch (UnableToRetrieveMetadata $e) {
// previous versions of flysystem would default to octet-stream when
// the file was unrecognized. Maintain the behaviour for now
return 'application/octet-stream';
}
return $mime ?: 'application/octet-stream';
}

/**
Expand All @@ -816,15 +816,15 @@ private function verifyMimeType(string $mimeType): string
* @return string
* @throws FileNotSupportedException If the file extension is not allowed
*/
private function verifyExtension(string $extension): string
private function verifyExtension(string $extension, bool $toLower = true): string
{
$extension = strtolower($extension);
$extensionLower = strtolower($extension);
$allowed = $this->config['allowed_extensions'] ?? [];
if (!empty($allowed) && !in_array($extension, $allowed)) {
throw FileNotSupportedException::extensionRestricted($extension, $allowed);
if (!empty($allowed) && !in_array($extensionLower, $allowed)) {
throw FileNotSupportedException::extensionRestricted($extensionLower, $allowed);
}

return $extension;
return $toLower ? $extensionLower : $extension;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/Integration/MediaUploaderTest.php
Expand Up @@ -621,6 +621,31 @@ public function test_it_imports_existing_files()
$this->assertEquals('image', $media->aggregate_type);
}

public function test_it_imports_existing_files_with_uppercase()
{
$this->useFilesystem('tmp');
$this->useDatabase();

$media = factory(Media::class)->make(
[
'disk' => 'tmp',
'directory' => 'foo',
'filename' => 'bar',
'extension' => 'PNG',
'mime_type' => 'image/png'
]
);
$this->seedFileForMedia($media, $this->sampleFile());

$media = Facade::importPath('tmp', 'foo/bar.PNG');
$this->assertInstanceOf(Media::class, $media);
$this->assertEquals('tmp', $media->disk);
$this->assertEquals('foo/bar.PNG', $media->getDiskPath());
$this->assertEquals('image/png', $media->mime_type);
$this->assertEquals(self::TEST_FILE_SIZE, $media->size);
$this->assertEquals('image', $media->aggregate_type);
}

public function test_it_updates_existing_media()
{
$this->useDatabase();
Expand Down

0 comments on commit 400260b

Please sign in to comment.