diff --git a/routes/api.php b/routes/api.php index 52716a0..3f85200 100644 --- a/routes/api.php +++ b/routes/api.php @@ -18,6 +18,8 @@ Route::prefix(config('asseco-attachments.routes.prefix')) ->middleware(config('asseco-attachments.routes.middleware')) ->group(function () { + Route::delete('attachments/bulk-delete', [AttachmentController::class, 'bulkDelete'])->name('attachments.bulkDelete'); + Route::apiResource('attachments', AttachmentController::class); Route::get('attachments/{attachment}/download', [AttachmentController::class, 'download'])->name('attachments.download'); diff --git a/src/App/Http/Controllers/AttachmentController.php b/src/App/Http/Controllers/AttachmentController.php index 195a999..74972d0 100644 --- a/src/App/Http/Controllers/AttachmentController.php +++ b/src/App/Http/Controllers/AttachmentController.php @@ -7,11 +7,13 @@ use Asseco\Attachments\App\Contracts\Attachment as AttachmentContract; use Asseco\Attachments\App\Http\Requests\AttachmentRequest; use Asseco\Attachments\App\Http\Requests\AttachmentUpdateRequest; +use Asseco\Attachments\App\Http\Requests\DeleteAttachmentsRequest; use Asseco\Attachments\App\Models\Attachment; use Asseco\Attachments\App\Service\CachedUploads; use Exception; use Illuminate\Http\JsonResponse; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; class AttachmentController extends Controller @@ -67,7 +69,7 @@ public function show(Attachment $attachment): JsonResponse * Update the specified resource. * * @param Attachment $attachment - * @param AttachmentRequest $request + * @param AttachmentUpdateRequest $request * @return JsonResponse */ public function update(Attachment $attachment, AttachmentUpdateRequest $request): JsonResponse @@ -97,4 +99,40 @@ public function download(Attachment $attachment) { return Storage::download($attachment->path, $attachment->name); } + + /** + * Remove multiple resources from storage. + * + * @param DeleteAttachmentsRequest $request + * @return JsonResponse + */ + public function bulkDelete(DeleteAttachmentsRequest $request): JsonResponse + { + $attachmentIds = $request->input('attachment_ids'); + $deleted = []; + $notDeleted = []; + $paths = Attachment::whereIn('id', $attachmentIds)->pluck('path', 'id'); + foreach ($attachmentIds as $id) { + $path = $paths->get($id); + try { + Attachment::where('id', $id)->delete(); + if ($path) { + Storage::delete($path); + } + $deleted[] = $id; + } catch (Exception $e) { + $notDeleted[] = $id; + Log::error('Failed to delete attachment', [ + 'attachment_id' => $id, + 'exception' => $e, + ]); + } + } + + return response()->json([ + 'message' => 'Bulk delete operation completed', + 'deleted' => $deleted, + 'not_deleted' => $notDeleted, + ]); + } } diff --git a/src/App/Http/Requests/DeleteAttachmentsRequest.php b/src/App/Http/Requests/DeleteAttachmentsRequest.php new file mode 100644 index 0000000..c450d32 --- /dev/null +++ b/src/App/Http/Requests/DeleteAttachmentsRequest.php @@ -0,0 +1,33 @@ + 'required|array', + 'attachment_ids.*' => 'exists:attachments,id', + ]; + } +}