spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.78k stars 1.08k forks source link

Media Library is trying to delete conversions that were not applied #3727

Closed timgavin closed 3 days ago

timgavin commented 3 days ago

Laravel 11.32.0 Media Library: 11.10.0 PHP: 8.2.25 Using Herd 1.12.0 DigitalOcean Spaces for file storage

I've encountered an issue and I'm not sure if it's a bug. There is pretty much nothing I could find in the docs about deleting images, other than a video which only skims the surface.

When deleting a single image, I noticed the conversions were not being deleted; only the original image. So I started poking around. When I looked in the Laravel log I noticed this.

[2024-11-18 17:28:52] local.ERROR: There is no conversion named `header` 
{"userId":2,"exception":"[object] (Spatie\\MediaLibrary\\MediaCollections\\Exceptions\\InvalidConversion(code: 0): 
There is no conversion named `header` at 
/Users/tim/Herd/myproject/vendor/spatie/laravel-medialibrary/src/MediaCollections/Exceptions/InvalidConversion.php:11)

Well, I have a header conversion, but it's not being applied to this particular disk.

Here are the methods on my User model

public function registerMediaCollections(): void
{
    $this->addMediaCollection('photos');
    $this->addMediaCollection('cloud');
    $this->addMediaCollection('my-photos'); // a user's profile page photos
    $this->addMediaCollection('avatars')->singleFile(); // user's avatar
    $this->addMediaCollection('headers')->singleFile(); // user's profile header image
}

public function registerMediaConversions(?Media $media = null): void
{
    $this
        ->addMediaConversion('sm')
        ->fit(Fit::Crop, 300, 300)
        ->performOnCollections('photos', 'cloud', 'my-photos', 'avatars')
        ->nonQueued();

    $this
        ->addMediaConversion('md')
        ->fit(Fit::Crop, 500, 500)
        ->performOnCollections('photos', 'cloud', 'my-photos', 'avatars')
        ->nonQueued();

    $this
        ->addMediaConversion('lg')
        ->fit(Fit::Crop, 800, 800)
        ->performOnCollections('photos', 'cloud', 'my-photos')
        ->nonQueued();

    $this
        ->addMediaConversion('fullsize')
        ->performOnCollections('photos', 'cloud', 'my-photos', 'headers')
        ->nonQueued();

    $this
        ->addMediaConversion('header')
        ->fit(Fit::Crop, 1300, 260)
        ->performOnCollections('headers')
        ->nonQueued();

    $this
        ->addMediaConversion('header-sm')
        ->fit(Fit::Crop, 780, 120)
        ->performOnCollections('headers')
        ->nonQueued();
}

In my Livewire component I am uploading the image to the cloud disk.

public function save()
{
    $this->validate();

    foreach ($this->photos as $photo) {
        auth()->user()
            ->addMedia($photo)
            ->usingName(Str::random(12))
            ->preservingOriginal()
            ->toMediaCollection('cloud', 'cloud');
    }

    $this->resetPhotos();

    $this->media = auth()->user()->getMedia('cloud');
}

And then deleting a single image from the same component

public function destroy($photoId)
{
    Media::find($photoId)->delete();

    $this->media = auth()->user()->getMedia('cloud');
}

When investigating the destroy method

public function destroy($photoId)
{
    $media = Media::find($photoId);
    dd($media->getMediaConversionNames());

    Media::find($photoId)->delete();

    $this->media = auth()->user()->getMedia('cloud');
}

This is returned.

array:6 [▼
  0 => "sm"
  1 => "md"
  2 => "lg"
  3 => "fullsize"
  4 => "header"
  5 => "header-sm"
]

As you can see from registerMediaConversions I am only applying sm, md, lg, and fullsize to the cloud disk. Why are the other unused collections being returned and throwing an error? Is this a bug? Or am I doing something wrong?

timgavin commented 3 days ago

I see this was actually covered here: https://github.com/spatie/laravel-medialibrary/issues/3724