Fix LessonFile duplication: upsert on upload, delete S3 on remove
POST /api/admin/lesson-files now checks for an existing record with the same (lessonId, name) before uploading — replaces it (old S3 object deleted) instead of always creating a new one. Previously every save cycle accumulated an extra copy; 1183 duplicates occupying 6.5 GiB were found and cleaned up. DELETE now receives the file URL and extracts the S3 key from it, so manual deletion actually removes the object from storage. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -36,12 +36,12 @@ export function LessonFilesManager({ lessonId, initialFiles }: { lessonId: strin
|
||||
e.target.value = "";
|
||||
}
|
||||
|
||||
async function handleDelete(fileId: string) {
|
||||
async function handleDelete(fileId: string, url: string) {
|
||||
if (!confirm("Удалить файл?")) return;
|
||||
await fetch("/api/admin/lesson-files", {
|
||||
method: "DELETE",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ fileId }),
|
||||
body: JSON.stringify({ fileId, url }),
|
||||
});
|
||||
setFiles((prev) => prev.filter((f) => f.id !== fileId));
|
||||
}
|
||||
@@ -122,7 +122,7 @@ export function LessonFilesManager({ lessonId, initialFiles }: { lessonId: strin
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleDelete(f.id)}
|
||||
onClick={() => handleDelete(f.id, f.url)}
|
||||
className="text-xs"
|
||||
style={{ color: "oklch(0.577 0.245 27.325)" }}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user