forked from UKSOURCE/cms.hailearning.edu.vn
first commit
This commit is contained in:
17
middleware/apiKey.js
Normal file
17
middleware/apiKey.js
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* API Key middleware
|
||||
* Validates the api_key query parameter against process.env.API_KEY
|
||||
* Spec: GET /api/verify-degree/{id}?api_key={API_KEY}
|
||||
*/
|
||||
|
||||
function validateApiKey(req, res, next) {
|
||||
const apiKey = req.query.api_key;
|
||||
|
||||
if (!apiKey || apiKey !== process.env.API_KEY) {
|
||||
return res.status(401).json({ error: 'Unauthorized - Invalid API key' });
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = { validateApiKey };
|
||||
@@ -155,8 +155,44 @@ async function convertToWebp(req, res, next) {
|
||||
}
|
||||
|
||||
|
||||
// Storage cho degree images — lưu ngoài public/ để không serve trực tiếp
|
||||
const degreeStorage = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
const uploadPath = path.join(__dirname, '../private/uploads/degree');
|
||||
if (!fs.existsSync(uploadPath)) {
|
||||
fs.mkdirSync(uploadPath, { recursive: true });
|
||||
}
|
||||
cb(null, uploadPath);
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
const ext = path.extname(file.originalname);
|
||||
cb(null, `degree-${Date.now()}-${Math.round(Math.random() * 1e9)}${ext}`);
|
||||
}
|
||||
});
|
||||
|
||||
// Lọc file chỉ cho phép ảnh degree
|
||||
const degreeFileFilter = (req, file, cb) => {
|
||||
const allowedMimes = ['image/jpeg', 'image/png', 'image/webp'];
|
||||
if (allowedMimes.includes(file.mimetype)) {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(new Error('Only image/jpeg, image/png, image/webp files are allowed!'));
|
||||
}
|
||||
};
|
||||
|
||||
// Cấu hình upload degree
|
||||
const uploadDegree = multer({
|
||||
storage: degreeStorage,
|
||||
limits: { fileSize: 5 * 1024 * 1024 }, // 5MB per file
|
||||
fileFilter: degreeFileFilter
|
||||
}).fields([
|
||||
{ name: 'degree_image', maxCount: 1 },
|
||||
{ name: 'certificate_image', maxCount: 1 }
|
||||
]);
|
||||
|
||||
module.exports = {
|
||||
upload,
|
||||
uploadVideo,
|
||||
convertToWebp
|
||||
};
|
||||
convertToWebp,
|
||||
uploadDegree
|
||||
};
|
||||
Reference in New Issue
Block a user