mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-27 12:08:02 -05:00
Added markdownbody-decorator
which puts the markdown text directly to a variable in the route function. Content-type of the http request is checked to be text/markdown because we dealing with markdown. Technically by now there can be any content which can be encoded. There could be features in the software which do not work properly if the text can't be parsed as markdown. Signed-off-by: Yannick Bungers <git@innay.de>
This commit is contained in:
parent
0dec095ed7
commit
258dd1db56
2 changed files with 42 additions and 46 deletions
|
@ -1,5 +1,4 @@
|
||||||
import {
|
import {
|
||||||
BadRequestException,
|
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
Delete,
|
Delete,
|
||||||
|
@ -8,14 +7,12 @@ import {
|
||||||
Param,
|
Param,
|
||||||
Post,
|
Post,
|
||||||
Put,
|
Put,
|
||||||
Req,
|
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { Request } from 'express';
|
|
||||||
import * as getRawBody from 'raw-body';
|
|
||||||
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
|
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
|
||||||
import { NotePermissionsUpdateDto } from '../../../notes/note-permissions.dto';
|
import { NotePermissionsUpdateDto } from '../../../notes/note-permissions.dto';
|
||||||
import { NotesService } from '../../../notes/notes.service';
|
import { NotesService } from '../../../notes/notes.service';
|
||||||
import { RevisionsService } from '../../../revisions/revisions.service';
|
import { RevisionsService } from '../../../revisions/revisions.service';
|
||||||
|
import { MarkdownBody } from '../../utils/markdownbody-decorator';
|
||||||
|
|
||||||
@Controller('notes')
|
@Controller('notes')
|
||||||
export class NotesController {
|
export class NotesController {
|
||||||
|
@ -27,24 +24,10 @@ export class NotesController {
|
||||||
this.logger.setContext(NotesController.name);
|
this.logger.setContext(NotesController.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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()
|
@Post()
|
||||||
async createNote(@Req() req: Request) {
|
async createNote(@MarkdownBody() text: string) {
|
||||||
// we have to check req.readable because of raw-body issue #57
|
this.logger.debug('Got raw markdown:\n' + text);
|
||||||
// https://github.com/stream-utils/raw-body/issues/57
|
return this.noteService.createNoteDto(text);
|
||||||
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.createNoteDto(bodyText);
|
|
||||||
} else {
|
|
||||||
// TODO: Better error message
|
|
||||||
throw new BadRequestException('Invalid body');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':noteIdOrAlias')
|
@Get(':noteIdOrAlias')
|
||||||
|
@ -55,19 +38,10 @@ export class NotesController {
|
||||||
@Post(':noteAlias')
|
@Post(':noteAlias')
|
||||||
async createNamedNote(
|
async createNamedNote(
|
||||||
@Param('noteAlias') noteAlias: string,
|
@Param('noteAlias') noteAlias: string,
|
||||||
@Req() req: Request,
|
@MarkdownBody() text: string,
|
||||||
) {
|
) {
|
||||||
// we have to check req.readable because of raw-body issue #57
|
this.logger.debug('Got raw markdown:\n' + text);
|
||||||
// https://github.com/stream-utils/raw-body/issues/57
|
return this.noteService.createNoteDto(text, noteAlias);
|
||||||
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.createNoteDto(bodyText, noteAlias);
|
|
||||||
} else {
|
|
||||||
// TODO: Better error message
|
|
||||||
throw new BadRequestException('Invalid body');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete(':noteIdOrAlias')
|
@Delete(':noteIdOrAlias')
|
||||||
|
@ -81,19 +55,10 @@ export class NotesController {
|
||||||
@Put(':noteIdOrAlias')
|
@Put(':noteIdOrAlias')
|
||||||
async updateNote(
|
async updateNote(
|
||||||
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
||||||
@Req() req: Request,
|
@MarkdownBody() text: string,
|
||||||
) {
|
) {
|
||||||
// we have to check req.readable because of raw-body issue #57
|
this.logger.debug('Got raw markdown:\n' + text);
|
||||||
// https://github.com/stream-utils/raw-body/issues/57
|
return this.noteService.updateNoteByIdOrAlias(noteIdOrAlias, text);
|
||||||
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.updateNoteByIdOrAlias(noteIdOrAlias, bodyText);
|
|
||||||
} else {
|
|
||||||
// TODO: Better error message
|
|
||||||
throw new BadRequestException('Invalid body');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':noteIdOrAlias/content')
|
@Get(':noteIdOrAlias/content')
|
||||||
|
|
31
src/api/utils/markdownbody-decorator.ts
Normal file
31
src/api/utils/markdownbody-decorator.ts
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
createParamDecorator,
|
||||||
|
ExecutionContext,
|
||||||
|
InternalServerErrorException,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import * as getRawBody from 'raw-body';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
export const MarkdownBody = createParamDecorator(async (_, context: ExecutionContext) => {
|
||||||
|
// we have to check req.readable because of raw-body issue #57
|
||||||
|
// https://github.com/stream-utils/raw-body/issues/57
|
||||||
|
const req = context.switchToHttp().getRequest<import('express').Request>();
|
||||||
|
// Here the Content-Type of the http request is checked to be text/markdown
|
||||||
|
// because we dealing with markdown. Technically by now there can be any content which can be encoded.
|
||||||
|
// There could be features in the software which do not work properly if the text can't be parsed as markdown.
|
||||||
|
if (req.get('Content-Type') === 'text/markdown') {
|
||||||
|
if (req.readable) {
|
||||||
|
return (await getRawBody(req)).toString().trim();
|
||||||
|
} else {
|
||||||
|
throw new InternalServerErrorException('Failed to parse request body!');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new BadRequestException('Body Content-Type has to be text/markdown!');
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in a new issue