This commit is contained in:
baodev0
2026-07-25 15:18:48 +07:00
parent 306e65e503
commit 8a451e434e
9 changed files with 341 additions and 1929 deletions
@@ -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');