Split out the image deletion into a separate method

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-20 03:40:16 +01:00
parent beac4c8b8a
commit 10856516ac

View file

@ -10,6 +10,7 @@ use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Exception\NotReadableException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
class ImageUploadRequest extends Request
{
@ -123,7 +124,7 @@ class ImageUploadRequest extends Request
} catch(NotReadableException $e) {
Log::debug($e);
$validator = \Validator::make([], []);
$validator = Validator::make([], []);
$validator->errors()->add($form_fieldname, trans('general.unaccepted_image_type', ['mimetype' => $image->getClientMimeType()]));
throw new \Illuminate\Validation\ValidationException($validator);
@ -135,28 +136,29 @@ class ImageUploadRequest extends Request
}
// Remove Current image if exists
if (($item->{$form_fieldname}!='') && (Storage::disk('public')->exists($path.'/'.$item->{$db_fieldname}))) {
try {
Storage::disk('public')->delete($path.'/'.$item->{$form_fieldname});
} catch (\Exception $e) {
Log::debug('Could not delete old file. '.$path.'/'.$file_name.' does not exist?');
}
}
$item = $this->deleteExistingImage($item, $form_fieldname, $path, $db_fieldname);
$item->{$db_fieldname} = $file_name;
}
// If the user isn't uploading anything new but wants to delete their old image, do so
} elseif ($this->input('image_delete') == '1') {
Log::debug('Deleting image');
try {
Storage::disk('public')->delete($path.'/'.$item->{$db_fieldname});
$item->{$db_fieldname} = null;
} catch (\Exception $e) {
Log::debug($e);
}
$item = $this->deleteExistingImage($item, $form_fieldname, $path, $db_fieldname);
}
return $item;
}
public function deleteExistingImage($item, $form_fieldname = 'image', $path = null, $db_fieldname = 'image') {
\Log::error('DELETING '.$form_fieldname);
try {
Storage::disk('public')->delete($path.'/'.$item->{$db_fieldname});
$item->{$db_fieldname} = null;
\Log::error('deleted successfully');
} catch (\Exception $e) {
Log::debug($e);
}
return $item;