From a2a6eef5f0c245b3126ff2d401fb960af321a2a9 Mon Sep 17 00:00:00 2001 From: fourgate Date: Sat, 28 Dec 2024 13:08:28 +0100 Subject: [PATCH] feat: move file uploading api into dedicated app audio-file-uploader --- apps/audio-file-uploader/.env.dev | 1 + apps/audio-file-uploader/.env.e2e-tests | 1 + apps/audio-file-uploader/.env.production | 2 ++ .../src/app/app.controller.spec.ts | 8 ++++++++ .../src/app}/fileUpload/FileUploadService.ts | 0 apps/rpg-maestro-ui-e2e/project.json | 8 +++++++- apps/rpg-maestro-ui/.env.development | 3 ++- apps/rpg-maestro-ui/.env.e2e-tests | 3 ++- apps/rpg-maestro-ui/.env.production | 3 ++- apps/rpg-maestro-ui/project.json | 1 + .../admin-ui/tracks-management/file-upload.tsx | 4 ++-- apps/rpg-maestro/.env.production | 1 - apps/rpg-maestro/src/app/app.controller.ts | 16 ++-------------- apps/rpg-maestro/src/app/app.module.ts | 15 +-------------- 14 files changed, 31 insertions(+), 35 deletions(-) create mode 100644 apps/audio-file-uploader/.env.dev create mode 100644 apps/audio-file-uploader/.env.e2e-tests create mode 100644 apps/audio-file-uploader/.env.production create mode 100644 apps/audio-file-uploader/src/app/app.controller.spec.ts rename apps/{rpg-maestro/src/app/infrastructure => audio-file-uploader/src/app}/fileUpload/FileUploadService.ts (100%) diff --git a/apps/audio-file-uploader/.env.dev b/apps/audio-file-uploader/.env.dev new file mode 100644 index 0000000..a43d6bb --- /dev/null +++ b/apps/audio-file-uploader/.env.dev @@ -0,0 +1 @@ +PORT=3001 \ No newline at end of file diff --git a/apps/audio-file-uploader/.env.e2e-tests b/apps/audio-file-uploader/.env.e2e-tests new file mode 100644 index 0000000..0277ab0 --- /dev/null +++ b/apps/audio-file-uploader/.env.e2e-tests @@ -0,0 +1 @@ +PORT=8098 \ No newline at end of file diff --git a/apps/audio-file-uploader/.env.production b/apps/audio-file-uploader/.env.production new file mode 100644 index 0000000..007a0d9 --- /dev/null +++ b/apps/audio-file-uploader/.env.production @@ -0,0 +1,2 @@ +DATABASE=firestore +FILESERVER_PATH=/usr/share/public/musics \ No newline at end of file diff --git a/apps/audio-file-uploader/src/app/app.controller.spec.ts b/apps/audio-file-uploader/src/app/app.controller.spec.ts new file mode 100644 index 0000000..1e2281b --- /dev/null +++ b/apps/audio-file-uploader/src/app/app.controller.spec.ts @@ -0,0 +1,8 @@ + +describe('AppController', () => { + describe('getData', () => { + it('should return "Hello API"', () => { + expect(1).toEqual(1); + }); + }); +}); diff --git a/apps/rpg-maestro/src/app/infrastructure/fileUpload/FileUploadService.ts b/apps/audio-file-uploader/src/app/fileUpload/FileUploadService.ts similarity index 100% rename from apps/rpg-maestro/src/app/infrastructure/fileUpload/FileUploadService.ts rename to apps/audio-file-uploader/src/app/fileUpload/FileUploadService.ts diff --git a/apps/rpg-maestro-ui-e2e/project.json b/apps/rpg-maestro-ui-e2e/project.json index e1bec1b..4647470 100644 --- a/apps/rpg-maestro-ui-e2e/project.json +++ b/apps/rpg-maestro-ui-e2e/project.json @@ -5,5 +5,11 @@ "sourceRoot": "apps/rpg-maestro-ui-e2e/src", "implicitDependencies": ["rpg-maestro-ui"], "// targets": "to see all targets run: nx show project rpg-maestro-ui-e2e --web", - "targets": {} + "targets": { + "e2e": { + "dependsOn": [ + "rpg-maestro:build", "audio-file-uploader:build", "rpg-maestro-ui:build" + ] + } + } } diff --git a/apps/rpg-maestro-ui/.env.development b/apps/rpg-maestro-ui/.env.development index 9c42a5e..67cb6da 100644 --- a/apps/rpg-maestro-ui/.env.development +++ b/apps/rpg-maestro-ui/.env.development @@ -1 +1,2 @@ -VITE_RPG_MAESTRO_API_URL=http://localhost:3000 \ No newline at end of file +VITE_RPG_MAESTRO_API_URL=http://localhost:3000 +VITE_AUDIO_FILE_UPLOADER_API_URL=http://localhost:3001/api \ No newline at end of file diff --git a/apps/rpg-maestro-ui/.env.e2e-tests b/apps/rpg-maestro-ui/.env.e2e-tests index b162f15..0841508 100644 --- a/apps/rpg-maestro-ui/.env.e2e-tests +++ b/apps/rpg-maestro-ui/.env.e2e-tests @@ -1 +1,2 @@ -VITE_RPG_MAESTRO_API_URL=http://localhost:8099 \ No newline at end of file +VITE_RPG_MAESTRO_API_URL=http://localhost:8099 +VITE_AUDIO_FILE_UPLOADER_API_URL=http://localhost:8098/api \ No newline at end of file diff --git a/apps/rpg-maestro-ui/.env.production b/apps/rpg-maestro-ui/.env.production index 9c01561..32f3409 100644 --- a/apps/rpg-maestro-ui/.env.production +++ b/apps/rpg-maestro-ui/.env.production @@ -1 +1,2 @@ -VITE_RPG_MAESTRO_API_URL=https://fourgate.cloud/api \ No newline at end of file +VITE_RPG_MAESTRO_API_URL=https://fourgate.cloud/api +VITE_AUDIO_FILE_UPLOADER_API_URL=https://fourgate.cloud/audio-file-uploader/api \ No newline at end of file diff --git a/apps/rpg-maestro-ui/project.json b/apps/rpg-maestro-ui/project.json index a0e76f8..5932f4c 100644 --- a/apps/rpg-maestro-ui/project.json +++ b/apps/rpg-maestro-ui/project.json @@ -11,6 +11,7 @@ "options": { "commands": [ "PORT=8099 npx nx run rpg-maestro:dev --configuration=e2e-tests --no-cloud", + "PORT=8098 npx nx run audio-file-uploader:dev --configuration=e2e-tests --no-cloud", "npx nx run rpg-maestro-ui:preview --configuration=e2e-tests --no-cloud" ] } diff --git a/apps/rpg-maestro-ui/src/app/admin-ui/tracks-management/file-upload.tsx b/apps/rpg-maestro-ui/src/app/admin-ui/tracks-management/file-upload.tsx index aab0362..947b565 100644 --- a/apps/rpg-maestro-ui/src/app/admin-ui/tracks-management/file-upload.tsx +++ b/apps/rpg-maestro-ui/src/app/admin-ui/tracks-management/file-upload.tsx @@ -4,7 +4,7 @@ import React, { useState } from 'react'; import { styled } from '@mui/material/styles'; import axios, { AxiosRequestConfig } from 'axios'; -const rpgmaestroapiurl = import.meta.env.VITE_RPG_MAESTRO_API_URL; // TODO centralize +const audioFileUploaderAPI = import.meta.env.VITE_AUDIO_FILE_UPLOADER_API_URL; const VisuallyHiddenInput = styled('input')({ clip: 'rect(0 0 0 0)', @@ -45,7 +45,7 @@ export function FileUpload(props: FileUploadProps) { }; axios - .post(`${rpgmaestroapiurl}/admin/tracks/upload`, formData, config) + .post(`${audioFileUploaderAPI}/upload/audio`, formData, config) .then((response) => { console.log(response.data); const uploadRes = response.data as {fileURL: string}; diff --git a/apps/rpg-maestro/.env.production b/apps/rpg-maestro/.env.production index 007a0d9..f252e8b 100644 --- a/apps/rpg-maestro/.env.production +++ b/apps/rpg-maestro/.env.production @@ -1,2 +1 @@ DATABASE=firestore -FILESERVER_PATH=/usr/share/public/musics \ No newline at end of file diff --git a/apps/rpg-maestro/src/app/app.controller.ts b/apps/rpg-maestro/src/app/app.controller.ts index 520de6d..0f3d705 100644 --- a/apps/rpg-maestro/src/app/app.controller.ts +++ b/apps/rpg-maestro/src/app/app.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Get, Inject, Param, Post, Put, UploadedFile, UseInterceptors } from '@nestjs/common'; +import { Body, Controller, Get, Inject, Param, Post, Put } from '@nestjs/common'; import { TrackService } from './admin-api/TrackService'; import { Database } from './admin-api/Database'; import { ManageCurrentlyPlayingTracks } from './admin-api/ManageCurrentlyPlayingTracks'; @@ -13,10 +13,6 @@ import { import { DEFAULT_CURRENT_SESSION_ID, FirestoreDatabase } from './infrastructure/FirestoreDatabase'; import * as process from 'node:process'; import { InMemoryDatabase } from './infrastructure/InMemoryDatabase'; -import { FileInterceptor } from '@nestjs/platform-express'; -import { Express } from 'express'; -import 'multer'; -import { FileUploadService } from './infrastructure/fileUpload/FileUploadService'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache, Milliseconds } from 'cache-manager'; @@ -29,10 +25,8 @@ export class AppController { private readonly manageCurrentlyPlayingTracks: ManageCurrentlyPlayingTracks; constructor( - private readonly fileUploadService: FileUploadService, @Inject(CACHE_MANAGER) private cacheManager: Cache ) { - this.fileUploadService = fileUploadService; const databaseImpl: string | undefined = process.env.DATABASE; if (databaseImpl === 'firestore') { console.log('using firestore as database'); @@ -68,12 +62,6 @@ export class AppController { return this.trackService.getAll(); } - @Post('/admin/tracks/upload') - @UseInterceptors(FileInterceptor('file')) - uploadTrack(@UploadedFile() file: Express.Multer.File) { - return this.fileUploadService.handleFileUpload(file); - } - @Put('/admin/sessions/current/tracks') async changeCurrentTrack(@Body() trackToPlay: TrackToPlay): Promise { const playingTrack = await this.manageCurrentlyPlayingTracks.changeCurrentTrack(trackToPlay); @@ -86,7 +74,7 @@ export class AppController { return this.trackService.get(id); } - @Get ('/sessions/current/tracks') + @Get('/sessions/current/tracks') async getCurrentTrack(): Promise { // TODO fix this hack forbidding having more than one instance // this was done to avoid reaching Firestore quotas diff --git a/apps/rpg-maestro/src/app/app.module.ts b/apps/rpg-maestro/src/app/app.module.ts index 92fe79a..3579f8e 100644 --- a/apps/rpg-maestro/src/app/app.module.ts +++ b/apps/rpg-maestro/src/app/app.module.ts @@ -3,10 +3,6 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { ServeStaticModule } from '@nestjs/serve-static'; import { join } from 'path'; -import { MulterModule } from '@nestjs/platform-express'; -import { diskStorage } from 'multer'; -import { FileUploadService } from './infrastructure/fileUpload/FileUploadService'; -import * as process from 'node:process'; import { CacheModule } from '@nestjs/cache-manager'; @Module({ @@ -17,18 +13,9 @@ import { CacheModule } from '@nestjs/cache-manager'; serveStaticOptions: { redirect: false }, }), - MulterModule.register({ - storage: diskStorage({ - destination: `${process.env.FILESERVER_PATH ? process.env.FILESERVER_PATH : '.'}/uploads`, - filename: (req, file, cb) => { - const filename = `${file.originalname}`; - cb(null, filename); - }, - }), - }), CacheModule.register(), ], controllers: [AppController], - providers: [FileUploadService], + providers: [], }) export class AppModule {}