mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-22 08:28:35 +00:00
37 lines
965 B
TypeScript
37 lines
965 B
TypeScript
|
|
import {
|
||
|
|
Controller,
|
||
|
|
Get,
|
||
|
|
Param,
|
||
|
|
UseInterceptors,
|
||
|
|
ClassSerializerInterceptor,
|
||
|
|
Res,
|
||
|
|
StreamableFile,
|
||
|
|
UseGuards,
|
||
|
|
} from '@nestjs/common';
|
||
|
|
import { Readable } from 'stream';
|
||
|
|
import { Response } from 'express';
|
||
|
|
import { FilesService } from '../services/files.service';
|
||
|
|
import { JwtAuthGuard } from 'src/modules/auth/jwt-auth.guard';
|
||
|
|
|
||
|
|
@Controller('files')
|
||
|
|
@UseInterceptors(ClassSerializerInterceptor)
|
||
|
|
export class FilesController {
|
||
|
|
constructor(private readonly filesService: FilesService) {}
|
||
|
|
|
||
|
|
@Get(':id')
|
||
|
|
@UseGuards(JwtAuthGuard)
|
||
|
|
async show(@Param('id') id: string, @Res({ passthrough: true }) response: Response) {
|
||
|
|
const file = await this.filesService.findOne(id);
|
||
|
|
|
||
|
|
const stream = Readable.from(file.data);
|
||
|
|
|
||
|
|
response.set({
|
||
|
|
'Content-Disposition': `inline; filename="${file.filename}"`,
|
||
|
|
'Content-Type': 'image',
|
||
|
|
});
|
||
|
|
|
||
|
|
// https://docs.nestjs.com/techniques/streaming-files
|
||
|
|
return new StreamableFile(stream);
|
||
|
|
}
|
||
|
|
}
|