file.entity.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
  2. import { UserEntity } from '@/server/modules/users/user.entity';
  3. import process from 'node:process';
  4. import { MinioService } from './minio.service';
  5. @Entity('file')
  6. export class File {
  7. @PrimaryGeneratedColumn({ name: 'id', type: 'int', unsigned: true })
  8. id!: number;
  9. @Column({ name: 'name', type: 'varchar', length: 255 })
  10. name!: string;
  11. @Column({ name: 'type', type: 'varchar', length: 100, nullable: true, comment: '文件类型' })
  12. type!: string | null;
  13. @Column({ name: 'size', type: 'int', unsigned: true, nullable: true, comment: '文件大小,单位字节' })
  14. size!: number | null;
  15. @Column({ name: 'path', type: 'varchar', length: 512, comment: '文件存储路径' })
  16. path!: string;
  17. // 获取完整的文件URL(包含MINIO_HOST前缀)
  18. // get fullUrl(): string {
  19. // const protocol = process.env.MINIO_USE_SSL !== 'false' ? 'https' : 'http';
  20. // const port = process.env.MINIO_PORT ? `:${process.env.MINIO_PORT}` : '';
  21. // const host = process.env.MINIO_HOST || 'localhost';
  22. // const bucketName = process.env.MINIO_BUCKET_NAME || 'd8dai';
  23. // return `${protocol}://${host}${port}/${bucketName}/${this.path}`;
  24. // }
  25. get fullUrl(): Promise<string> {
  26. // 创建MinioService实例
  27. const minioService = new MinioService();
  28. // 获取配置的桶名称
  29. const bucketName = process.env.MINIO_BUCKET_NAME || 'd8dai';
  30. // 返回一个Promise,内部处理异步获取URL的逻辑
  31. return new Promise((resolve, reject) => {
  32. // 调用minioService的异步方法
  33. minioService.getPresignedFileUrl(bucketName, this.path)
  34. .then(url => {
  35. // 成功获取URL后解析Promise
  36. resolve(url);
  37. })
  38. .catch(error => {
  39. // 处理可能的错误
  40. console.error('获取文件预签名URL失败:', error);
  41. reject(error); // 将错误传递出去
  42. });
  43. });
  44. }
  45. @Column({ name: 'description', type: 'text', nullable: true, comment: '文件描述' })
  46. description!: string | null;
  47. @Column({ name: 'upload_user_id', type: 'int', unsigned: true })
  48. uploadUserId!: number;
  49. @ManyToOne(() => UserEntity)
  50. @JoinColumn({ name: 'upload_user_id', referencedColumnName: 'id' })
  51. uploadUser!: UserEntity;
  52. @Column({ name: 'upload_time', type: 'datetime' })
  53. uploadTime!: Date;
  54. @Column({ name: 'last_updated', type: 'datetime', nullable: true, comment: '最后更新时间' })
  55. lastUpdated!: Date | null;
  56. @Column({ name: 'created_at', type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  57. createdAt!: Date;
  58. @Column({
  59. name: 'updated_at',
  60. type: 'timestamp',
  61. default: () => 'CURRENT_TIMESTAMP',
  62. onUpdate: 'CURRENT_TIMESTAMP'
  63. })
  64. updatedAt!: Date;
  65. }