route.ts
基本信息
- 类型: API 路由
- 路径:
./src/app/api/upload/route.ts - 路由:
/api/upload
概述
文件上传接口。支持上传图片和视频文件,自动压缩图片并存储到配置的存储后端。
HTTP 方法
- POST: 上传文件
文件限制
| 类型 | 允许格式 | 最大大小 |
|---|---|---|
| 图片 | JPEG, PNG, GIF, WebP | 4MB |
| 视频 | MP4 | 4MB |
图片处理
- 所有图片自动转换为 JPEG 格式
- 压缩质量: 90%
- 使用 mozjpeg 优化
请求格式
Content-Type: multipart/form-data
Form Data
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| file | File | 是 | 要上传的文件 |
响应
成功 (200)
{
"url": "https://cdn.example.com/prompt-media/xxx.jpg",
"size": 12345
}
错误
未授权 (401)
{
"error": "Unauthorized"
}
URL 存储模式 (400)
{
"error": "File upload is not enabled. Using URL storage mode."
}
存储插件未配置 (500)
{
"error": "Storage plugin \"xxx\" not found"
}
存储插件配置错误 (500)
{
"error": "Storage plugin \"xxx\" is not configured"
}
无文件 (400)
{
"error": "No file provided"
}
文件类型无效 (400)
{
"error": "Invalid file type. Only JPEG, PNG, GIF, WebP images and MP4 videos are allowed."
}
文件过大 (400)
{
"error": "File too large. Maximum size is 4MB."
}
上传失败 (500)
{
"error": "错误信息"
}
依赖
next/server- Next.js 服务器组件sharp- 图片处理@/lib/auth- 认证@/lib/plugins/registry- 存储插件注册表
权限
- 需要登录
存储配置
通过 ENABLED_STORAGE 环境变量配置存储后端:
url: URL 模式,禁用文件上传s3: AWS S3 存储- 其他自定义存储插件
文件名生成
prompt-media-{timestamp}-{random}.jpg
prompt-media-{timestamp}-{random}.mp4