From dea3c1d3932d295931ad6bf8b019ddfe54506f01 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sat, 17 Oct 2020 16:44:00 +0200 Subject: [PATCH] MediaController: Get parent note from `HedgeDoc-Note` header Signed-off-by: David Mehren --- src/api/public/media/media.controller.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/api/public/media/media.controller.ts b/src/api/public/media/media.controller.ts index 4322aaf4e..ce82247dc 100644 --- a/src/api/public/media/media.controller.ts +++ b/src/api/public/media/media.controller.ts @@ -1,5 +1,6 @@ import { Controller, + Headers, Post, UploadedFile, UseInterceptors, @@ -8,26 +9,32 @@ import { FileInterceptor } from '@nestjs/platform-express'; 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); } @Post('upload') @UseInterceptors(FileInterceptor('file')) - async uploadImage(@UploadedFile() file: MulterFile) { - this.logger.debug('Recieved file: ' + file.originalname); - //TODO: Get user and note from request - const url = await this.mediaService.saveFile( - file, - 'hardcoded', - 'hardcoded', + async uploadImage( + @UploadedFile() file: MulterFile, + @Headers('HedgeDoc-Note') noteId: string, + ) { + //TODO: Get user from request + const username = 'hardcoded'; + this.logger.debug( + `Recieved filename '${file.originalname}' for note '${noteId}' from user '${username}'`, + 'uploadImage', ); + const note = await this.notesService.getNoteByIdOrAlias(noteId); + const url = await this.mediaService.saveFile(file, username, note.id); return { link: url, };