|
|
@@ -8,22 +8,26 @@ import { authMiddleware } from '@/server/middleware/auth.middleware';
|
|
|
|
|
|
// 创建分片上传策略请求Schema
|
|
|
const CreateMultipartUploadPolicyDto = z.object({
|
|
|
- fileKey: z.string().openapi({
|
|
|
- description: '文件键名',
|
|
|
- example: 'documents/report.pdf'
|
|
|
- }),
|
|
|
- totalSize: z.coerce.number().int().positive().openapi({
|
|
|
- description: '文件总大小(字节)',
|
|
|
- example: 10485760
|
|
|
- }),
|
|
|
- partSize: z.coerce.number().int().positive().openapi({
|
|
|
- description: '分片大小(字节)',
|
|
|
- example: 5242880
|
|
|
- }),
|
|
|
- type: z.string().max(50).nullable().optional().openapi({
|
|
|
- description: '文件类型',
|
|
|
- example: 'application/pdf'
|
|
|
- })
|
|
|
+fileKey: z.string().openapi({
|
|
|
+ description: '文件键名',
|
|
|
+ example: 'documents/report.pdf'
|
|
|
+}),
|
|
|
+totalSize: z.coerce.number().int().positive().openapi({
|
|
|
+ description: '文件总大小(字节)',
|
|
|
+ example: 10485760
|
|
|
+}),
|
|
|
+partSize: z.coerce.number().int().positive().openapi({
|
|
|
+ description: '分片大小(字节)',
|
|
|
+ example: 5242880
|
|
|
+}),
|
|
|
+type: z.string().max(50).nullable().optional().openapi({
|
|
|
+ description: '文件类型',
|
|
|
+ example: 'application/pdf'
|
|
|
+}),
|
|
|
+name: z.string().max(255).openapi({
|
|
|
+ description: '文件名称',
|
|
|
+ example: '项目计划书.pdf'
|
|
|
+})
|
|
|
});
|
|
|
|
|
|
// 创建分片上传策略路由定义
|
|
|
@@ -87,23 +91,27 @@ const fileService = new FileService(AppDataSource);
|
|
|
|
|
|
// 创建路由实例
|
|
|
const app = new OpenAPIHono<AuthContext>().openapi(createMultipartUploadPolicyRoute, async (c) => {
|
|
|
- try {
|
|
|
- const data = await c.req.json();
|
|
|
- // 计算分片数量
|
|
|
- const partCount = Math.ceil(data.totalSize / data.partSize);
|
|
|
- const result = await fileService.createMultipartUploadPolicy(data, partCount);
|
|
|
-
|
|
|
- return c.json({
|
|
|
- uploadId: result.uploadId,
|
|
|
- bucket: result.bucket,
|
|
|
- key: result.key,
|
|
|
- host: `${process.env.MINIO_USE_SSL ? 'https' : 'http'}://${process.env.MINIO_ENDPOINT}:${process.env.MINIO_PORT}`,
|
|
|
- partUrls: result.uploadUrls
|
|
|
- }, 200);
|
|
|
- } catch (error) {
|
|
|
- const message = error instanceof Error ? error.message : '生成分片上传策略失败';
|
|
|
- return c.json({ code: 500, message }, 500);
|
|
|
- }
|
|
|
+try {
|
|
|
+ const data = await c.req.json();
|
|
|
+ const user = c.get('user');
|
|
|
+ // 计算分片数量
|
|
|
+ const partCount = Math.ceil(data.totalSize / data.partSize);
|
|
|
+ const result = await fileService.createMultipartUploadPolicy({
|
|
|
+ ...data,
|
|
|
+ uploadUserId: user.id
|
|
|
+ }, partCount);
|
|
|
+
|
|
|
+ return c.json({
|
|
|
+ uploadId: result.uploadId,
|
|
|
+ bucket: result.bucket,
|
|
|
+ key: result.key,
|
|
|
+ host: `${process.env.MINIO_USE_SSL ? 'https' : 'http'}://${process.env.MINIO_ENDPOINT}:${process.env.MINIO_PORT}`,
|
|
|
+ partUrls: result.uploadUrls
|
|
|
+ }, 200);
|
|
|
+} catch (error) {
|
|
|
+ const message = error instanceof Error ? error.message : '生成分片上传策略失败';
|
|
|
+ return c.json({ code: 500, message }, 500);
|
|
|
+}
|
|
|
});
|
|
|
|
|
|
export default app;
|