From aea08e9c80a81f75869def2fc459ad4e4a8abe22 Mon Sep 17 00:00:00 2001 From: Takeshi Kimata <117462761+kimatata@users.noreply.github.com> Date: Sun, 17 Mar 2024 16:34:53 +0900 Subject: [PATCH] file upload and store --- backend/routes/attachments/new.js | 15 +++++- .../cases/[caseId]/attachments-editor.tsx | 48 +++++++++++++++-- .../[folderId]/cases/[caseId]/page.tsx | 53 +++++++++++-------- 3 files changed, 91 insertions(+), 25 deletions(-) diff --git a/backend/routes/attachments/new.js b/backend/routes/attachments/new.js index edf56a2..066f1db 100644 --- a/backend/routes/attachments/new.js +++ b/backend/routes/attachments/new.js @@ -4,10 +4,12 @@ const path = require("path"); const fs = require("fs"); const multer = require("multer"); const defineAttachment = require("../../models/attachments"); +const defineCaseAttachment = require("../../models/caseAttachments"); const { DataTypes } = require("sequelize"); module.exports = function (sequelize) { const Attachment = defineAttachment(sequelize, DataTypes); + const CaseAttachment = defineCaseAttachment(sequelize, DataTypes); // Create uploads folder if it does not exist const uploadDir = path.join(__dirname, "../../public/uploads"); @@ -45,7 +47,9 @@ module.exports = function (sequelize) { const upload = multer({ storage }); router.post("/", upload.array("files", 10), async (req, res) => { + const t = await sequelize.transaction(); try { + const caseId = req.query.parentCaseId; const files = req.files; if (files.length === 0) { return res.status(400).json({ error: "No files uploaded" }); @@ -58,10 +62,19 @@ module.exports = function (sequelize) { path: `${protocol}://${host}/uploads/${file.filename}`, })); - const newAttachments = await Attachment.bulkCreate(attachmentsData); + const newAttachments = await Attachment.bulkCreate(attachmentsData, { + transaction: t, + }); + const caseAttachmentsData = newAttachments.map((attachment) => ({ + caseId: caseId, + attachmentId: attachment.id, + })); + await CaseAttachment.bulkCreate(caseAttachmentsData, { transaction: t }); + await t.commit(); res.json(newAttachments); } catch (error) { + await t.rollback(); res.status(500).json({ error: "Internal server error" }); } }); diff --git a/frontend/app/projects/[projectId]/folders/[folderId]/cases/[caseId]/attachments-editor.tsx b/frontend/app/projects/[projectId]/folders/[folderId]/cases/[caseId]/attachments-editor.tsx index 8b3aa04..6d0b970 100644 --- a/frontend/app/projects/[projectId]/folders/[folderId]/cases/[caseId]/attachments-editor.tsx +++ b/frontend/app/projects/[projectId]/folders/[folderId]/cases/[caseId]/attachments-editor.tsx @@ -11,18 +11,60 @@ export default function AttachmentsEditor({ attachments = [], onAttachmentDelete, }: Props) { + let images = []; + let others = []; + + attachments.forEach((attachment) => { + let path = attachment.path; + let extension = path.substring(path.lastIndexOf(".") + 1).toLowerCase(); + if ( + extension === "png" || + extension === "jpg" || + extension === "jpeg" || + extension === "gif" || + extension === "bmp" || + extension === "svg" + ) { + images.push(attachment); + } else { + others.push(attachment); + } + }); return ( <> - {attachments.map((attachment, index) => ( + {images.map((image, index) => (
- {attachment.title} + {image.title} +
+ + + +
+
+ ))} + + {others.map((file, index) => ( +
+
{file.title}
+
{file.path}
diff --git a/frontend/app/projects/[projectId]/folders/[folderId]/cases/[caseId]/page.tsx b/frontend/app/projects/[projectId]/folders/[folderId]/cases/[caseId]/page.tsx index 2f198b8..0499cef 100644 --- a/frontend/app/projects/[projectId]/folders/[folderId]/cases/[caseId]/page.tsx +++ b/frontend/app/projects/[projectId]/folders/[folderId]/cases/[caseId]/page.tsx @@ -245,15 +245,23 @@ export default function Page({ }); }; - const handleFileUpload = async (event) => { - const files = event.target.files; + const handleDrop = (event) => { + event.preventDefault(); + handleFileUpload(event.dataTransfer.files); + }; + + const handleInput = (event) => { + handleFileUpload(event.target.files); + }; + + const handleFileUpload = async (files) => { try { const formData = new FormData(); for (let i = 0; i < files.length; i++) { formData.append("files", files[i]); } - const url = `${apiServer}/attachments`; + const url = `${apiServer}/attachments?parentCaseId=${params.caseId}`; const response = await fetch(url, { method: "POST", body: formData, @@ -284,6 +292,21 @@ export default function Page({ return (
+
+ +
Basic
console.log(id)} /> -
+
event.preventDefault()} + >
- -
- -
); }