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

acceptedFileTypes on SpatieMediaLibraryFileUpload doesn't work for 3DM and SKP files #15611

Open
fmeccanici opened this issue Feb 15, 2025 · 2 comments

Comments

@fmeccanici
Copy link

fmeccanici commented Feb 15, 2025

Package

filament/spatie-laravel-media-library-plugin

Package Version

v3.2.140

Laravel Version

v11.42.1

Livewire Version

v3.5.20

PHP Version

PHP 8.3.16

Problem description

I want to reject IFC files but allow 3DM and SKP files. To accomplish this I used the SpatieMediaLibraryFileUpload component in my Resource form and wanted to use the ->acceptedFileTypes() function. I checked the source code and it sets rules for the mime types, so I researched which mime types where detected by PHP for these files:

$threeDmFile->getMimeType();
"application/octet-stream"
$ifcFile->getMimeType();
"text/plain"
$skpFile->getMimeType();`
"application/vnd.sketchup.skp"

So I added the ones I want to allow to the component:

Forms\Components\SpatieMediaLibraryFileUpload::make('designs')
    ->collection('designs')
    ->multiple()
    ->required()
    ->acceptedFileTypes([
        'application/vnd.sketchup.skp',
        'application/octet-stream'
    ]),

What happens after uploading all three files is that they all fail in their validation:

Image

So I decided to check which mime types are detected:

Forms\Components\SpatieMediaLibraryFileUpload::make('designs')
    ->collection('designs')
    ->multiple()
    ->required()
    ->afterStateUpdated(function ($state) {
        ray(collect($state)->map->getMimeType()->toArray());
    })

This leads me to the following discovery:

Image

The SKP file is correctly detected but the IFC and 3DM files are empty strings. From this observation you would expect the SKP file to be correctly validated but this is not the case (see first image). However when I add 'application/vnd.koan' :

->acceptedFileTypes([
    'application/vnd.sketchup.skp',
    'application/octet-stream',
    'application/vnd.koan'
]),

The SKP file is correctly uploaded:

Image

I'm trying to wrap my head around on what is happening, but I suspect the problem is that there is a different mechanism for mime detection. Since you use FilePond under the hood, this might do a different mime detection.

In any case do you have any suggestions on how to solve this problem?

Expected behavior

When I build the component like this:

  Forms\Components\SpatieMediaLibraryFileUpload::make('designs')
      ->collection('designs')
      ->multiple()
      ->required()
      ->acceptedFileTypes([
          'application/vnd.sketchup.skp',
          'application/octet-stream',
      ]),

Or ideally using the actual mime type for 3DM files 'model/x-3dmf' instead of 'application/octet-stream'.

I want to be able to upload 3DM and SKP files, rejecting IFC files.

Steps to reproduce

Create a Resource and add a SpatieMediaLibraryFileUpload component to the form schema:

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                SpatieMediaLibraryFileUpload::make('designs')
                    ->collection('designs')
                    ->multiple()
                    ->required()
                    ->acceptedFileTypes([
                        'application/vnd.sketchup.skp',
                        'application/octet-stream',
                    ])
//                    ->afterStateUpdated(function ($state) {
//                        ray(collect($state)->map->getMimeType()->toArray());
//                    })
            ]);
    }

Then try to upload a SKP, IFC and 3DM file. You will see that they will all be rejected.

Image

Unfortunately I cannot upload these files here so I created a WeTransfer link that is valid for 7 days: Link to download these files

Reproduction repository (issue will be closed if this is not valid)

https://github.com/fmeccanici/filament-replicate-file-upload-problem

Relevant log output

The return of ->getMimeType() for these files inside ->afterStateUpdated:

array:3 [▼
  "f7630376-4a4d-43fe-bb60-1694aab6fd5f" => "application/vnd.sketchup.skp"
  "ec6b5cee-529e-40ac-9bf8-5ac5d3509fb3" => ""
  "e9e2fdf1-7516-4553-a9c0-1440ebdfe097" => ""
]
@danharrin
Copy link
Member

I do think that the fix is unlikely to be in Filament your best bet is trying to reproduce the problem with Filepond and Livewire and pull request there. None of the file upload logic in Filament is really custom

@fmeccanici
Copy link
Author

@danharrin I created a PR with a solution to my problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

2 participants