feat: add file support

This commit is contained in:
Kilian Decaderincourt 2021-11-28 17:48:16 +01:00
parent 6b525d0f64
commit 77479b8a5d
5 changed files with 71 additions and 2 deletions

View File

@ -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 {

View File

@ -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>(FilesController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@ -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<void> {
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,
};
}
}

View File

@ -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,

View File

@ -39,4 +39,5 @@ export class StorageService {
export enum StorageNamespace {
SCENES = 'SCENES',
ROOMS = 'ROOMS',
FILES = 'FILES',
}