mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 12:58:13 +00:00
implement file module and add files relation to booking entity and populate on fetch
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { BaseEntity } from "@edr/api-common";
|
||||
import { Column, Entity } from "typeorm";
|
||||
|
||||
@Entity({ schema: "freight", name: "files" })
|
||||
export class FileRecord extends BaseEntity {
|
||||
@Column({ name: "resource_id", type: "uuid" })
|
||||
resourceId!: string;
|
||||
|
||||
@Column({ name: "resource", type: "varchar", length: 100 })
|
||||
resource!: string;
|
||||
|
||||
@Column({ name: "code", type: "varchar", length: 100 })
|
||||
code!: string;
|
||||
|
||||
@Column({ name: "name", type: "varchar", length: 500 })
|
||||
name!: string;
|
||||
|
||||
@Column({ name: "url", type: "text" })
|
||||
url!: string;
|
||||
|
||||
@Column({ name: "size", type: "integer" })
|
||||
size!: number;
|
||||
|
||||
@Column({ name: "mime_type", type: "varchar", length: 255 })
|
||||
mimeType!: string;
|
||||
}
|
||||
28
apps/edr-freight-api/src/modules/files/files.controller.ts
Normal file
28
apps/edr-freight-api/src/modules/files/files.controller.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Controller, Get, Param, ParseUUIDPipe, Res } from "@nestjs/common";
|
||||
import { ApiOperation, ApiTags } from "@nestjs/swagger";
|
||||
import { Response } from "express";
|
||||
|
||||
import { FilesService } from "./files.service";
|
||||
|
||||
@ApiTags("files")
|
||||
@Controller("files")
|
||||
export class FilesController {
|
||||
constructor(private readonly filesService: FilesService) {}
|
||||
|
||||
@Get(":fileId")
|
||||
@ApiOperation({
|
||||
summary: "Download a file by ID",
|
||||
description:
|
||||
"Global endpoint — streams any uploaded file directly from MinIO by its UUID. " +
|
||||
"No resource context (e.g. booking ID) required.",
|
||||
})
|
||||
async download(
|
||||
@Param("fileId", ParseUUIDPipe) fileId: string,
|
||||
@Res() res: Response,
|
||||
) {
|
||||
const { stream, record } = await this.filesService.streamById(fileId);
|
||||
res.setHeader("Content-Type", record.mimeType);
|
||||
res.setHeader("Content-Disposition", `attachment; filename="${record.name}"`);
|
||||
stream.pipe(res);
|
||||
}
|
||||
}
|
||||
16
apps/edr-freight-api/src/modules/files/files.module.ts
Normal file
16
apps/edr-freight-api/src/modules/files/files.module.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
|
||||
import { MinioModule } from "../minio/minio.module";
|
||||
import { FilesController } from "./files.controller";
|
||||
import { FilesRepository } from "./files.repository";
|
||||
import { FilesService } from "./files.service";
|
||||
import { FileRecord } from "./entities/file.entity";
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([FileRecord]), MinioModule],
|
||||
controllers: [FilesController],
|
||||
providers: [FilesService, FilesRepository],
|
||||
exports: [FilesService],
|
||||
})
|
||||
export class FilesModule {}
|
||||
28
apps/edr-freight-api/src/modules/files/files.repository.ts
Normal file
28
apps/edr-freight-api/src/modules/files/files.repository.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { BaseRepository } from "@edr/api-common";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
|
||||
import { FileRecord } from "./entities/file.entity";
|
||||
|
||||
@Injectable()
|
||||
export class FilesRepository extends BaseRepository<FileRecord> {
|
||||
constructor(
|
||||
@InjectRepository(FileRecord)
|
||||
repository: Repository<FileRecord>,
|
||||
) {
|
||||
super(repository);
|
||||
}
|
||||
|
||||
findByResource(resourceId: string, resource: string): Promise<FileRecord[]> {
|
||||
return this.repository.find({ where: { resourceId, resource } });
|
||||
}
|
||||
|
||||
findByCode(
|
||||
resourceId: string,
|
||||
resource: string,
|
||||
code: string,
|
||||
): Promise<FileRecord | null> {
|
||||
return this.repository.findOne({ where: { resourceId, resource, code } });
|
||||
}
|
||||
}
|
||||
84
apps/edr-freight-api/src/modules/files/files.service.ts
Normal file
84
apps/edr-freight-api/src/modules/files/files.service.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { Readable } from "stream";
|
||||
|
||||
import { MinioService } from "../minio/minio.service";
|
||||
import { FilesRepository } from "./files.repository";
|
||||
import { FileRecord } from "./entities/file.entity";
|
||||
|
||||
export interface CreateFileInput {
|
||||
resourceId: string;
|
||||
resource: string;
|
||||
code: string;
|
||||
file: Express.Multer.File;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class FilesService {
|
||||
constructor(
|
||||
private readonly filesRepository: FilesRepository,
|
||||
private readonly minioService: MinioService,
|
||||
) {}
|
||||
|
||||
async upload(input: CreateFileInput): Promise<FileRecord> {
|
||||
const { resourceId, resource, code, file } = input;
|
||||
const objectName = `${resource}/${resourceId}/${Date.now()}_${file.originalname}`;
|
||||
const url = await this.minioService.uploadFile(objectName, file.buffer, file.mimetype);
|
||||
|
||||
return this.filesRepository.create({
|
||||
resourceId,
|
||||
resource,
|
||||
code,
|
||||
name: file.originalname,
|
||||
url,
|
||||
size: file.size,
|
||||
mimeType: file.mimetype,
|
||||
});
|
||||
}
|
||||
|
||||
async uploadMany(
|
||||
resourceId: string,
|
||||
resource: string,
|
||||
files: Express.Multer.File[],
|
||||
): Promise<FileRecord[]> {
|
||||
return Promise.all(
|
||||
files.map((file) =>
|
||||
this.upload({ resourceId, resource, code: file.fieldname, file }),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<FileRecord> {
|
||||
const record = await this.filesRepository.findById(id);
|
||||
if (!record) throw new NotFoundException(`File ${id} not found`);
|
||||
return record;
|
||||
}
|
||||
|
||||
findByResource(resourceId: string, resource: string): Promise<FileRecord[]> {
|
||||
return this.filesRepository.findByResource(resourceId, resource);
|
||||
}
|
||||
|
||||
async findByCode(
|
||||
resourceId: string,
|
||||
resource: string,
|
||||
code: string,
|
||||
): Promise<FileRecord> {
|
||||
const record = await this.filesRepository.findByCode(resourceId, resource, code);
|
||||
if (!record)
|
||||
throw new NotFoundException(
|
||||
`File with code "${code}" not found for ${resource} ${resourceId}`,
|
||||
);
|
||||
return record;
|
||||
}
|
||||
|
||||
async streamById(id: string): Promise<{ stream: Readable; record: FileRecord }> {
|
||||
const record = await this.findById(id);
|
||||
const objectName = this.extractObjectName(record.url);
|
||||
const stream = await this.minioService.getFileStream(objectName);
|
||||
return { stream, record };
|
||||
}
|
||||
|
||||
private extractObjectName(url: string): string {
|
||||
const parts = url.split("/");
|
||||
return parts.slice(4).join("/");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user