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:
parent
56df921a9b
commit
6b8ea9cd00
@ -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
|
||||
|
||||
}
|
||||
|
@ -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<IFileWithInformation<object>> {
|
||||
public async get(fileId: string): Promise<StreamableFile> {
|
||||
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()}"`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user