hedgedoc/src/api/public/notes/notes.controller.ts
David Mehren b4b91acddb
NotesController: Use custom logic to access raw markdown
NestJS does not support content-types other than application/json.
Therefore we need to directly access the request object to get the raw body content.

Signed-off-by: David Mehren <git@herrmehren.de>
2020-09-25 21:35:47 +02:00

107 lines
3.2 KiB
TypeScript

import {
BadRequestException,
Body,
Controller,
Delete,
Get,
Header,
Logger,
Param,
Post,
Put,
Req,
} from '@nestjs/common';
import { Request } from 'express';
import * as getRawBody from 'raw-body';
import { NotePermissionsUpdateDto } from '../../../notes/note-permissions.dto';
import { NotesService } from '../../../notes/notes.service';
import { RevisionsService } from '../../../revisions/revisions.service';
@Controller('notes')
export class NotesController {
private readonly logger = new Logger(NotesController.name);
constructor(
private noteService: NotesService,
private revisionsService: RevisionsService,
) {}
/**
* Extract the raw markdown from the request body and create a new note with it
*
* Implementation inspired by https://stackoverflow.com/questions/52283713/how-do-i-pass-plain-text-as-my-request-body-using-nestjs
*/
@Post()
@Header('content-type', 'text/markdown')
async createNote(@Req() req: Request) {
// we have to check req.readable because of raw-body issue #57
// https://github.com/stream-utils/raw-body/issues/57
if (req.readable) {
let bodyText: string = await getRawBody(req, 'utf-8');
bodyText = bodyText.trim();
this.logger.debug('Got raw markdown:\n' + bodyText);
return this.noteService.createNote(bodyText);
} else {
// TODO: Better error message
throw new BadRequestException('Invalid body');
}
}
@Get(':noteIdOrAlias')
getNote(@Param('noteIdOrAlias') noteIdOrAlias: string) {
return this.noteService.getNoteByIdOrAlias(noteIdOrAlias);
}
@Post(':noteAlias')
createNamedNote(
@Param('noteAlias') noteAlias: string,
@Body() noteContent: string,
) {
return this.noteService.createNote(noteContent, noteAlias);
}
@Delete(':noteIdOrAlias')
deleteNote(@Param('noteIdOrAlias') noteIdOrAlias: string) {
return this.noteService.deleteNoteByIdOrAlias(noteIdOrAlias);
}
@Put(':noteIdOrAlias')
updateNote(
@Param('noteIdOrAlias') noteIdOrAlias: string,
@Body() noteContent: string,
) {
return this.noteService.updateNoteByIdOrAlias(noteIdOrAlias, noteContent);
}
@Get(':noteIdOrAlias/content')
@Header('content-type', 'text/markdown')
getNoteContent(@Param('noteIdOrAlias') noteIdOrAlias: string) {
return this.noteService.getNoteContent(noteIdOrAlias);
}
@Get(':noteIdOrAlias/metadata')
getNoteMetadata(@Param('noteIdOrAlias') noteIdOrAlias: string) {
return this.noteService.getNoteMetadata(noteIdOrAlias);
}
@Put(':noteIdOrAlias/permissions')
updateNotePermissions(
@Param('noteIdOrAlias') noteIdOrAlias: string,
@Body() updateDto: NotePermissionsUpdateDto,
) {
return this.noteService.updateNotePermissions(noteIdOrAlias, updateDto);
}
@Get(':noteIdOrAlias/revisions')
getNoteRevisions(@Param('noteIdOrAlias') noteIdOrAlias: string) {
return this.revisionsService.getNoteRevisionMetadatas(noteIdOrAlias);
}
@Get(':noteIdOrAlias/revisions/:revisionId')
getNoteRevision(
@Param('noteIdOrAlias') noteIdOrAlias: string,
@Param('revisionId') revisionId: string,
) {
return this.revisionsService.getNoteRevision(noteIdOrAlias, revisionId);
}
}