From 6b8ea9cd003d1771e1166a1e2c3e90b30f358953 Mon Sep 17 00:00:00 2001 From: Mathis Date: Thu, 3 Oct 2024 09:51:26 +0200 Subject: [PATCH] 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. --- .../backend/src/app/files/files.controller.ts | 11 +++++++- apps/backend/src/app/files/files.service.ts | 28 ++++++++----------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/apps/backend/src/app/files/files.controller.ts b/apps/backend/src/app/files/files.controller.ts index 3f89e02..5f699f7 100644 --- a/apps/backend/src/app/files/files.controller.ts +++ b/apps/backend/src/app/files/files.controller.ts @@ -1,7 +1,16 @@ -import { Controller } from "@nestjs/common"; +import { Controller, Get, Param, StreamableFile } from '@nestjs/common'; import { FilesService } from "./files.service"; @Controller("files") export class FilesController { 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 + } diff --git a/apps/backend/src/app/files/files.service.ts b/apps/backend/src/app/files/files.service.ts index 8096be8..0e326b1 100644 --- a/apps/backend/src/app/files/files.service.ts +++ b/apps/backend/src/app/files/files.service.ts @@ -1,9 +1,9 @@ -import { Injectable, NotFoundException, StreamableFile } from "@nestjs/common"; -import { DbService } from "apps/backend/src/app/db/db.service"; -import { FilesTable } from "apps/backend/src/app/db/schema"; -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 { Injectable, NotFoundException, StreamableFile } from '@nestjs/common'; +import { DbService } from 'apps/backend/src/app/db/db.service'; +import { FilesTable } from 'apps/backend/src/app/db/schema'; +import { StorageService } from 'apps/backend/src/app/storage/storage.service'; +import { eq } from 'drizzle-orm'; + @Injectable() 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. * @throws NotFoundException if the file does not exist in the database. */ - public async get(fileId: string): Promise> { + public async get(fileId: string): Promise { const foundFiles = await this.database .use() .select() @@ -44,20 +44,14 @@ export class FilesService { file.extension, file.isDocumentation, ); - const streamableFile = new StreamableFile(fileBuffer); const fileInformation = await this.storage.generateInformation( fileBuffer, file.fileName, file.isDocumentation, ); - - return { - stream: streamableFile, - info: fileInformation, - additionalData: { - //Since we can have twice or more entry in the database for the same checksum of a file. - foundEntry: foundFiles.length, - }, - }; + const fileNameWithoutSpaces = file.fileName.replace(/\s/g, "_"); + return new StreamableFile(fileBuffer, { + disposition: `attachment; filename="${fileNameWithoutSpaces}.${fileInformation.fileType.ext.toLowerCase()}"` + }); } }