mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-22 19:25:18 -04:00
69 lines
1.8 KiB
TypeScript
69 lines
1.8 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 { ConsoleLoggerService } from '../../../logger/console-logger.service';
|
|
import { MediaService } from '../../../media/media.service';
|
|
import { MulterFile } from '../../../media/multer-file.interface';
|
|
import { MediaUploadUrlDto } from '../../../media/media-upload-url.dto';
|
|
import {
|
|
ClientError,
|
|
MediaBackendError,
|
|
NotInDBError,
|
|
} from '../../../errors/errors';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
|
|
@Controller('media')
|
|
export class MediaController {
|
|
constructor(
|
|
private readonly logger: ConsoleLoggerService,
|
|
private mediaService: MediaService,
|
|
) {
|
|
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 username = 'hardcoded';
|
|
this.logger.debug(
|
|
`Recieved filename '${file.originalname}' for note '${noteId}' from user '${username}'`,
|
|
'uploadImage',
|
|
);
|
|
try {
|
|
const url = await this.mediaService.saveFile(
|
|
file.buffer,
|
|
username,
|
|
noteId,
|
|
);
|
|
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;
|
|
}
|
|
}
|
|
}
|