Skip to content

Commit

Permalink
restore flysystem unrecognized mime type behaviour
Browse files Browse the repository at this point in the history
could be changed in the next major version
  • Loading branch information
frasmage committed May 10, 2022
1 parent aca0360 commit 271a8c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/Helpers/File.php
Expand Up @@ -17,7 +17,6 @@ class File
*/
public static function cleanDirname(string $path): string
{

$dirname = pathinfo($path, PATHINFO_DIRNAME);
if ($dirname == '.') {
return '';
Expand Down
21 changes: 19 additions & 2 deletions src/MediaUploader.php
Expand Up @@ -5,6 +5,7 @@

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Filesystem\FilesystemManager;
use League\Flysystem\UnableToRetrieveMetadata;
use Plank\Mediable\Exceptions\MediaUpload\ConfigurationException;
use Plank\Mediable\Exceptions\MediaUpload\FileExistsException;
use Plank\Mediable\Exceptions\MediaUpload\FileNotFoundException;
Expand Down Expand Up @@ -673,7 +674,10 @@ public function import(string $disk, string $directory, string $filename, string
throw FileNotFoundException::fileNotFound($model->getDiskPath());
}

$model->mime_type = $this->verifyMimeType($storage->mimeType($model->getDiskPath()));

$model->mime_type = $this->verifyMimeType(
$this->inferMimeType($storage, $model->getDiskPath())
);
$model->aggregate_type = $this->inferAggregateType($model->mime_type, $model->extension);
$model->size = $this->verifyFileSize($storage->size($model->getDiskPath()));

Expand Down Expand Up @@ -702,7 +706,9 @@ public function update(Media $media): bool
$storage = $this->filesystem->disk($media->disk);

$media->size = $this->verifyFileSize($storage->size($media->getDiskPath()));
$media->mime_type = $this->verifyMimeType($storage->mimeType($media->getDiskPath()));
$media->mime_type = $this->verifyMimeType(
$this->inferMimeType($storage, $media->getDiskPath())
);
$media->aggregate_type = $this->inferAggregateType($media->mime_type, $media->extension);

if ($dirty = $media->isDirty()) {
Expand Down Expand Up @@ -776,6 +782,17 @@ private function verifySource(): void
}
}

private function inferMimeType(Filesystem $filesystem, string $path): string
{
try {
return $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';
}
}

/**
* Ensure that the file's mime type is allowed.
* @param string $mimeType
Expand Down

0 comments on commit 271a8c3

Please sign in to comment.