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

@ -1,22 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});

View File

@ -1,12 +0,0 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}

View File

@ -1,10 +1,15 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MiddlewareConsumer, Module } from '@nestjs/common';
import { RawParserMiddleware } from './raw-parser.middleware';
import { ScenesController } from './scenes/scenes.controller';
import { MemoryService } from './storages/memory.service';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
controllers: [ScenesController],
providers: [MemoryService],
})
export class AppModule {}
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(RawParserMiddleware).forRoutes('**');
}
}

View File

@ -1,8 +0,0 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}

View File

@ -2,7 +2,11 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
const app = await NestFactory.create(AppModule, {
cors: true,
});
app.setGlobalPrefix('api/v2');
await app.listen(8080);
}
bootstrap();

View File

@ -0,0 +1,7 @@
import { RawParserMiddleware } from './raw-parser.middleware';
describe('RawParserMiddleware', () => {
it('should be defined', () => {
expect(new RawParserMiddleware()).toBeDefined();
});
});

View File

@ -0,0 +1,14 @@
import { Injectable, NestMiddleware } from '@nestjs/common';
import { raw } from 'express';
import { hasBody } from 'type-is';
// Excalidraw Front end doesn't send a Content Type Header
// so we tell raw parser to check if there is a body
const rawParserMiddleware = raw({ type: hasBody });
@Injectable()
export class RawParserMiddleware implements NestMiddleware {
use(req: any, res: any, next: () => void) {
rawParserMiddleware(req, res, next);
}
}

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}`,
};
}
}

View File

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

View File

@ -0,0 +1,17 @@
import { Injectable } from '@nestjs/common';
import { StorageService } from './storageService';
@Injectable()
export class MemoryService implements StorageService {
scenesMap = new Map<string, Buffer>();
async save(id: string, data: Buffer): Promise<boolean> {
this.scenesMap.set(id, data);
return true;
}
async load(id: string): Promise<false | Buffer> {
return this.scenesMap.get(id);
}
}

View File

@ -0,0 +1,5 @@
export interface StorageService {
save(id: string, data: Buffer): Promise<boolean>;
load(id: string): Promise<Buffer | false>;
}

12
src/utils.ts Normal file
View File

@ -0,0 +1,12 @@
import { createHash } from 'crypto';
export function hash(buffer): string {
return createHash(`sha256`).update(buffer).digest(`hex`);
}
// Copied from https://github.com/NMinhNguyen/excalidraw-json
export function hexadecimalToDecimal(hexadecimal: string) {
// See https://stackoverflow.com/a/53751162
const bigInt = BigInt(`0x${hexadecimal}`);
return bigInt.toString(10);
}