import type { Readable } from 'node:stream'; import { Inject, Injectable } from '@nestjs/common'; import { FILE_STORAGE, type IFileStorage } from '../../../domain/ports/file-storage.port.js'; import { DownloadFileQuery } from './download-file.query.js'; import { FileDto } from '../../dto/file.dto.js'; import { FileApplicationMapper } from '../../mappers/file-application.mapper.js'; import { FileAccessService } from '../../services/file-access.service.js'; export interface DownloadFileResult { file: FileDto; /** Можно ли показывать в браузере inline, иначе — только скачивание */ inline: boolean; stream: Readable; } @Injectable() export class DownloadFileUseCase { constructor( @Inject(FILE_STORAGE) private readonly fileStorage: IFileStorage, private readonly fileAccess: FileAccessService, ) {} async execute(query: DownloadFileQuery): Promise { const file = await this.fileAccess.getVisibleFile(query.id, query.userId); const stream = await this.fileStorage.get(file.storageKey); return { file: FileApplicationMapper.toDto(file), inline: file.isInlineViewable, stream, }; } }