Browse Source

✨ feat(upload): add file upload user tracking and name parameter

- add fileName parameter to getUploadPolicy function for better file identification
- automatically record uploadUserId from authentication context when creating file records
- add uploadTime field to automatically record file upload timestamp
- remove uploadUserId from CreateFileDto as it's now set server-side from auth context
yourname 8 months ago
parent
commit
8378c29164

+ 5 - 2
src/client/utils/minio.ts

@@ -314,9 +314,12 @@ export class MinIOXHRUploader {
     }
 } 
 
-export async function getUploadPolicy(key: string): Promise<MinioUploadPolicy> {
+export async function getUploadPolicy(key: string, fileName: string): Promise<MinioUploadPolicy> {
   const policyResponse = await fileClient["upload-policy"].$post({
-    json: { path: key }
+    json: {
+      path: key,
+      name: fileName
+    }
   });
   if (!policyResponse.ok) {
     throw new Error('获取上传策略失败');

+ 9 - 1
src/server/api/files/upload-policy/post.ts

@@ -58,7 +58,15 @@ const fileService = new FileService(AppDataSource);
 const app = new OpenAPIHono<AuthContext>().openapi(createUploadPolicyRoute, async (c) => {
   try {
     const data = await c.req.json();
-    const result = await fileService.createFile(data);
+    const user = c.get('user');
+    
+    // 添加用户ID到文件数据
+    const fileData = {
+      ...data,
+      uploadUserId: user.id,
+      uploadTime: new Date()
+    };
+    const result = await fileService.createFile(fileData);
     // 手动转换日期类型并处理可选字段
     const formattedFile = {
       ...result.file,

+ 0 - 4
src/server/modules/files/file.entity.ts

@@ -110,10 +110,6 @@ export const CreateFileDto = z.object({
     description: '文件描述',
     example: '2023年度项目计划书'
   }),
-  uploadUserId: z.string().max(50).openapi({
-    description: '上传用户ID',
-    example: 'U1001'
-  }),
   lastUpdated: z.coerce.date().nullable().optional().openapi({ 
     description: '最后更新时间',
     example: '2023-01-16T14:20:00Z'