update
This commit is contained in:
@@ -72,6 +72,42 @@ class MediaController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(string $id): JsonResponse
|
||||
{
|
||||
$path = $this->resolvePathById($id);
|
||||
|
||||
if ($path === null) {
|
||||
return response()->json(['error' => true, 'message' => 'File không tồn tại.'], 404);
|
||||
}
|
||||
|
||||
Storage::disk('local')->delete($path);
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Xoá file thành công.']);
|
||||
}
|
||||
|
||||
public function bulkDestroy(Request $request): JsonResponse
|
||||
{
|
||||
$ids = $request->validate(['ids' => ['required', 'array'], 'ids.*' => ['string']])['ids'];
|
||||
|
||||
$paths = collect($ids)
|
||||
->map(fn (string $id) => $this->resolvePathById($id))
|
||||
->filter()
|
||||
->values();
|
||||
|
||||
Storage::disk('local')->delete($paths->all());
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => "Đã xoá {$paths->count()} file.",
|
||||
]);
|
||||
}
|
||||
|
||||
private function resolvePathById(string $id): ?string
|
||||
{
|
||||
return collect(Storage::disk('local')->allFiles())
|
||||
->first(fn (string $path) => sha1($path) === $id);
|
||||
}
|
||||
|
||||
private function formatFile(string $path): array
|
||||
{
|
||||
$disk = Storage::disk('local');
|
||||
|
||||
Reference in New Issue
Block a user