Refactor file service return type to StreamableFile

Updated the FilesService.get() method to return a StreamableFile directly with appropriate headers instead of a custom object. Adjusted the FilesController to reflect this change and ensure the file names are formatted by replacing spaces with underscores.
This commit is contained in:
Mathis H (Avnyr) 2024-10-03 09:51:26 +02:00
parent 56df921a9b
commit 6b8ea9cd00
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
2 changed files with 21 additions and 18 deletions

View File

@ -1,7 +1,16 @@
import { Controller } from "@nestjs/common"; import { Controller, Get, Param, StreamableFile } from '@nestjs/common';
import { FilesService } from "./files.service"; import { FilesService } from "./files.service";
@Controller("files") @Controller("files")
export class FilesController { export class FilesController {
constructor(private readonly filesService: FilesService) {} constructor(private readonly filesService: FilesService) {}
//TODO the file name, replace spaces to underscore and deliver it via the response
@Get(':fileId')
async getFile(@Param('fileId') fileId: string) {
return this.filesService.get(fileId);
}
//TODO POST FILE
} }

View File

@ -1,9 +1,9 @@
import { Injectable, NotFoundException, StreamableFile } from "@nestjs/common"; import { Injectable, NotFoundException, StreamableFile } from '@nestjs/common';
import { DbService } from "apps/backend/src/app/db/db.service"; import { DbService } from 'apps/backend/src/app/db/db.service';
import { FilesTable } from "apps/backend/src/app/db/schema"; import { FilesTable } from 'apps/backend/src/app/db/schema';
import { StorageService } from "apps/backend/src/app/storage/storage.service"; import { StorageService } from 'apps/backend/src/app/storage/storage.service';
import { IFileWithInformation } from "apps/backend/src/app/storage/storage.types"; import { eq } from 'drizzle-orm';
import { eq } from "drizzle-orm";
@Injectable() @Injectable()
export class FilesService { export class FilesService {
@ -19,7 +19,7 @@ export class FilesService {
* @return A promise that resolves to an object containing the streamable file and additional file information. * @return A promise that resolves to an object containing the streamable file and additional file information.
* @throws NotFoundException if the file does not exist in the database. * @throws NotFoundException if the file does not exist in the database.
*/ */
public async get(fileId: string): Promise<IFileWithInformation<object>> { public async get(fileId: string): Promise<StreamableFile> {
const foundFiles = await this.database const foundFiles = await this.database
.use() .use()
.select() .select()
@ -44,20 +44,14 @@ export class FilesService {
file.extension, file.extension,
file.isDocumentation, file.isDocumentation,
); );
const streamableFile = new StreamableFile(fileBuffer);
const fileInformation = await this.storage.generateInformation( const fileInformation = await this.storage.generateInformation(
fileBuffer, fileBuffer,
file.fileName, file.fileName,
file.isDocumentation, file.isDocumentation,
); );
const fileNameWithoutSpaces = file.fileName.replace(/\s/g, "_");
return { return new StreamableFile(fileBuffer, {
stream: streamableFile, disposition: `attachment; filename="${fileNameWithoutSpaces}.${fileInformation.fileType.ext.toLowerCase()}"`
info: fileInformation, });
additionalData: {
//Since we can have twice or more entry in the database for the same checksum of a file.
foundEntry: foundFiles.length,
},
};
} }
} }