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

Spatie medialibrary speedup remote file upload #14542

Open
jornwildenbeest opened this issue Oct 16, 2024 · 0 comments
Open

Spatie medialibrary speedup remote file upload #14542

jornwildenbeest opened this issue Oct 16, 2024 · 0 comments
Labels

Comments

@jornwildenbeest
Copy link

jornwildenbeest commented Oct 16, 2024

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

  • You can donate funding to this issue. We receive the money once the issue is completed & confirmed by you.
  • 100% of the funding will be distributed between the Filament core team to run all aspects of the project.
  • Thank you in advance for helping us make maintenance sustainable!
Fund with Polar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Todo
Development

No branches or pull requests

1 participant