Commit 10500f04 authored by Lorenzo Trujillo Rojas's avatar Lorenzo Trujillo Rojas
Browse files

Merge branch 'main' into 'main'

Cambiando la forma en la que se sirven audios, agregando http:// al regresar una url de un statico

See merge request ltrpro/pueblosmagicosconia!34
parents 112e8009 96e5a213
Loading
Loading
Loading
Loading
+566 −5

File changed.

Preview size limit exceeded, changes collapsed.

+2 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@google-cloud/text-to-speech": "^5.3.0",
    "@nestjs/common": "^10.0.0",
    "@nestjs/core": "^10.0.0",
    "@nestjs/jwt": "^10.2.0",
@@ -31,6 +32,7 @@
    "bcrypt": "^5.1.1",
    "dotenv": "^16.4.5",
    "mysql2": "^3.9.2",
    "network": "^0.7.0",
    "reflect-metadata": "^0.2.0",
    "rxjs": "^7.8.1",
    "typeorm": "^0.3.20"
+5 −2
Original line number Diff line number Diff line
import * as dotenv from 'dotenv';
import * as network from 'network';
import { join } from 'path';

let publicIp: string;

@@ -8,11 +9,13 @@ dotenv.config();
export class ServerConstants {
  static PORT: number = process.env.SERVER_PORT ? parseInt(process.env.SERVER_PORT) : 3003;
  static IP: string = publicIp || 'http://localhost';
  static HOST: string = `${publicIp || 'http://localhost'}:${this.PORT}`;
  static HOST: string = `http://${publicIp || 'localhost'}:${this.PORT}`;
  static ROOT_PATH: string = join(__dirname, '..', '..');
  static ROOT_STATIC_PATH: string = join(__dirname, '..', '..', 'static');
}

network.get_private_ip(function (err, ip) {
  publicIp = ip;
  ServerConstants.IP = publicIp;
  ServerConstants.HOST = `${publicIp}:${ServerConstants.PORT}`;
  ServerConstants.HOST = `http://${publicIp || 'localhost'}:${ServerConstants.PORT}`;
});
+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() {}
}