Introduce prettier
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
const express = require("express");
|
||||
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 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) => {
|
||||
router.delete('/:attachmentId', async (req, res) => {
|
||||
const attachmentId = req.params.attachmentId;
|
||||
const t = await sequelize.transaction();
|
||||
|
||||
@@ -16,19 +16,19 @@ module.exports = function (sequelize) {
|
||||
const attachment = await Attachment.findByPk(attachmentId);
|
||||
if (!attachment) {
|
||||
await t.rollback();
|
||||
return res.status(404).send("Attachment not found");
|
||||
return res.status(404).send('Attachment not found');
|
||||
}
|
||||
|
||||
// delete file from folder
|
||||
const uploadDir = path.join(__dirname, "../../public/uploads");
|
||||
const uploadDir = path.join(__dirname, '../../public/uploads');
|
||||
const url = attachment.path;
|
||||
const fileName = url.substring(url.lastIndexOf("/") + 1);
|
||||
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);
|
||||
t.rollback();
|
||||
return res.status(404).send("Attachment not found");
|
||||
return res.status(404).send('Attachment not found');
|
||||
}
|
||||
});
|
||||
|
||||
@@ -38,7 +38,7 @@ module.exports = function (sequelize) {
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
await t.rollback();
|
||||
res.status(500).send("Internal Server Error");
|
||||
res.status(500).send('Internal Server Error');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
const express = require("express");
|
||||
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 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.get("/download/:attachmentId", async (req, res) => {
|
||||
router.get('/download/:attachmentId', async (req, res) => {
|
||||
const attachmentId = req.params.attachmentId;
|
||||
|
||||
try {
|
||||
const attachment = await Attachment.findByPk(attachmentId);
|
||||
if (!attachment) {
|
||||
return res.status(404).send("Attachment not found");
|
||||
return res.status(404).send('Attachment not found');
|
||||
}
|
||||
|
||||
const filename = attachment.path.split("/").pop();
|
||||
const filename = attachment.path.split('/').pop();
|
||||
const filePath = path.join(__dirname, `../../public/uploads/${filename}`);
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return res.status(404).json({ error: "File not found" });
|
||||
return res.status(404).json({ error: 'File not found' });
|
||||
}
|
||||
|
||||
res.download(filePath);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send("Internal Server Error");
|
||||
res.status(500).send('Internal Server Error');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
const express = require("express");
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
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");
|
||||
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");
|
||||
const uploadDir = path.join(__dirname, '../../public/uploads');
|
||||
if (!fs.existsSync(uploadDir)) {
|
||||
fs.mkdirSync(uploadDir, { recursive: true });
|
||||
}
|
||||
@@ -46,16 +46,16 @@ module.exports = function (sequelize) {
|
||||
|
||||
const upload = multer({ storage });
|
||||
|
||||
router.post("/", upload.array("files", 10), async (req, res) => {
|
||||
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" });
|
||||
return res.status(400).json({ error: 'No files uploaded' });
|
||||
}
|
||||
|
||||
const host = req.get("host");
|
||||
const host = req.get('host');
|
||||
const protocol = req.protocol;
|
||||
const attachmentsData = files.map((file) => ({
|
||||
title: file.originalname,
|
||||
@@ -75,7 +75,7 @@ module.exports = function (sequelize) {
|
||||
res.json(newAttachments);
|
||||
} catch (error) {
|
||||
await t.rollback();
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user