file.entity.ts 2.9 KB

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