新增 阿拉伯语 和 俄语
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
import { isAllowedImage, MAX_IMAGE_SIZE, saveUploadedImage } from '~/server/utils/upload'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const parts = await readMultipartFormData(event)
|
||||
const filePart = parts?.find((part) => part.name === 'file' && part.filename)
|
||||
|
||||
if (!filePart?.data || !filePart.filename) {
|
||||
return fail('请选择要上传的图片')
|
||||
}
|
||||
|
||||
if (!isAllowedImage(filePart.filename, filePart.type || undefined)) {
|
||||
return fail('仅支持 JPG、PNG、GIF、WEBP、AVIF 格式')
|
||||
}
|
||||
|
||||
if (filePart.data.length > MAX_IMAGE_SIZE) {
|
||||
return fail('图片大小不能超过 5MB')
|
||||
}
|
||||
|
||||
try {
|
||||
const url = await saveUploadedImage(filePart.data, filePart.filename)
|
||||
return ok({ url }, '上传成功')
|
||||
} catch {
|
||||
return fail('图片保存失败,请稍后重试')
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user