|
|
|
@ -2,6 +2,7 @@ import multer from 'multer'; |
|
|
|
import fs from 'node:fs'; |
|
|
|
import path from 'node:path'; |
|
|
|
import { callNodeListener } from 'h3'; |
|
|
|
import { RELATIVE_ASSETS_DIR, POST_MEDIA_PUBLIC_PREFIX } from '../../constants/upload'; |
|
|
|
|
|
|
|
// 类型定义
|
|
|
|
interface IFile { |
|
|
|
@ -14,8 +15,8 @@ interface IFile { |
|
|
|
|
|
|
|
export default defineWrappedResponseHandler(async (event) => { |
|
|
|
try { |
|
|
|
// 存储目录
|
|
|
|
const uploadDir = path.join(process.cwd(), 'public/assets'); |
|
|
|
// 存储目录(使用统一定义的常量,默认 static/upload)
|
|
|
|
const uploadDir = RELATIVE_ASSETS_DIR; |
|
|
|
|
|
|
|
// 自动创建目录
|
|
|
|
if (!fs.existsSync(uploadDir)) { |
|
|
|
@ -69,12 +70,12 @@ export default defineWrappedResponseHandler(async (event) => { |
|
|
|
// 格式化返回数据
|
|
|
|
const result: IFile[] = uploadedFiles.map((file: any) => ({ |
|
|
|
name: file.originalname, |
|
|
|
url: `/public/assets/${file.filename}`, // ✅ 前端可直接访问
|
|
|
|
url: `${POST_MEDIA_PUBLIC_PREFIX}${file.filename}`, // ✅ 与 static 插件对齐
|
|
|
|
mimeType: file.mimetype, |
|
|
|
size: file.size, |
|
|
|
})); |
|
|
|
|
|
|
|
return result; |
|
|
|
return R.success(result); |
|
|
|
|
|
|
|
} catch (err: any) { |
|
|
|
console.error('上传失败:', err); |
|
|
|
|