FilamentF
Filament3y ago
eazy

Spatie Media Library File Upload replacing every uploaded file

Hello, I have created a custom action for a file upload to my table, but when I upload a file it replaces all the already uploaded files. This hasn't been happening before.
After going through the code I found out the deleteAbandonedFiles() method in the plugin, this gets called in the saveRelationshipUsing() method. It looks to me that only the uploaded file UUID gets added to the state so the rest of the files get deleted. But I'm not sure how to fix this.

My custom action:
    protected function setUp(): void
    {
        parent::setUp();

//        $this->authorize('upload-file');

        $this->label(__('filemanager.upload file'));

        $this->modalButton(__('filemanager.upload file'));

        $this->modalHeading(__('filemanager.upload file'));

        $this->button();

        $this->successNotificationTitle(__('filemanager.file uploaded'));

        $this->icon('tabler-file-upload');

        $this->form(function () {
            $field = SpatieMediaLibraryFileUpload::make('file')
                ->model($this->record)
                ->disableLabel()
                ->required()
                ->multiple()
                ->minFiles(1)
                ->maxSize(500_000); // 500mb

            $acceptedFileTypes = $this->getAcceptedFileTypes();

            if (!empty($acceptedFileTypes) && $acceptedFileTypes !== null) {
                $field->acceptedFileTypes($acceptedFileTypes);
            }

            return [$field];
        });

        $this->action(fn(UploadFilesAction $action) => $action->success());
    }
Was this page helpful?