feat: add in memory storage for exported scenes

This commit is contained in:
Kilian Decaderincourt
2021-09-06 21:55:38 +02:00
parent de6a0efd3a
commit ad14da6af7
15 changed files with 171 additions and 51 deletions

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ScenesController } from './scenes.controller';
describe('ScenesController', () => {
let controller: ScenesController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ScenesController],
}).compile();
controller = module.get<ScenesController>(ScenesController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@ -0,0 +1,42 @@
import {
Body,
Controller,
Get,
Header,
Param,
Post,
Res,
} from '@nestjs/common';
import { Response } from 'express';
import { MemoryService } from 'src/storages/memory.service';
import { hash, hexadecimalToDecimal } from 'src/utils';
import { Readable } from 'stream';
@Controller()
export class ScenesController {
constructor(private storageService: MemoryService) {}
@Get(':id')
@Header('content-type', 'application/octet-stream')
async findOne(@Param() params, @Res() res: Response): Promise<void> {
const data = await this.storageService.load(params.id);
const stream = new Readable();
stream.push(data);
stream.push(null);
stream.pipe(res);
}
@Post()
async create(@Body() payload: Buffer) {
const drawingHash = hash(payload);
const id = hexadecimalToDecimal(drawingHash);
await this.storageService.save(id, payload);
return {
id,
data: `http://localhost:8080/api/v2/${id}`,
};
}
}