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

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

Closed
timgavin opened this issue Nov 18, 2024 · 1 comment
Closed

Comments

@timgavin
Copy link
Contributor

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
Copy link
Contributor Author

I see this was actually covered here: #3724

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant