refactor(media-apis): Implement a decorator to convert header to Note instance

Just find the related note in an Interceptor (in both public and private routes)

Related issue: https://github.com/hedgedoc/hedgedoc/issues/1594

Signed-off-by: Lautaro Alvarez <lautarolalvarez@gmail.com>
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
Lautaro Alvarez 2022-05-25 13:22:05 -03:00 committed by David Mehren
parent d36961878d
commit a0b5da6c8b
3 changed files with 47 additions and 10 deletions

View file

@ -6,7 +6,6 @@
import {
Controller,
Delete,
Headers,
Param,
Post,
UploadedFile,
@ -31,7 +30,9 @@ import { MulterFile } from '../../../media/multer-file.interface';
import { Note } from '../../../notes/note.entity';
import { NotesService } from '../../../notes/notes.service';
import { User } from '../../../users/user.entity';
import { NoteHeaderInterceptor } from '../../utils/note-header.interceptor';
import { OpenApi } from '../../utils/openapi.decorator';
import { RequestNote } from '../../utils/request-note.decorator';
import { RequestUser } from '../../utils/request-user.decorator';
@UseGuards(TokenAuthGuard)
@ -77,15 +78,14 @@ export class MediaController {
500,
)
@UseInterceptors(FileInterceptor('file'))
@UseInterceptors(NoteHeaderInterceptor)
async uploadMedia(
@RequestUser() user: User,
@UploadedFile() file: MulterFile,
@Headers('HedgeDoc-Note') noteId: string,
@RequestNote() note: Note,
): Promise<MediaUploadDto> {
// TODO: Move getting the Note object into a decorator
const note: Note = await this.noteService.getNoteByIdOrAlias(noteId);
this.logger.debug(
`Recieved filename '${file.originalname}' for note '${noteId}' from user '${user.username}'`,
`Recieved filename '${file.originalname}' for note '${note.id}' from user '${user.username}'`,
'uploadMedia',
);
const upload = await this.mediaService.saveFile(file.buffer, user, note);