diff --git a/backend/routes/attachments/delete.js b/backend/routes/attachments/delete.js index 668800a..0a03258 100644 --- a/backend/routes/attachments/delete.js +++ b/backend/routes/attachments/delete.js @@ -20,11 +20,10 @@ module.exports = function (sequelize) { } // delete file from folder - const uploadDir = path.join(__dirname, "../../public"); + const uploadDir = path.join(__dirname, "../../public/uploads"); const url = attachment.path; - const parts = url.split('/'); - const fileNameWithFolder = parts.slice(-2).join('/'); - const filePath = path.join(uploadDir, fileNameWithFolder); + const fileName = url.substring(url.lastIndexOf("/") + 1); + const filePath = path.join(uploadDir, fileName); fs.unlink(filePath, (err) => { if (err) { console.error('Error deleting file:', err); diff --git a/backend/seeders/seed.js b/backend/seeders/seed.js index dc50257..badb5ca 100644 --- a/backend/seeders/seed.js +++ b/backend/seeders/seed.js @@ -1,4 +1,6 @@ "use strict"; +const path = require("path"); +const fs = require("fs"); module.exports = { up: async (queryInterface, Sequelize) => { @@ -179,19 +181,46 @@ module.exports = { { title: "Selenium logo", detail: "", - path: "http://localhost:3001/sample/861px-Selenium_Logo.png", + path: "http://localhost:3001/uploads/861px-Selenium_Logo.png", createdAt: new Date(), updatedAt: new Date(), }, { title: "vitest logo", detail: "", - path: "http://localhost:3001/sample/logo-shadow.svg", + path: "http://localhost:3001/uploads/logo-shadow.svg", createdAt: new Date(), updatedAt: new Date(), - } + }, ]); + // copy sample files to uploads folder + const sampleFolderPath = "public/sample"; + const uploadsFolderPath = "public/uploads"; + const SeleniumLogoFileName = "861px-Selenium_Logo.png"; + const vitestLogoFileName = "logo-shadow.svg"; + if (!fs.existsSync(uploadsFolderPath)) { + fs.mkdirSync(uploadsFolderPath, { recursive: true }); + } + fs.copyFile( + `${sampleFolderPath}/${SeleniumLogoFileName}`, + `${uploadsFolderPath}/${SeleniumLogoFileName}`, + (err) => { + if (err) { + console.log(err); + } + } + ); + fs.copyFile( + `${sampleFolderPath}/${vitestLogoFileName}`, + `${uploadsFolderPath}/${vitestLogoFileName}`, + (err) => { + if (err) { + console.log(err); + } + } + ); + await queryInterface.bulkInsert("caseAttachments", [ { caseId: 1, @@ -204,7 +233,7 @@ module.exports = { attachmentId: 2, createdAt: new Date(), updatedAt: new Date(), - } + }, ]); },