import { Controller, Get, Param, Query, Res, StreamableFile, UseGuards, } from '@nestjs/common'; import type { Response } from 'express'; import { ApiBearerAuth, ApiOkResponse, ApiProduces, ApiTags, } from '@nestjs/swagger'; import { createReadStream } from 'node:fs'; import { DeviceAuthGuard } from '../auth/device-auth.guard'; import { AssetDownloadQueryDto } from './assets.dto'; import { AssetsService } from './assets.service'; @ApiTags('assets') @UseGuards(DeviceAuthGuard) @ApiBearerAuth() @Controller({ path: 'assets', version: '1', }) export class AssetsController { constructor(private readonly assetsService: AssetsService) {} @Get(':assetId/download') @ApiProduces('audio/mpeg') @ApiOkResponse({ schema: { type: 'string', format: 'binary', }, }) async download( @Param('assetId') assetId: string, @Query() query: AssetDownloadQueryDto, @Res({ passthrough: true }) response: Response, ): Promise { const download = await this.assetsService.getOwnedAudioAssetDownload( assetId, query.deviceId, ); response.setHeader('Content-Type', 'audio/mpeg'); response.setHeader('Content-Length', String(download.contentLength)); return new StreamableFile(createReadStream(download.filePath)); } }