This commit is contained in:
@@ -10,12 +10,88 @@ 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({
|
||||
@@ -34,11 +110,20 @@ const uploadFiles = async (req, res) => {
|
||||
};
|
||||
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: req.params.name}).toArray());
|
||||
const cursor = await (images.find({filename: fileName}).toArray());
|
||||
if (cursor.length === 0) {
|
||||
return res.status(500).send({
|
||||
message: "No files found!",
|
||||
@@ -55,14 +140,21 @@ const getFile = async (req, res) => {
|
||||
|
||||
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: req.body.name}).toArray());
|
||||
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: req.body.name});
|
||||
await images.deleteMany({filename: targetName});
|
||||
}
|
||||
return res.status(200).send({ message: 'Delete Image Succeed'});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user