F
Filament14mo 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());
}
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());
}
12 Replies
eazy
eazy14mo ago
saveRelationshipsUsing() in SpateMiediaLibraryFileUpload.php:91:
$this->saveRelationshipsUsing(static function (SpatieMediaLibraryFileUpload $component) {
$component->deleteAbandonedFiles();
$component->saveUploadedFiles();
});
$this->saveRelationshipsUsing(static function (SpatieMediaLibraryFileUpload $component) {
$component->deleteAbandonedFiles();
$component->saveUploadedFiles();
});
deleteAbondanodedFiles() in SpatieMediaLibraryFileUpload.php:189:
public function deleteAbandonedFiles(): void
{
/** @var Model&HasMedia $record */
$record = $this->getRecord();

$record
->getMedia($this->getCollection())
->whereNotIn('uuid', array_keys($this->getState() ?? []))
->each(fn (Media $media) => $media->delete());
}
public function deleteAbandonedFiles(): void
{
/** @var Model&HasMedia $record */
$record = $this->getRecord();

$record
->getMedia($this->getCollection())
->whereNotIn('uuid', array_keys($this->getState() ?? []))
->each(fn (Media $media) => $media->delete());
}
Dan Harrin
Dan Harrin14mo ago
it should be multiple() if you want multiple files to be stored at once
eazy
eazy14mo ago
Yeah but it still replaces all my uploaded files Let me add some screenshots
eazy
eazy14mo ago
A folder to which I uploaded 17 files.
eazy
eazy14mo ago
this is what it looks like in my "file viewer"
Dan Harrin
Dan Harrin14mo ago
are the 17 files populating into your file upload when the form loads
eazy
eazy14mo ago
Not sure how can I check that? I thank that is the issue
Dan Harrin
Dan Harrin14mo ago
open the file upload make sure theyre all there then you can add new ones, delete, etc
eazy
eazy14mo ago
This is what I see when I open the file upload, so no they're not populating
eazy
eazy14mo ago
How can I make sure they do?
Dan Harrin
Dan Harrin14mo ago
is that what you want though maybe you just extend the field class and remove the bit that deletes abandoned files
eazy
eazy14mo ago
yeah I do this:
->saveRelationshipsUsing(static function (SpatieMediaLibraryFileUpload $component) {
$component->saveUploadedFiles();
})
->saveRelationshipsUsing(static function (SpatieMediaLibraryFileUpload $component) {
$component->saveUploadedFiles();
})
and it seems to work