| Total Complexity | 3 | 
| Complexity/F | 3 | 
| Lines of Code | 36 | 
| Function Count | 1 | 
| Duplicated Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
| 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 |