import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm'; import { UserEntity } from '@/server/modules/users/user.entity'; import process from 'node:process'; import { MinioService } from './minio.service'; @Entity('file') export class File { @PrimaryGeneratedColumn({ name: 'id', type: 'int', unsigned: true }) id!: number; @Column({ name: 'name', type: 'varchar', length: 255 }) name!: string; @Column({ name: 'type', type: 'varchar', length: 100, nullable: true, comment: '文件类型' }) type!: string | null; @Column({ name: 'size', type: 'int', unsigned: true, nullable: true, comment: '文件大小,单位字节' }) size!: number | null; @Column({ name: 'path', type: 'varchar', length: 512, comment: '文件存储路径' }) path!: string; // 获取完整的文件URL(包含MINIO_HOST前缀) // get fullUrl(): string { // const protocol = process.env.MINIO_USE_SSL !== 'false' ? 'https' : 'http'; // const port = process.env.MINIO_PORT ? `:${process.env.MINIO_PORT}` : ''; // const host = process.env.MINIO_HOST || 'localhost'; // const bucketName = process.env.MINIO_BUCKET_NAME || 'd8dai'; // return `${protocol}://${host}${port}/${bucketName}/${this.path}`; // } get fullUrl(): Promise { // 创建MinioService实例 const minioService = new MinioService(); // 获取配置的桶名称 const bucketName = process.env.MINIO_BUCKET_NAME || 'd8dai'; // 返回一个Promise,内部处理异步获取URL的逻辑 return new Promise((resolve, reject) => { // 调用minioService的异步方法 minioService.getPresignedFileUrl(bucketName, this.path) .then(url => { // 成功获取URL后解析Promise resolve(url); }) .catch(error => { // 处理可能的错误 console.error('获取文件预签名URL失败:', error); reject(error); // 将错误传递出去 }); }); } @Column({ name: 'description', type: 'text', nullable: true, comment: '文件描述' }) description!: string | null; @Column({ name: 'upload_user_id', type: 'int', unsigned: true }) uploadUserId!: number; @ManyToOne(() => UserEntity) @JoinColumn({ name: 'upload_user_id', referencedColumnName: 'id' }) uploadUser!: UserEntity; @Column({ name: 'upload_time', type: 'datetime' }) uploadTime!: Date; @Column({ name: 'last_updated', type: 'datetime', nullable: true, comment: '最后更新时间' }) lastUpdated!: Date | null; @Column({ name: 'created_at', type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' }) createdAt!: Date; @Column({ name: 'updated_at', type: 'timestamp', default: () => 'CURRENT_TIMESTAMP', onUpdate: 'CURRENT_TIMESTAMP' }) updatedAt!: Date; }