From 490e4cac6988b2b6f7c04bdff94ee0ddb1b2470a Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sat, 17 Oct 2020 21:54:44 +0200 Subject: [PATCH] MediaController: Add `DELETE /{filename}` route Signed-off-by: David Mehren --- src/api/public/media/media.controller.ts | 29 ++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/api/public/media/media.controller.ts b/src/api/public/media/media.controller.ts index 9627a5054..109a2d1c3 100644 --- a/src/api/public/media/media.controller.ts +++ b/src/api/public/media/media.controller.ts @@ -1,13 +1,21 @@ import { BadRequestException, Controller, + Delete, Headers, + NotFoundException, + Param, Post, + UnauthorizedException, UploadedFile, UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; -import { ClientError, NotInDBError } from '../../../errors/errors'; +import { + ClientError, + NotInDBError, + PermissionError, +} from '../../../errors/errors'; import { ConsoleLoggerService } from '../../../logger/console-logger.service'; import { MediaService } from '../../../media/media.service'; import { MulterFile } from '../../../media/multer-file.interface'; @@ -25,7 +33,7 @@ export class MediaController { @Post() @UseInterceptors(FileInterceptor('file')) - async uploadImage( + async uploadMedia( @UploadedFile() file: MulterFile, @Headers('HedgeDoc-Note') noteId: string, ) { @@ -47,4 +55,21 @@ export class MediaController { throw e; } } + + @Delete(':filename') + async deleteMedia(@Param('filename') filename: string) { + //TODO: Get user from request + const username = 'hardcoded'; + try { + await this.mediaService.deleteFile(filename, username); + } catch (e) { + if (e instanceof PermissionError) { + throw new UnauthorizedException(e.message); + } + if (e instanceof NotInDBError) { + throw new NotFoundException(e.message); + } + throw e; + } + } }