From 1825545bf0df30af46db72fd37020aba7e279b66 Mon Sep 17 00:00:00 2001 From: Takeshi Kimata <117462761+kimatata@users.noreply.github.com> Date: Sun, 17 Mar 2024 20:15:54 +0900 Subject: [PATCH] delete uploaded file --- backend/index.js | 2 + backend/routes/attachments/delete.js | 47 +++++++++++++++++++ .../[folderId]/cases/[caseId]/case-editor.tsx | 7 ++- .../[folderId]/cases/[caseId]/caseControl.ts | 33 ++++++++++++- 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 backend/routes/attachments/delete.js diff --git a/backend/index.js b/backend/index.js index db34475..e9d5aee 100644 --- a/backend/index.js +++ b/backend/index.js @@ -63,7 +63,9 @@ app.use("/steps", stepsDeleteRoute); // "/attachments" const attachmentsNewRoute = require("./routes/attachments/new")(sequelize); +const attachmentsDeleteRoute = require("./routes/attachments/delete")(sequelize); app.use("/attachments", attachmentsNewRoute); +app.use("/attachments", attachmentsDeleteRoute); // "/runs" const runsIndexRoute = require("./routes/runs/index")(sequelize); diff --git a/backend/routes/attachments/delete.js b/backend/routes/attachments/delete.js new file mode 100644 index 0000000..668800a --- /dev/null +++ b/backend/routes/attachments/delete.js @@ -0,0 +1,47 @@ +const express = require("express"); +const router = express.Router(); +const path = require("path"); +const fs = require("fs"); +const defineAttachment = require("../../models/attachments"); +const { DataTypes } = require("sequelize"); + +module.exports = function (sequelize) { + const Attachment = defineAttachment(sequelize, DataTypes); + + router.delete("/:attachmentId", async (req, res) => { + const attachmentId = req.params.attachmentId; + const t = await sequelize.transaction(); + + try { + const attachment = await Attachment.findByPk(attachmentId); + if (!attachment) { + await t.rollback(); + return res.status(404).send("Attachment not found"); + } + + // delete file from folder + const uploadDir = path.join(__dirname, "../../public"); + const url = attachment.path; + const parts = url.split('/'); + const fileNameWithFolder = parts.slice(-2).join('/'); + const filePath = path.join(uploadDir, fileNameWithFolder); + fs.unlink(filePath, (err) => { + if (err) { + console.error('Error deleting file:', err); + t.rollback(); + return res.status(404).send("Attachment not found"); + } + }); + + await attachment.destroy({ transaction: t }); + await t.commit(); + res.status(204).send(); + } catch (error) { + console.error(error); + await t.rollback(); + res.status(500).send("Internal Server Error"); + } + }); + + return router; +}; diff --git a/frontend/app/projects/[projectId]/folders/[folderId]/cases/[caseId]/case-editor.tsx b/frontend/app/projects/[projectId]/folders/[folderId]/cases/[caseId]/case-editor.tsx index 6e39c7a..aabca7f 100644 --- a/frontend/app/projects/[projectId]/folders/[folderId]/cases/[caseId]/case-editor.tsx +++ b/frontend/app/projects/[projectId]/folders/[folderId]/cases/[caseId]/case-editor.tsx @@ -22,6 +22,7 @@ import { fetchDeleteStep, updateCase, fetchCreateAttachments, + fetchDeleteAttachment, } from "./caseControl"; const defaultTestCase = { @@ -113,6 +114,10 @@ export default function CaseEditor({ fetchCreateAttachments(params.caseId, event.target.files); }; + const onAttachmentDelete = async (attachmentId: number) => { + await fetchDeleteAttachment(attachmentId); + }; + useEffect(() => { async function fetchDataEffect() { try { @@ -334,7 +339,7 @@ export default function CaseEditor({