Allow the main_ext to be webp by not creating or deleting a webp copy when this is the case, and delete thumbnails when DashboardModel items are deleted

This commit is contained in:
Kevin MacMartin 2023-05-31 23:21:14 -04:00
parent f8aa6e9984
commit ced98ebb81

View file

@ -282,7 +282,10 @@ class DashboardModel extends Model
// Save the image // Save the image
$image->save($base_image_filename . $main_ext); $image->save($base_image_filename . $main_ext);
$image->save($base_image_filename . 'webp');
if ($main_ext !== 'webp') {
$image->save($base_image_filename . 'webp');
}
// Create the thumbnail directory if it doesn't exist // Create the thumbnail directory if it doesn't exist
$thumb_directory = public_path($this->getUploadsPath('thumb')); $thumb_directory = public_path($this->getUploadsPath('thumb'));
@ -303,7 +306,10 @@ class DashboardModel extends Model
// Save the thumbnail // Save the thumbnail
$thumb->save($base_thumb_filename . $main_ext); $thumb->save($base_thumb_filename . $main_ext);
$thumb->save($base_thumb_filename . 'webp');
if ($main_ext !== 'webp') {
$thumb->save($base_thumb_filename . 'webp');
}
} }
return 'success'; return 'success';
@ -342,8 +348,8 @@ class DashboardModel extends Model
// Build the set of extensions to delete // Build the set of extensions to delete
array_push($extensions, $main_ext); array_push($extensions, $main_ext);
// If the image extension isn't svg also delete the webp // If the image extension isn't svg or webp also delete the webp copy
if ($main_ext != 'svg') { if ($main_ext != 'svg' && $main_ext != 'webp') {
array_push($extensions, 'webp'); array_push($extensions, 'webp');
} }
@ -360,6 +366,18 @@ class DashboardModel extends Model
} else if ($not_exist_fail) { } else if ($not_exist_fail) {
return 'image-not-exists-fail'; return 'image-not-exists-fail';
} }
// Get the full path of the thumbnail
$thumb = public_path($this->getUploadsPath('thumb') . $this->id . '-' . $name . '.' . $ext);
// Try to delete the thumbnail
if (file_exists($thumb)) {
if (!unlink($thumb)) {
return 'thumb-delete-fail';
}
} else if ($not_exist_fail) {
return 'thumb-not-exists-fail';
}
} }
// Success // Success