filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
19.4k stars 2.97k forks source link

Spatie medialibrary speedup remote file upload #14542

Open jornwildenbeest opened 1 month ago

jornwildenbeest commented 1 month ago

Package

filament/spatie-laravel-media-library-plugin

Package Version

v3.2.115

Laravel Version

v10.48.22

Livewire Version

v3.5.6

PHP Version

8.1.29

Problem description

When uploading files to a remote file system (amazon s3 for example) the filament package does not make use of the copy file method that spatie medialibrary provides, but always uploads the temporary file again. Even when the temporary file system is also pointed to the remote file system. This causes slow file uploads with larger files.

By default it uses the File class instead of RemoteFile class.

This is because the SpatieMediaLibraryFileUpload class uses the addMediaFromString method instead of the addMediaFromDisk method: https://github.com/filamentphp/filament/blob/3.x/packages/spatie-laravel-media-library-plugin/src/Forms/Components/SpatieMediaLibraryFileUpload.php#L143

I believe this can be fixed by changed the code on line 143 to:

 $disk = config('filament.default_filesystem_disk');

            $pathToTemporaryLivewireFile = (string) Str::of($file->getRealPath())
                ->after(Storage::disk($disk)->path('/'))
                ->ltrim('/');

            /** @var FileAdder $mediaAdder */
            $mediaAdder = $record->addMediaFromDisk($pathToTemporaryLivewireFile, $disk);

This significantly improves the performance of large file uploads to a remote S3 server since the files do not need to be downloaded and re-uploaded by the laravel application

Expected behavior

When both (temporary and final file system) are the same remote file system, use the copy method.

Steps to reproduce

  1. clone the added repo
  2. setup s3 with
  3. try uploading a file
  4. Add logs in the vendor/spatie/laravel-medialibrary/src/MediaCollections/FileAdder.php file on line 456

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

https://github.com/jornwildenbeest/filament-issues-video

Relevant log output

No response

Donate 💰 to fund this issue

Fund with Polar