Passed
Pull Request — master (#105)
by Mathieu
17:05
created

server/src/Application/File/Command/DownloadFileQueryHandler.ts   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 36
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 30
mnd 2
bc 2
fnc 1
dl 0
loc 36
rs 10
bpm 2
cpm 3
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A DownloadFileQueryHandler.execute 0 16 3
1
import {Inject} from '@nestjs/common';
2
import {QueryHandler} from '@nestjs/cqrs';
3
import {IFileRepository} from 'src/Domain/File/Repository/IFileRepository';
4
import {DownloadFileQuery} from './DownloadFileQuery';
5
import {IFileStorage} from 'src/Application/IFileStorage';
6
import {DownloadedFileView} from '../View/DownloadedFileView';
7
import {FileNotFoundException} from 'src/Domain/File/Exception/FileNotFoundException';
8
9
@QueryHandler(DownloadFileQuery)
10
export class DownloadFileQueryHandler {
11
  constructor(
12
    @Inject('IFileRepository')
13
    private readonly fileRepository: IFileRepository,
14
    @Inject('IFileStorage')
15
    public readonly fileStorage: IFileStorage
16
  ) {}
17
18
  public async execute(query: DownloadFileQuery): Promise<DownloadedFileView> {
19
    const file = await this.fileRepository.findOneById(query.id);
20
    if (!file) {
21
      throw new FileNotFoundException();
22
    }
23
24
    const buffer = await this.fileStorage.download(file);
25
    if (!buffer) {
26
      throw new FileNotFoundException();
27
    }
28
29
    return new DownloadedFileView(
30
      file.getOriginalName(),
31
      file.getMimeType(),
32
      buffer
33
    );
34
  }
35
}
36