MediaController: Add DELETE /{filename} route

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2020-10-17 21:54:44 +02:00
parent 9e7e15a20a
commit 3686685f08
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

@ -1,13 +1,21 @@
import { import {
BadRequestException, BadRequestException,
Controller, Controller,
Delete,
Headers, Headers,
NotFoundException,
Param,
Post, Post,
UnauthorizedException,
UploadedFile, UploadedFile,
UseInterceptors, UseInterceptors,
} from '@nestjs/common'; } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express'; 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 { ConsoleLoggerService } from '../../../logger/console-logger.service';
import { MediaService } from '../../../media/media.service'; import { MediaService } from '../../../media/media.service';
import { MulterFile } from '../../../media/multer-file.interface'; import { MulterFile } from '../../../media/multer-file.interface';
@ -25,7 +33,7 @@ export class MediaController {
@Post() @Post()
@UseInterceptors(FileInterceptor('file')) @UseInterceptors(FileInterceptor('file'))
async uploadImage( async uploadMedia(
@UploadedFile() file: MulterFile, @UploadedFile() file: MulterFile,
@Headers('HedgeDoc-Note') noteId: string, @Headers('HedgeDoc-Note') noteId: string,
) { ) {
@ -47,4 +55,21 @@ export class MediaController {
throw e; 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;
}
}
} }