Commit 9b8526c2 authored by Diego Iván's avatar Diego Iván
Browse files

Cambiando de ReadableStream a Streamable File

parent 4e4f9955
Loading
Loading
Loading
Loading
+14 −6
Original line number Diff line number Diff line
import { Controller, Get, Post, Body, Param, UseInterceptors, UploadedFile, Query } from '@nestjs/common';
import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  UseInterceptors,
  UploadedFile,
  Query,
  StreamableFile,
} from '@nestjs/common';
import { PointOfInterestService } from './PointOfInterest.service';
import { CreatePointAndTradDto } from './dto/create-pointAndTraduction.dto';
import { ApiBearerAuth, ApiConsumes, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';
@@ -36,10 +46,8 @@ export class PointOfInterestController {

  @ApiQuery({ name: 'lang', type: String })
  @Get('point/:idPoint/audio')
  async getAudio(
    @Param('idPoint') idPoint: number,
    @Query('lang') lang: string,
  ): Promise<Promise<NodeJS.ReadableStream>> {
    return this.activityService.getAudio(idPoint, lang as LANGUAGES);
  async getAudio(@Param('idPoint') idPoint: number, @Query('lang') lang: string): Promise<StreamableFile> {
    const file = await this.activityService.getAudio(idPoint, lang as LANGUAGES);
    return file;
  }
}
+12 −5
Original line number Diff line number Diff line
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { BadRequestException, Injectable, NotFoundException, StreamableFile } from '@nestjs/common';
import { CreatePointAndTradDto } from './dto/create-pointAndTraduction.dto';
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
import { PointOfInterest } from './entities/PointOfInterest.entity';
@@ -11,6 +11,7 @@ import { LANGUAGES } from 'src/shared/enum/languages.enum';
import { getPointDto } from './dto/getPoint.dto';
import { ServerConstants } from 'src/constants/server.contants';
import { createReadStream } from 'fs';
import { join } from 'path';

@Injectable()
export class PointOfInterestService {
@@ -87,12 +88,18 @@ export class PointOfInterestService {
    return points;
  }

  async getAudio(idPoint: number, lang: LANGUAGES): Promise<NodeJS.ReadableStream> {
    const pointTrad = await this.pointTraductionRepository.findOneBy({ idPoint, language: lang });
  async getAudio(idPoint: number, lang: LANGUAGES): Promise<StreamableFile> {
    const pointTrad: PointOfInterestTraduction = await this.pointTraductionRepository.findOneBy({
      idPoint,
      language: lang,
    });
    if (!pointTrad) {
      throw new NotFoundException('Point not found');
    }
    const filePath = `${ServerConstants.HOST}/audios/${pointTrad.audioName}`;
    return createReadStream(filePath);
    const filePath = join(ServerConstants.ROOT_STATIC_PATH, 'audios', pointTrad.audioName);
    const fileStream = createReadStream(filePath);
    return new StreamableFile(fileStream);
  }

  async createAudio() {}
}