Files

89 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2026-05-23 14:15:45 +08:00
const Router = require('koa-router')
const multer = require('@koa/multer')
const path = require('path')
const fs = require('fs')
2026-06-04 19:34:31 +08:00
const { requireAuth, requireStaffAuth } = require('../middleware/auth')
2026-05-23 14:15:45 +08:00
const router = new Router()
2026-05-26 13:37:55 +08:00
const ALLOWED_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
2026-06-03 14:15:55 +08:00
const ALLOWED_EXTS = ['.jpg', '.jpeg', '.png', '.gif', '.webp']
2026-05-26 13:37:55 +08:00
const MAX_SIZE = 5 * 1024 * 1024
2026-06-03 14:15:55 +08:00
const ALLOWED_BUCKETS = ['goods', 'points', 'avatar', 'category']
2026-05-26 13:37:55 +08:00
2026-06-04 19:34:31 +08:00
// 普通用户可上传的目录
const USER_ALLOWED_BUCKETS = ['avatar']
2026-05-23 14:15:45 +08:00
const uploadDir = path.join(__dirname, '..', 'public', 'uploads')
const storage = multer.diskStorage({
destination: (req, file, cb) => {
2026-05-26 14:53:42 +08:00
const type = (req.query && req.query.type) || 'goods'
2026-06-03 14:15:55 +08:00
if (!ALLOWED_BUCKETS.includes(type)) {
return cb(new Error('非法的上传目录'))
}
2026-05-26 13:37:55 +08:00
const dir = path.join(uploadDir, type)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
cb(null, dir)
2026-05-23 14:15:45 +08:00
},
filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9)
2026-06-03 14:15:55 +08:00
const ext = (path.extname(file.originalname) || '').toLowerCase()
const safeExt = ALLOWED_EXTS.includes(ext) ? ext : '.jpg'
cb(null, uniqueSuffix + safeExt)
2026-05-23 14:15:45 +08:00
}
})
2026-05-26 13:37:55 +08:00
const upload = multer({
storage,
2026-06-03 14:15:55 +08:00
limits: { fileSize: MAX_SIZE, files: 1 },
2026-05-26 13:37:55 +08:00
fileFilter: (req, file, cb) => {
2026-06-03 14:15:55 +08:00
if (!ALLOWED_TYPES.includes(file.mimetype)) {
return cb(new Error('不支持的文件类型,仅支持 jpg/png/gif/webp'))
2026-05-26 13:37:55 +08:00
}
2026-06-03 14:15:55 +08:00
cb(null, true)
2026-05-26 13:37:55 +08:00
}
})
2026-05-23 14:15:45 +08:00
2026-06-04 19:34:31 +08:00
// 头像上传 - 所有已登录用户可用
router.post('/avatar', requireAuth(), upload.single('file'), async (ctx) => {
if (!ctx.file) {
ctx.status = 400
ctx.body = { code: 400, message: '没有上传文件' }
return
}
const fileUrl = `/uploads/avatar/${ctx.file.filename}`
ctx.body = {
code: 200,
message: '上传成功',
url: fileUrl
}
})
// 通用上传 - staff/admin 可用
2026-06-03 14:15:55 +08:00
router.post('/', requireStaffAuth(), upload.single('file'), async (ctx) => {
2026-05-23 14:15:45 +08:00
if (!ctx.file) {
ctx.status = 400
2026-05-26 13:37:55 +08:00
ctx.body = { code: 400, message: '没有上传文件' }
2026-05-23 14:15:45 +08:00
return
}
2026-05-26 13:37:55 +08:00
const type = ctx.query.type || 'goods'
2026-06-03 14:15:55 +08:00
if (!ALLOWED_BUCKETS.includes(type)) {
ctx.status = 400
ctx.body = { code: 400, message: '非法的上传目录' }
return
}
2026-05-26 13:37:55 +08:00
const fileUrl = `/uploads/${type}/${ctx.file.filename}`
2026-05-23 14:15:45 +08:00
ctx.body = {
code: 200,
2026-05-24 17:05:26 +08:00
message: '上传成功',
2026-05-23 14:15:45 +08:00
url: fileUrl
}
})
module.exports = router.routes()