2024-08-16 07:19:40 +00:00
|
|
|
import multer from "multer";
|
|
|
|
|
import crypto from "crypto";
|
|
|
|
|
import path from "path";
|
|
|
|
|
import fs from "fs";
|
|
|
|
|
import response from "../response.js";
|
|
|
|
|
|
|
|
|
|
const memoryStorage = multer.memoryStorage();
|
|
|
|
|
|
|
|
|
|
const fileFilter = (req, file, cb) => {
|
|
|
|
|
const ext = path.extname(file.originalname).toLowerCase();
|
|
|
|
|
if (ext === ".png" || ext === ".jpg" || ext === ".jpeg") {
|
|
|
|
|
cb(null, true);
|
|
|
|
|
} else {
|
|
|
|
|
cb(
|
|
|
|
|
new Error(
|
|
|
|
|
"Invalid file type, only .png, .jpg, and .jpeg files are allowed!"
|
|
|
|
|
),
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const upload = multer({
|
|
|
|
|
storage: memoryStorage,
|
|
|
|
|
fileFilter,
|
2024-09-13 13:03:35 +00:00
|
|
|
limits: { fileSize: 5 * 1024 * 1024 },
|
|
|
|
|
}).fields([{ name: "thumbnail", maxCount: 1 }]);
|
2024-08-16 07:19:40 +00:00
|
|
|
|
|
|
|
|
const handleUpload = (req, res, next) => {
|
|
|
|
|
upload(req, res, (err) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
return response(400, null, err.message, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const files = req.files;
|
|
|
|
|
const thumbnail = files?.thumbnail ? files.thumbnail[0] : null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
let validFiles = true;
|
|
|
|
|
let errorMessages = [];
|
|
|
|
|
|
|
|
|
|
if (thumbnail && thumbnail.size > 5 * 1024 * 1024) {
|
|
|
|
|
validFiles = false;
|
|
|
|
|
thumbnail.buffer = null;
|
|
|
|
|
errorMessages.push("Thumbnail file exceeds the size limit of 5MB");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (validFiles) {
|
2024-09-13 13:03:35 +00:00
|
|
|
req.filesToSave = { thumbnail };
|
2024-08-16 07:19:40 +00:00
|
|
|
next();
|
|
|
|
|
} else {
|
2024-09-13 13:03:35 +00:00
|
|
|
clearFileBuffers({ thumbnail });
|
2024-08-16 07:19:40 +00:00
|
|
|
return response(400, null, errorMessages.join(", "), res);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
2024-09-13 13:03:35 +00:00
|
|
|
clearFileBuffers({ thumbnail });
|
2024-08-16 07:19:40 +00:00
|
|
|
return response(500, null, "Internal Server Error", res);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const clearFileBuffers = (files) => {
|
|
|
|
|
for (const file of Object.values(files)) {
|
|
|
|
|
if (file && file.buffer) {
|
|
|
|
|
file.buffer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-13 13:03:35 +00:00
|
|
|
export const saveFileToDisk = (file, fieldName, idSection) => {
|
2024-08-16 07:19:40 +00:00
|
|
|
const ext = path.extname(file.originalname);
|
|
|
|
|
const hash = crypto
|
|
|
|
|
.createHash("md5")
|
2024-09-13 13:03:35 +00:00
|
|
|
.update(file.originalname + file.buffer.length.toString())
|
|
|
|
|
.digest("hex");
|
|
|
|
|
const filename = `${fieldName}-${idSection}-${hash}${ext}`;
|
|
|
|
|
const folderPath = path.join("public/uploads/section");
|
2024-08-16 07:19:40 +00:00
|
|
|
|
|
|
|
|
if (!fs.existsSync(folderPath)) {
|
|
|
|
|
fs.mkdirSync(folderPath, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const filepath = path.join(folderPath, filename);
|
|
|
|
|
fs.writeFileSync(filepath, file.buffer);
|
|
|
|
|
return filename;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default handleUpload;
|