mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-30 14:55:27 -04:00

The function now expects a `Note` object instead of a noteId and a `User` instead of a username to make it more consistent with other functions. Signed-off-by: David Mehren <git@herrmehren.de>
73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
*
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
import {
|
|
BadRequestException,
|
|
Controller,
|
|
Headers,
|
|
HttpCode,
|
|
InternalServerErrorException,
|
|
Post,
|
|
UploadedFile,
|
|
UseInterceptors,
|
|
} from '@nestjs/common';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
|
|
import {
|
|
ClientError,
|
|
MediaBackendError,
|
|
NotInDBError,
|
|
} from '../../../errors/errors';
|
|
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
|
|
import { MediaUploadUrlDto } from '../../../media/media-upload-url.dto';
|
|
import { MediaService } from '../../../media/media.service';
|
|
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 { UsersService } from '../../../users/users.service';
|
|
|
|
@Controller('media')
|
|
export class MediaController {
|
|
constructor(
|
|
private readonly logger: ConsoleLoggerService,
|
|
private mediaService: MediaService,
|
|
private userService: UsersService,
|
|
private noteService: NotesService,
|
|
) {
|
|
this.logger.setContext(MediaController.name);
|
|
}
|
|
|
|
@Post()
|
|
@UseInterceptors(FileInterceptor('file'))
|
|
@HttpCode(201)
|
|
async uploadMedia(
|
|
@UploadedFile() file: MulterFile,
|
|
@Headers('HedgeDoc-Note') noteId: string,
|
|
): Promise<MediaUploadUrlDto> {
|
|
// ToDo: Get real userName
|
|
const user: User = await this.userService.getUserByUsername('hardcoded');
|
|
try {
|
|
// 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}'`,
|
|
'uploadMedia',
|
|
);
|
|
const url = await this.mediaService.saveFile(file.buffer, user, note);
|
|
return this.mediaService.toMediaUploadUrlDto(url);
|
|
} catch (e) {
|
|
if (e instanceof ClientError || e instanceof NotInDBError) {
|
|
throw new BadRequestException(e.message);
|
|
}
|
|
if (e instanceof MediaBackendError) {
|
|
throw new InternalServerErrorException(
|
|
'There was an error in the media backend',
|
|
);
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
}
|