diff --git a/src/app.module.ts b/src/app.module.ts index 8ae99b0..f5f4421 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -3,10 +3,11 @@ import { RawParserMiddleware } from './raw-parser.middleware'; import { ScenesController } from './scenes/scenes.controller'; import { StorageService } from './storage/storage.service'; import { RoomsController } from './rooms/rooms.controller'; +import { FilesController } from './files/files.controller'; @Module({ imports: [], - controllers: [ScenesController, RoomsController], + controllers: [ScenesController, RoomsController, FilesController], providers: [StorageService], }) export class AppModule { diff --git a/src/files/files.controller.spec.ts b/src/files/files.controller.spec.ts new file mode 100644 index 0000000..0358f0a --- /dev/null +++ b/src/files/files.controller.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { FilesController } from './files.controller'; + +describe('FilesController', () => { + let controller: FilesController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [FilesController], + }).compile(); + + controller = module.get(FilesController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/src/files/files.controller.ts b/src/files/files.controller.ts new file mode 100644 index 0000000..cc8c5aa --- /dev/null +++ b/src/files/files.controller.ts @@ -0,0 +1,49 @@ +import { + Body, + Controller, + Get, + Header, + Logger, + NotFoundException, + Param, + Put, + Res, +} from '@nestjs/common'; +import { Response } from 'express'; +import { StorageNamespace, StorageService } from 'src/storage/storage.service'; +import { Readable } from 'stream'; + +@Controller('files') +export class FilesController { + private readonly logger = new Logger(FilesController.name); + namespace = StorageNamespace.FILES; + + constructor(private storageService: StorageService) {} + + @Get(':id') + @Header('content-type', 'application/octet-stream') + async findOne(@Param() params, @Res() res: Response): Promise { + const data = await this.storageService.get(params.id, this.namespace); + this.logger.debug(`Get image ${params.id}`); + + if (!data) { + throw new NotFoundException(); + } + + const stream = new Readable(); + stream.push(data); + stream.push(null); + stream.pipe(res); + } + + @Put(':id') + async create(@Param() params, @Body() payload: Buffer) { + const id = params.id; + await this.storageService.set(id, payload, this.namespace); + this.logger.debug(`Created image ${id}`); + + return { + id, + }; + } +} diff --git a/src/rooms/rooms.controller.ts b/src/rooms/rooms.controller.ts index 9014789..b2fa499 100644 --- a/src/rooms/rooms.controller.ts +++ b/src/rooms/rooms.controller.ts @@ -40,7 +40,7 @@ export class RoomsController { async create(@Param() params, @Body() payload: Buffer) { const id = params.id; await this.storageService.set(id, payload, this.namespace); - this.logger.debug(`Created scene ${id}`); + this.logger.debug(`Created room ${id}`); return { id, diff --git a/src/storage/storage.service.ts b/src/storage/storage.service.ts index ddf4379..6201d1e 100644 --- a/src/storage/storage.service.ts +++ b/src/storage/storage.service.ts @@ -39,4 +39,5 @@ export class StorageService { export enum StorageNamespace { SCENES = 'SCENES', ROOMS = 'ROOMS', + FILES = 'FILES', }