MediaService: Simplify saveFile signature

As the `saveFile` method only really uses the files `Buffer`, this commit changes the signature so it directly gets a `Buffer` instead of a complicated `MulterFile` object. This also simplifies testing.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2020-10-24 11:49:01 +02:00
parent 53fbe82b6a
commit 2b23dcc5a9
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 9 additions and 8 deletions

View file

@ -19,14 +19,12 @@ import {
import { ConsoleLoggerService } from '../../../logger/console-logger.service'; import { ConsoleLoggerService } from '../../../logger/console-logger.service';
import { MediaService } from '../../../media/media.service'; import { MediaService } from '../../../media/media.service';
import { MulterFile } from '../../../media/multer-file.interface'; import { MulterFile } from '../../../media/multer-file.interface';
import { NotesService } from '../../../notes/notes.service';
@Controller('media') @Controller('media')
export class MediaController { export class MediaController {
constructor( constructor(
private readonly logger: ConsoleLoggerService, private readonly logger: ConsoleLoggerService,
private mediaService: MediaService, private mediaService: MediaService,
private notesService: NotesService,
) { ) {
this.logger.setContext(MediaController.name); this.logger.setContext(MediaController.name);
} }
@ -44,7 +42,11 @@ export class MediaController {
'uploadImage', 'uploadImage',
); );
try { try {
const url = await this.mediaService.saveFile(file, username, noteId); const url = await this.mediaService.saveFile(
file.buffer,
username,
noteId,
);
return { return {
link: url, link: url,
}; };

View file

@ -10,7 +10,6 @@ import { UsersService } from '../users/users.service';
import { BackendType } from './backends/backend-type.enum'; import { BackendType } from './backends/backend-type.enum';
import { FilesystemBackend } from './backends/filesystem-backend'; import { FilesystemBackend } from './backends/filesystem-backend';
import { MediaUpload } from './media-upload.entity'; import { MediaUpload } from './media-upload.entity';
import { MulterFile } from './multer-file.interface';
@Injectable() @Injectable()
export class MediaService { export class MediaService {
@ -44,14 +43,14 @@ export class MediaService {
return allowedTypes.includes(mimeType); return allowedTypes.includes(mimeType);
} }
public async saveFile(file: MulterFile, username: string, noteId: string) { public async saveFile(fileBuffer: Buffer, username: string, noteId: string) {
this.logger.debug( this.logger.debug(
`Saving '${file.originalname}' for note '${noteId}' and user '${username}'`, `Saving file for note '${noteId}' and user '${username}'`,
'saveFile', 'saveFile',
); );
const note = await this.notesService.getNoteByIdOrAlias(noteId); const note = await this.notesService.getNoteByIdOrAlias(noteId);
const user = await this.usersService.getUserByUsername(username); const user = await this.usersService.getUserByUsername(username);
const fileTypeResult = await FileType.fromBuffer(file.buffer); const fileTypeResult = await FileType.fromBuffer(fileBuffer);
if (!fileTypeResult) { if (!fileTypeResult) {
throw new ClientError('Could not detect file type.'); throw new ClientError('Could not detect file type.');
} }
@ -68,7 +67,7 @@ export class MediaService {
this.logger.debug(`Generated filename: '${mediaUpload.id}'`, 'saveFile'); this.logger.debug(`Generated filename: '${mediaUpload.id}'`, 'saveFile');
const backend = this.moduleRef.get(FilesystemBackend); const backend = this.moduleRef.get(FilesystemBackend);
const [url, backendData] = await backend.saveFile( const [url, backendData] = await backend.saveFile(
file.buffer, fileBuffer,
mediaUpload.id, mediaUpload.id,
); );
mediaUpload.backendData = backendData; mediaUpload.backendData = backendData;