All checks were successful
Build And Deploy Main / build-and-deploy (push) Successful in 33s
278 lines
8.6 KiB
JavaScript
278 lines
8.6 KiB
JavaScript
const upload = require("../middlewares/upload");
|
|
// const uploadPhysical = require("../middlewares/upload_physical");
|
|
const dbConfig = require("../config/db.config");
|
|
const MongoClient = require("mongodb").MongoClient;
|
|
const GridFSBucket = require("mongodb").GridFSBucket;
|
|
const util = require("util");
|
|
var fs = require('fs');
|
|
var multer = require('multer');
|
|
const path = require("path");
|
|
const url = dbConfig.url;
|
|
const baseUrl = dbConfig.fileUrl;
|
|
const mongoClient = new MongoClient(url);
|
|
const BASE_UPLOAD_DIR = `/www/wwwroot/upload/`;
|
|
const DRIVER_SIGNATURE_DIR = path.join(BASE_UPLOAD_DIR, 'driver', 'sig');
|
|
const uploadToMemory = multer({
|
|
storage: multer.memoryStorage(),
|
|
limits: { fileSize: 10 * 1024 * 1024 }
|
|
}).single("file");
|
|
const uploadToMemoryAsync = util.promisify(uploadToMemory);
|
|
|
|
const readdir = util.promisify(fs.readdir);
|
|
const stat = util.promisify(fs.stat);
|
|
|
|
const isDriverSignatureFileName = (name = '') => {
|
|
const safeName = `${name || ''}`.trim();
|
|
return /^.+_.+_\d{2}_\d{2}$/.test(safeName);
|
|
};
|
|
|
|
const getDriverSignatureYear = (req) => {
|
|
const queryYear = `${req?.query?.year || ''}`.trim();
|
|
if (/^\d{4}$/.test(queryYear)) {
|
|
return queryYear;
|
|
}
|
|
return `${new Date().getFullYear()}`;
|
|
};
|
|
|
|
const resolveSignatureExt = (mimetype = '', originalName = '') => {
|
|
const safeMimetype = `${mimetype || ''}`.toLowerCase();
|
|
if (safeMimetype.includes('png')) return '.png';
|
|
if (safeMimetype.includes('jpeg') || safeMimetype.includes('jpg')) return '.jpg';
|
|
const ext = path.extname(`${originalName || ''}`.toLowerCase());
|
|
if (ext === '.png' || ext === '.jpg' || ext === '.jpeg') return ext;
|
|
return '.jpg';
|
|
};
|
|
|
|
const findDriverSignatureFilePath = async (fileBaseName = '') => {
|
|
const safeName = `${fileBaseName || ''}`.trim();
|
|
if (!safeName) return null;
|
|
if (!fs.existsSync(DRIVER_SIGNATURE_DIR)) return null;
|
|
|
|
const yearFolders = await readdir(DRIVER_SIGNATURE_DIR, { withFileTypes: true });
|
|
const yearDirs = yearFolders.filter((entry) => entry.isDirectory()).map((entry) => path.join(DRIVER_SIGNATURE_DIR, entry.name));
|
|
for (const yearDir of yearDirs) {
|
|
const candidates = [
|
|
path.join(yearDir, safeName),
|
|
path.join(yearDir, `${safeName}.jpg`),
|
|
path.join(yearDir, `${safeName}.jpeg`),
|
|
path.join(yearDir, `${safeName}.png`)
|
|
];
|
|
for (const candidate of candidates) {
|
|
if (fs.existsSync(candidate)) {
|
|
return candidate;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
};
|
|
|
|
const uploadFiles = async (req, res) => {
|
|
try {
|
|
const fileName = `${req?.params?.filename || ''}`.trim();
|
|
if (!fileName) {
|
|
return res.status(400).send({ message: "Invalid filename." });
|
|
}
|
|
// Driver signature upload now stores to shared server files.
|
|
if (isDriverSignatureFileName(fileName)) {
|
|
await uploadToMemoryAsync(req, res);
|
|
if (!req.file) {
|
|
return res.status(400).send({ message: "You must select a file." });
|
|
}
|
|
const year = getDriverSignatureYear(req);
|
|
const targetDir = path.join(DRIVER_SIGNATURE_DIR, year);
|
|
if (!fs.existsSync(targetDir)) {
|
|
fs.mkdirSync(targetDir, { recursive: true });
|
|
}
|
|
const ext = resolveSignatureExt(req.file?.mimetype, req.file?.originalname);
|
|
const targetFilePath = path.join(targetDir, `${fileName}${ext}`);
|
|
fs.writeFileSync(targetFilePath, req.file.buffer);
|
|
return res.send({
|
|
message: "Driver signature has been uploaded.",
|
|
path: targetFilePath
|
|
});
|
|
}
|
|
|
|
await upload(req, res);
|
|
if (req.file == undefined) {
|
|
return res.send({
|
|
message: "You must select a file.",
|
|
});
|
|
}
|
|
return res.send({
|
|
message: "File has been uploaded.",
|
|
});
|
|
} catch (error) {
|
|
console.log(error);
|
|
return res.send({
|
|
message: "Error when trying upload image: ${error}",
|
|
});
|
|
}
|
|
};
|
|
const getFile = async (req, res) => {
|
|
try {
|
|
const fileName = decodeURIComponent(`${req?.params?.name || ''}`.trim());
|
|
if (isDriverSignatureFileName(fileName)) {
|
|
const signaturePath = await findDriverSignatureFilePath(fileName);
|
|
if (signaturePath) {
|
|
const fileBuffer = await fs.promises.readFile(signaturePath);
|
|
return res.status(200).send(fileBuffer.toString('base64'));
|
|
}
|
|
}
|
|
|
|
await mongoClient.connect();
|
|
const database = mongoClient.db(dbConfig.database);
|
|
const images = database.collection(dbConfig.imgBucket + ".files");
|
|
const chunks = database.collection(dbConfig.imgBucket + ".chunks");
|
|
const cursor = await (images.find({filename: fileName}).toArray());
|
|
if (cursor.length === 0) {
|
|
return res.status(500).send({
|
|
message: "No files found!",
|
|
});
|
|
}
|
|
const chunkCursor = await(chunks.find({files_id: cursor[cursor.length-1]._id}).toArray());
|
|
return res.status(200).send(chunkCursor[0].data);
|
|
} catch (error) {
|
|
return res.status(500).send({
|
|
message: error.message,
|
|
});
|
|
}
|
|
};
|
|
|
|
const deleteFile = async (req, res) => {
|
|
try {
|
|
const targetName = `${req?.body?.name || ''}`.trim();
|
|
if (isDriverSignatureFileName(targetName)) {
|
|
const signaturePath = await findDriverSignatureFilePath(targetName);
|
|
if (signaturePath && fs.existsSync(signaturePath)) {
|
|
await fs.promises.unlink(signaturePath);
|
|
}
|
|
}
|
|
await mongoClient.connect();
|
|
const database = mongoClient.db(dbConfig.database);
|
|
const images = database.collection(dbConfig.imgBucket + ".files");
|
|
const chunks = database.collection(dbConfig.imgBucket + ".chunks");
|
|
const cursor = await (images.find({filename: targetName}).toArray());
|
|
if (cursor.length > 0) {
|
|
await chunks.deleteMany({files_id: cursor[cursor.length-1]._id});
|
|
await images.deleteMany({filename: targetName});
|
|
}
|
|
return res.status(200).send({ message: 'Delete Image Succeed'});
|
|
} catch (error) {
|
|
return res.status(500).send({
|
|
message: error.message,
|
|
});
|
|
}
|
|
};
|
|
|
|
const uploadPhysicalFile = async (req, res) => {
|
|
try {
|
|
// Destination and file name would be adjusted to be the same with original file system
|
|
const {objectId, name, fileType, model} = req.query;
|
|
const uploadedFile = req.file;
|
|
if (!uploadedFile) {
|
|
return res.status(400).send({
|
|
message: 'No file uploaded.'
|
|
})
|
|
}
|
|
return res.status(200).send({
|
|
message: `File ${fileType} Uploaded Successfully for ${model} ${objectId}-${name}`
|
|
})
|
|
|
|
} catch (error) {
|
|
return res.status(500).send({
|
|
message: error.message,
|
|
});
|
|
}
|
|
}
|
|
|
|
const deletePhysicalFile = async (req, res) => {
|
|
try {
|
|
const { objectId, fileType, model, fileName } = req.body || {};
|
|
if (!objectId || !fileType || !model || !fileName) {
|
|
return res.status(400).send({ message: 'Required fields missed' });
|
|
}
|
|
|
|
const BASE_UPLOAD_DIR = `/www/wwwroot/upload/`;
|
|
const targetFilePath = path.join(BASE_UPLOAD_DIR, model, objectId, fileType, fileName);
|
|
|
|
if (!fs.existsSync(targetFilePath)) {
|
|
return res.status(200).send({ message: 'File already removed.' });
|
|
}
|
|
|
|
await fs.promises.unlink(targetFilePath);
|
|
return res.status(200).send({ message: 'File deleted successfully.' });
|
|
} catch (error) {
|
|
return res.status(500).send({
|
|
message: error.message,
|
|
});
|
|
}
|
|
}
|
|
|
|
const getFilesByType = async (req, res) => {
|
|
try {
|
|
const {objectId, fileType, name, model} = req.params;
|
|
|
|
if (!objectId || !name || !fileType || !model) {
|
|
return res.status(400).send({message: 'Required fields missed'});
|
|
}
|
|
|
|
const BASE_UPLOAD_DIR = `/www/wwwroot/upload/`;
|
|
const filesDir = `${BASE_UPLOAD_DIR}${model}/${objectId}/${fileType}/`;
|
|
|
|
if (!fs.existsSync(filesDir)) {
|
|
return res.status(200).send({
|
|
data: {
|
|
objectId,
|
|
model,
|
|
fileType,
|
|
name,
|
|
files: [],
|
|
count: 0
|
|
}
|
|
})
|
|
}
|
|
|
|
const fileEntries = await readdir(filesDir, { withFileTypes: true});
|
|
|
|
const filePromises = fileEntries.filter(entry => entry.isFile()).map(async (entry) => {
|
|
const filePath = path.join(filesDir, entry.name);
|
|
const stats = await stat(filePath);
|
|
|
|
const fileUrl = `/files/${model}/${objectId}/${fileType}/${entry.name}`;
|
|
return {
|
|
name: entry.name,
|
|
url: fileUrl,
|
|
size: stats.size,
|
|
extension: path.extname(entry.name),
|
|
createdAt: stats.birthtime,
|
|
modifiedAt: stats.mtime
|
|
}
|
|
});
|
|
const files = await Promise.all(filePromises);
|
|
return res.status(200).json({
|
|
data: {
|
|
objectId,
|
|
fileType,
|
|
name,
|
|
files,
|
|
count: files.length
|
|
}
|
|
})
|
|
|
|
} catch(err) {
|
|
return res.status(500).send({
|
|
message: err.message
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
uploadFiles,
|
|
getFile,
|
|
deleteFile,
|
|
uploadPhysicalFile,
|
|
deletePhysicalFile,
|
|
getFilesByType
|
|
}; |