Passed
Pull Request — master (#105)
by Mathieu
02:00 queued 25s
created

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

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 33
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A DownloadFileQueryHandler.execute 0 13 2
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
26
    return new DownloadedFileView(
27
      file.getOriginalName(),
28
      file.getMimeType(),
29
      buffer
30
    );
31
  }
32
}
33