mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-13 22:54:42 -04:00
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>
This commit is contained in:
parent
bbba2fc348
commit
93cf9c2c56
3 changed files with 37 additions and 3 deletions
|
@ -1,27 +1,50 @@
|
|||
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()
|
||||
createNote(@Body() noteContent: string) {
|
||||
return this.noteService.createNote(noteContent);
|
||||
@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')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue