feat: add file support
This commit is contained in:
parent
6b525d0f64
commit
77479b8a5d
@ -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 {
|
||||
|
18
src/files/files.controller.spec.ts
Normal file
18
src/files/files.controller.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
49
src/files/files.controller.ts
Normal file
49
src/files/files.controller.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
@ -39,4 +39,5 @@ export class StorageService {
|
||||
export enum StorageNamespace {
|
||||
SCENES = 'SCENES',
|
||||
ROOMS = 'ROOMS',
|
||||
FILES = 'FILES',
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user