Files
tk-factory-services/user-management/api/middleware/upload.js
Hyungi Ahn 3623551a6b feat(purchase): 생산소모품 구매 관리 시스템 구현
tkuser: 업체(공급업체) CRUD + 소모품 마스터 CRUD (사진 업로드 포함)
tkfb: 구매신청 → 구매 처리 → 월간 분석/정산 전체 워크플로
설비(equipment) 분류 구매 시 자동 등록 + 실패 시 admin 알림

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 21:21:59 +09:00

59 lines
1.6 KiB
JavaScript

/**
* 파일 업로드 미들웨어 (multer)
*/
const multer = require('multer');
const path = require('path');
const crypto = require('crypto');
const fs = require('fs');
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path.join(__dirname, '..', 'uploads'));
},
filename: (req, file, cb) => {
const ext = path.extname(file.originalname).toLowerCase();
const uniqueName = `workplace-layout-${Date.now()}-${crypto.randomInt(100000000, 999999999)}${ext}`;
cb(null, uniqueName);
}
});
const fileFilter = (req, file, cb) => {
const allowed = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
if (allowed.includes(file.mimetype)) {
cb(null, true);
} else {
cb(new Error('허용되지 않는 파일 형식입니다. (JPEG, PNG, GIF, WebP만 가능)'), false);
}
};
const upload = multer({
storage,
fileFilter,
limits: { fileSize: 5 * 1024 * 1024 }
});
// 소모품 사진 업로드
const consumablesDir = path.join(__dirname, '..', 'uploads', 'consumables');
if (!fs.existsSync(consumablesDir)) { fs.mkdirSync(consumablesDir, { recursive: true }); }
const consumableStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, consumablesDir);
},
filename: (req, file, cb) => {
const ext = path.extname(file.originalname).toLowerCase();
const uniqueName = `consumable-${Date.now()}-${crypto.randomInt(100000000, 999999999)}${ext}`;
cb(null, uniqueName);
}
});
const consumableUpload = multer({
storage: consumableStorage,
fileFilter,
limits: { fileSize: 5 * 1024 * 1024 }
});
module.exports = upload;
module.exports.consumableUpload = consumableUpload;