Просмотр исходного кода

✨ feat(file): 重构文件ID生成机制为自增整数

- 将文件ID类型从字符串改为自增整数
- 移除手动生成FILE前缀ID的逻辑,使用数据库自增
- 更新FileSchema中id字段的类型和示例值
- 添加uploadUserId字段到CreateFileDto

♻️ refactor(file): 优化文件路径生成逻辑

- 移除文件ID相关注释,明确路径生成逻辑
- 统一文件路径格式为"uploadUserId/uuid-filename"
yourname 8 месяцев назад
Родитель
Сommit
d6976e1af4
2 измененных файлов с 13 добавлено и 13 удалено
  1. 11 7
      src/server/modules/files/file.entity.ts
  2. 2 6
      src/server/modules/files/file.service.ts

+ 11 - 7
src/server/modules/files/file.entity.ts

@@ -1,10 +1,10 @@
-import { Entity, PrimaryColumn, Column, Index } from 'typeorm';
+import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm';
 import { z } from '@hono/zod-openapi';
 
 @Entity('file')
 export class File {
-  @PrimaryColumn({ name: 'id', type: 'varchar', length: 50 })
-  id!: string;
+  @PrimaryGeneratedColumn({ name: 'id', type: 'int', unsigned: true })
+  id!: number;
 
   @Column({ name: 'name', type: 'varchar', length: 255 })
   name!: string;
@@ -43,9 +43,9 @@ export class File {
 }
 
 export const FileSchema = z.object({
-  id: z.string().max(50).openapi({ 
+  id: z.number().int().positive().openapi({
     description: '文件ID',
-    example: 'FILE20230001' 
+    example: 1
   }),
   name: z.string().max(255).openapi({ 
     description: '文件名称',
@@ -110,9 +110,13 @@ export const CreateFileDto = z.object({
     description: '文件描述',
     example: '2023年度项目计划书'
   }),
-  lastUpdated: z.coerce.date().nullable().optional().openapi({ 
+  uploadUserId: z.string().max(50).openapi({
+    description: '上传用户ID',
+    example: 'U1001'
+  }),
+  lastUpdated: z.coerce.date().nullable().optional().openapi({
     description: '最后更新时间',
-    example: '2023-01-16T14:20:00Z' 
+    example: '2023-01-16T14:20:00Z'
   })
 });
 

+ 2 - 6
src/server/modules/files/file.service.ts

@@ -19,8 +19,7 @@ export class FileService extends GenericCrudService<File> {
    */
   async createFile(data: Partial<File>) {
     try {
-      // 生成唯一文件ID和存储路径
-      const fileId = `FILE${Date.now()}${Math.floor(Math.random() * 1000)}`;
+      // 生成唯一文件存储路径
       const fileKey = `${data.uploadUserId}/${uuidv4()}-${data.name}`;
       
       // 生成MinIO上传策略
@@ -29,7 +28,6 @@ export class FileService extends GenericCrudService<File> {
       // 准备文件记录数据
       const fileData = {
         ...data,
-        id: fileId,
         path: fileKey,
         uploadTime: new Date(),
         createdAt: new Date(),
@@ -91,8 +89,7 @@ export class FileService extends GenericCrudService<File> {
    */
   async createMultipartUploadPolicy(data: Partial<File>, partCount: number) {
     try {
-      // 生成唯一文件ID和存储路径
-      const fileId = `FILE${Date.now()}${Math.floor(Math.random() * 1000)}`;
+      // 生成唯一文件存储路径
       const fileKey = `${data.uploadUserId}/${uuidv4()}-${data.name}`;
       
       // 初始化多部分上传
@@ -112,7 +109,6 @@ export class FileService extends GenericCrudService<File> {
       // 准备文件记录数据
       const fileData = {
         ...data,
-        id: fileId,
         path: fileKey,
         uploadTime: new Date(),
         createdAt: new Date(),