mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-22 01:36:29 -05:00
MediaService: Improve error handling and logging
Add debug logging to `saveFile` method and throw the proper errors when problems with the mime type are encountered Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
219a3bcb5f
commit
0cc9c6b347
1 changed files with 18 additions and 8 deletions
|
@ -3,6 +3,8 @@ import { ModuleRef } from '@nestjs/core';
|
|||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import * as FileType from 'file-type';
|
||||
import { Repository } from 'typeorm';
|
||||
import { ClientError } from '../errors/errors';
|
||||
import { ConsoleLoggerService } from '../logger/console-logger.service';
|
||||
import { NotesService } from '../notes/notes.service';
|
||||
import { UsersService } from '../users/users.service';
|
||||
import { BackendType } from './backends/backend-type.enum';
|
||||
|
@ -13,22 +15,34 @@ import { MulterFile } from './multer-file.interface';
|
|||
@Injectable()
|
||||
export class MediaService {
|
||||
constructor(
|
||||
private readonly logger: ConsoleLoggerService,
|
||||
@InjectRepository(MediaUpload)
|
||||
private mediaUploadRepository: Repository<MediaUpload>,
|
||||
private notesService: NotesService,
|
||||
private usersService: UsersService,
|
||||
private moduleRef: ModuleRef,
|
||||
) {}
|
||||
) {
|
||||
this.logger.setContext(MediaService.name);
|
||||
}
|
||||
|
||||
private static isAllowedMimeType(mimeType: string): boolean {
|
||||
//TODO: Which mimetypes are allowed?
|
||||
return true;
|
||||
}
|
||||
|
||||
public async saveFile(file: MulterFile, username: string, noteId: string) {
|
||||
this.logger.debug(
|
||||
`Saving '${file.originalname}' for note '${noteId}' and user '${username}'`,
|
||||
'saveFile',
|
||||
);
|
||||
const note = await this.notesService.getNoteByIdOrAlias(noteId);
|
||||
const user = await this.usersService.getUserByUsername(username);
|
||||
const fileTypeResult = await FileType.fromBuffer(file.buffer);
|
||||
if (!fileTypeResult) {
|
||||
throw new Error('Could not detect file type.');
|
||||
throw new ClientError('Could not detect file type.');
|
||||
}
|
||||
if (!MediaService.isAllowedMimeType(fileTypeResult.mime)) {
|
||||
throw new Error('MIME type not allowed');
|
||||
throw new ClientError('MIME type not allowed.');
|
||||
}
|
||||
//TODO: Choose backend according to config
|
||||
const mediaUpload = MediaUpload.create(
|
||||
|
@ -37,6 +51,7 @@ export class MediaService {
|
|||
fileTypeResult.ext,
|
||||
BackendType.FILEYSTEM,
|
||||
);
|
||||
this.logger.debug(`Generated filename: '${mediaUpload.id}'`, 'saveFile');
|
||||
const backend = this.moduleRef.get(FilesystemBackend);
|
||||
const [url, backendData] = await backend.saveFile(
|
||||
file.buffer,
|
||||
|
@ -46,9 +61,4 @@ export class MediaService {
|
|||
await this.mediaUploadRepository.save(mediaUpload);
|
||||
return url;
|
||||
}
|
||||
|
||||
private static isAllowedMimeType(mimeType: string): boolean {
|
||||
//TODO: Which mimetypes are allowed?
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue