From 2b23dcc5a96bd4975a70c2b9c4f7cc44d3369930 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sat, 24 Oct 2020 11:49:01 +0200 Subject: [PATCH] MediaService: Simplify `saveFile` signature As the `saveFile` method only really uses the files `Buffer`, this commit changes the signature so it directly gets a `Buffer` instead of a complicated `MulterFile` object. This also simplifies testing. Signed-off-by: David Mehren --- src/api/public/media/media.controller.ts | 8 +++++--- src/media/media.service.ts | 9 ++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/api/public/media/media.controller.ts b/src/api/public/media/media.controller.ts index 109a2d1c3..410340ece 100644 --- a/src/api/public/media/media.controller.ts +++ b/src/api/public/media/media.controller.ts @@ -19,14 +19,12 @@ import { import { ConsoleLoggerService } from '../../../logger/console-logger.service'; import { MediaService } from '../../../media/media.service'; import { MulterFile } from '../../../media/multer-file.interface'; -import { NotesService } from '../../../notes/notes.service'; @Controller('media') export class MediaController { constructor( private readonly logger: ConsoleLoggerService, private mediaService: MediaService, - private notesService: NotesService, ) { this.logger.setContext(MediaController.name); } @@ -44,7 +42,11 @@ export class MediaController { 'uploadImage', ); try { - const url = await this.mediaService.saveFile(file, username, noteId); + const url = await this.mediaService.saveFile( + file.buffer, + username, + noteId, + ); return { link: url, }; diff --git a/src/media/media.service.ts b/src/media/media.service.ts index 92c96ee49..7b59ede3b 100644 --- a/src/media/media.service.ts +++ b/src/media/media.service.ts @@ -10,7 +10,6 @@ import { UsersService } from '../users/users.service'; import { BackendType } from './backends/backend-type.enum'; import { FilesystemBackend } from './backends/filesystem-backend'; import { MediaUpload } from './media-upload.entity'; -import { MulterFile } from './multer-file.interface'; @Injectable() export class MediaService { @@ -44,14 +43,14 @@ export class MediaService { return allowedTypes.includes(mimeType); } - public async saveFile(file: MulterFile, username: string, noteId: string) { + public async saveFile(fileBuffer: Buffer, username: string, noteId: string) { this.logger.debug( - `Saving '${file.originalname}' for note '${noteId}' and user '${username}'`, + `Saving file for note '${noteId}' and user '${username}'`, 'saveFile', ); const note = await this.notesService.getNoteByIdOrAlias(noteId); const user = await this.usersService.getUserByUsername(username); - const fileTypeResult = await FileType.fromBuffer(file.buffer); + const fileTypeResult = await FileType.fromBuffer(fileBuffer); if (!fileTypeResult) { throw new ClientError('Could not detect file type.'); } @@ -68,7 +67,7 @@ export class MediaService { this.logger.debug(`Generated filename: '${mediaUpload.id}'`, 'saveFile'); const backend = this.moduleRef.get(FilesystemBackend); const [url, backendData] = await backend.saveFile( - file.buffer, + fileBuffer, mediaUpload.id, ); mediaUpload.backendData = backendData;