ระบบ uploads
This commit is contained in:
84
exthernal-ttc-api/src/middlewares/uploadMiddleware.js
Normal file
84
exthernal-ttc-api/src/middlewares/uploadMiddleware.js
Normal file
@@ -0,0 +1,84 @@
|
||||
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()
|
||||
})
|
||||
}
|
||||
BIN
exthernal-ttc-api/uploads/projects/1764504521158-123821263.docx
Normal file
BIN
exthernal-ttc-api/uploads/projects/1764504521158-123821263.docx
Normal file
Binary file not shown.
BIN
exthernal-ttc-api/uploads/projects/1764504584000-133438404.docx
Normal file
BIN
exthernal-ttc-api/uploads/projects/1764504584000-133438404.docx
Normal file
Binary file not shown.
BIN
exthernal-ttc-api/uploads/projects/1764504589491-532615394.docx
Normal file
BIN
exthernal-ttc-api/uploads/projects/1764504589491-532615394.docx
Normal file
Binary file not shown.
BIN
exthernal-ttc-api/uploads/projects/1764504605122-350813611.docx
Normal file
BIN
exthernal-ttc-api/uploads/projects/1764504605122-350813611.docx
Normal file
Binary file not shown.
BIN
exthernal-ttc-api/uploads/projects/1764504623958-252845613.docx
Normal file
BIN
exthernal-ttc-api/uploads/projects/1764504623958-252845613.docx
Normal file
Binary file not shown.
BIN
exthernal-ttc-api/uploads/projects/1764504649758-558938587.docx
Normal file
BIN
exthernal-ttc-api/uploads/projects/1764504649758-558938587.docx
Normal file
Binary file not shown.
BIN
exthernal-ttc-api/uploads/projects/1764504665734-82185672.docx
Normal file
BIN
exthernal-ttc-api/uploads/projects/1764504665734-82185672.docx
Normal file
Binary file not shown.
BIN
exthernal-ttc-api/uploads/projects/1764504705776-32739561.docx
Normal file
BIN
exthernal-ttc-api/uploads/projects/1764504705776-32739561.docx
Normal file
Binary file not shown.
BIN
exthernal-ttc-api/uploads/projects/1764504713709-764652284.docx
Normal file
BIN
exthernal-ttc-api/uploads/projects/1764504713709-764652284.docx
Normal file
Binary file not shown.
BIN
exthernal-ttc-api/uploads/projects/1764504763972-112141709.docx
Normal file
BIN
exthernal-ttc-api/uploads/projects/1764504763972-112141709.docx
Normal file
Binary file not shown.
BIN
exthernal-ttc-api/uploads/projects/1764504878090-18492765.docx
Normal file
BIN
exthernal-ttc-api/uploads/projects/1764504878090-18492765.docx
Normal file
Binary file not shown.
Reference in New Issue
Block a user