fix: cannot upload attachment files in docker (#241)
This commit is contained in:
3
backend/.gitignore
vendored
3
backend/.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
node_modules/
|
||||
.env
|
||||
public/uploads/
|
||||
public/uploads/*
|
||||
!public/uploads/dummy.txt
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
up: async (queryInterface) => {
|
||||
// Rename column 'path' to 'filename' in 'Attachments' table
|
||||
await queryInterface.renameColumn('Attachments', 'path', 'filename');
|
||||
},
|
||||
|
||||
down: async (queryInterface) => {
|
||||
// Revert column name from 'filename' back to 'path'
|
||||
await queryInterface.renameColumn('Attachments', 'filename', 'path');
|
||||
},
|
||||
};
|
||||
@@ -8,7 +8,7 @@ function defineAttachment(sequelize, DataTypes) {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
},
|
||||
path: {
|
||||
filename: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
|
||||
0
backend/public/uploads/dummy.txt
Normal file
0
backend/public/uploads/dummy.txt
Normal file
@@ -1,9 +1,9 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
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');
|
||||
const defineAttachment = require('../../models/attachments');
|
||||
|
||||
module.exports = function (sequelize) {
|
||||
const Attachment = defineAttachment(sequelize, DataTypes);
|
||||
@@ -21,9 +21,7 @@ module.exports = function (sequelize) {
|
||||
|
||||
// delete file from folder
|
||||
const uploadDir = path.join(__dirname, '../../public/uploads');
|
||||
const url = attachment.path;
|
||||
const fileName = url.substring(url.lastIndexOf('/') + 1);
|
||||
const filePath = path.join(uploadDir, fileName);
|
||||
const filePath = path.join(uploadDir, attachment.filename);
|
||||
fs.unlink(filePath, (err) => {
|
||||
if (err) {
|
||||
console.error('Error deleting file:', err);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
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');
|
||||
const defineAttachment = require('../../models/attachments');
|
||||
|
||||
module.exports = function (sequelize) {
|
||||
const Attachment = defineAttachment(sequelize, DataTypes);
|
||||
@@ -16,8 +16,7 @@ module.exports = function (sequelize) {
|
||||
return res.status(404).send('Attachment not found');
|
||||
}
|
||||
|
||||
const filename = attachment.path.split('/').pop();
|
||||
const filePath = path.join(__dirname, `../../public/uploads/${filename}`);
|
||||
const filePath = path.join(__dirname, `../../public/uploads/${attachment.filename}`);
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return res.status(404).json({ error: 'File not found' });
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const multer = require('multer');
|
||||
const { DataTypes } = require('sequelize');
|
||||
const defineAttachment = require('../../models/attachments');
|
||||
const defineCaseAttachment = require('../../models/caseAttachments');
|
||||
const { DataTypes } = require('sequelize');
|
||||
|
||||
module.exports = function (sequelize) {
|
||||
const Attachment = defineAttachment(sequelize, DataTypes);
|
||||
@@ -55,11 +55,9 @@ module.exports = function (sequelize) {
|
||||
return res.status(400).json({ error: 'No files uploaded' });
|
||||
}
|
||||
|
||||
const host = req.get('host');
|
||||
const protocol = req.protocol;
|
||||
const attachmentsData = files.map((file) => ({
|
||||
title: file.originalname,
|
||||
path: `${protocol}://${host}/uploads/${file.filename}`,
|
||||
filename: file.filename,
|
||||
}));
|
||||
|
||||
const newAttachments = await Attachment.bulkCreate(attachmentsData, {
|
||||
@@ -74,6 +72,7 @@ module.exports = function (sequelize) {
|
||||
await t.commit();
|
||||
res.json(newAttachments);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
await t.rollback();
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
|
||||
@@ -550,19 +550,18 @@ module.exports = {
|
||||
},
|
||||
]);
|
||||
|
||||
const backendOrigin = process.env.BACKEND_ORIGIN || 'http://localhost:8001';
|
||||
await queryInterface.bulkInsert('attachments', [
|
||||
{
|
||||
title: 'Selenium logo',
|
||||
detail: '',
|
||||
path: `${backendOrigin}/uploads/861px-Selenium_Logo.png`,
|
||||
filename: '861px-Selenium_Logo.png',
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
{
|
||||
title: 'vitest logo',
|
||||
detail: '',
|
||||
path: `${backendOrigin}/uploads/logo-shadow.svg`,
|
||||
filename: 'logo-shadow.svg',
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user