forked from ttc/micro-service-api
84 lines
2.8 KiB
JavaScript
84 lines
2.8 KiB
JavaScript
|
|
import multer from 'multer'
|
||
|
|
import path from 'path'
|
||
|
|
import fs from 'fs'
|
||
|
|
import { sendError } from '../utils/response.js'
|
||
|
|
|
||
|
|
const tempDir = 'uploads/temp'
|
||
|
|
if (!fs.existsSync(tempDir)) {
|
||
|
|
fs.mkdirSync(tempDir, { recursive: true })
|
||
|
|
}
|
||
|
|
|
||
|
|
const storage = multer.diskStorage({
|
||
|
|
destination: function (req, file, cb) {
|
||
|
|
cb(null, tempDir)
|
||
|
|
},
|
||
|
|
filename: function (req, file, cb) {
|
||
|
|
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
|
||
|
|
cb(null, uniqueSuffix + path.extname(file.originalname))
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
const fileFilter = (req, file, cb) => {
|
||
|
|
const allowedMimes = [
|
||
|
|
'image/jpeg', 'image/png', 'image/jpg',
|
||
|
|
'application/pdf',
|
||
|
|
'application/msword',
|
||
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
||
|
|
]
|
||
|
|
if (allowedMimes.includes(file.mimetype)) {
|
||
|
|
cb(null, true)
|
||
|
|
} else {
|
||
|
|
cb(new Error('รองรับเฉพาะไฟล์รูปภาพ, PDF หรือเอกสาร Word เท่านั้น'), false)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const upload = multer({
|
||
|
|
storage: storage,
|
||
|
|
fileFilter: fileFilter,
|
||
|
|
limits: { fileSize: 10 * 1024 * 1024 }
|
||
|
|
})
|
||
|
|
|
||
|
|
function verifyFileSignature(filePath) {
|
||
|
|
try {
|
||
|
|
const buffer = Buffer.alloc(8)
|
||
|
|
const fd = fs.openSync(filePath, 'r')
|
||
|
|
fs.readSync(fd, buffer, 0, 8, 0)
|
||
|
|
fs.closeSync(fd)
|
||
|
|
const hex = buffer.toString('hex').toUpperCase()
|
||
|
|
|
||
|
|
if (hex.startsWith('FFD8FF')) return true // JPG
|
||
|
|
if (hex.startsWith('89504E47')) return true // PNG
|
||
|
|
if (hex.startsWith('25504446')) return true // PDF
|
||
|
|
if (hex.startsWith('D0CF11E0')) return true // DOC
|
||
|
|
if (hex.startsWith('504B0304')) return true // DOCX
|
||
|
|
|
||
|
|
return false
|
||
|
|
} catch (err) {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export const uploadMiddleware = (req, res, next) => {
|
||
|
|
// เปลี่ยนเป็น .array() เพื่อรับหลายไฟล์ (รองรับสูงสุด 10 ไฟล์ต่อครั้ง)
|
||
|
|
const uploadHandler = upload.array('prjdoc', 10)
|
||
|
|
|
||
|
|
uploadHandler(req, res, (err) => {
|
||
|
|
if (err) return res.status(400).json(sendError(err.message))
|
||
|
|
|
||
|
|
// ถ้าไม่มีไฟล์ ข้ามไป
|
||
|
|
if (!req.files || req.files.length === 0) return next()
|
||
|
|
|
||
|
|
// Loop ตรวจสอบ Signature ทุกไฟล์
|
||
|
|
for (const file of req.files) {
|
||
|
|
const isSafe = verifyFileSignature(file.path)
|
||
|
|
if (!isSafe) {
|
||
|
|
// ลบไฟล์ทั้งหมดทิ้งทันทีถ้าเจอไฟล์อันตรายแม้แต่ไฟล์เดียว
|
||
|
|
req.files.forEach(f => {
|
||
|
|
if (fs.existsSync(f.path)) fs.unlinkSync(f.path)
|
||
|
|
})
|
||
|
|
return res.status(400).json(sendError(`ไฟล์ ${file.originalname} ไม่ปลอดภัย (Invalid Signature)`))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
next()
|
||
|
|
})
|
||
|
|
}
|