|
1 | 1 | import { Controller, Get, Header, Inject, Logger, Param, Res } from '@nestjs/common'; |
2 | 2 | import { Response } from 'express'; |
3 | | -import { join } from 'path'; |
4 | | -import sharp from 'sharp'; |
5 | | -import { FILE_SERVICE } from '../file-service.token'; |
6 | 3 | import { ImageFileService } from '../image-file/image-file.service'; |
7 | | -import { FileServiceInterface } from '../interfaces/file-service.interface'; |
| 4 | +import { FileService } from '../file-service.abstract'; |
8 | 5 |
|
9 | 6 | @Controller('file') |
10 | 7 | export class FileController { |
11 | 8 | private logger = new Logger(FileController.name); |
12 | 9 |
|
13 | 10 | constructor( |
14 | | - @Inject(FILE_SERVICE) |
15 | | - private readonly fileService: FileServiceInterface, |
| 11 | + @Inject() |
| 12 | + private readonly fileService: FileService, |
16 | 13 | private readonly imageService: ImageFileService, |
17 | 14 | ) {} |
18 | 15 |
|
19 | 16 | @Get(':fileName') |
20 | 17 | @Header('Cache-Control', 'public, max-age=86400') // public for CDN, max-age= 24hrs in seconds |
21 | | - get(@Param('fileName') fileName: string, @Res() response: Response) { |
22 | | - this.fileService.get(fileName).on('error', (err) => { |
| 18 | + async get(@Param('fileName') fileName: string, @Res() response: Response) { |
| 19 | + (await this.fileService.get(fileName)).on('error', (err) => { |
23 | 20 | this.logger.error(err); |
24 | 21 | response.status(500).send(err); |
25 | 22 | }).pipe(response); |
26 | 23 | } |
27 | 24 |
|
28 | | - @Get('watermark/:shareableId') |
| 25 | + @Get('watermark/:shareableId') |
29 | 26 | @Header('Cache-Control', 'public, max-age=86400') // public for CDN, max-age= 24hrs in seconds |
30 | 27 | @Header('content-type', 'image/jpeg') |
31 | | - async watermark(@Param('shareableId') shareableId: string, @Res() response: Response) { |
32 | | - const watermark = await sharp( |
33 | | - join(process.cwd(), 'public', 'assets', 'lazztech_icon.webp') |
34 | | - ).resize(150, 150) |
35 | | - .extend({ |
36 | | - top: 0, |
37 | | - bottom: 20, |
38 | | - left: 20, |
39 | | - right: 0, |
40 | | - background: { r: 0, g: 0, b: 0, alpha: 0 } |
41 | | - }) |
42 | | - .composite([ |
43 | | - { |
44 | | - input: Buffer.from([0, 0, 0, 200]), |
45 | | - raw: { |
46 | | - width: 1, |
47 | | - height: 1, |
48 | | - channels: 4, |
49 | | - }, |
50 | | - tile: true, |
51 | | - blend: 'dest-in', |
52 | | - } |
53 | | - ]).toBuffer(); |
| 28 | + async watermark( |
| 29 | + @Param('shareableId') shareableId: string, |
| 30 | + @Res() response: Response, |
| 31 | + ) { |
54 | 32 | const fileStream = await this.fileService.getByShareableId(shareableId); |
55 | | - fileStream.pipe( |
56 | | - sharp() |
57 | | - .jpeg() |
58 | | - .resize(1080, 1080, { fit: sharp.fit.inside }) |
59 | | - .composite([ |
60 | | - { input: watermark, gravity: 'southwest' }, |
61 | | - ]) |
62 | | - ).pipe(response).on('error', (err) => { |
| 33 | + const readable = await this.fileService.watermarkImage(fileStream); |
| 34 | + readable?.pipe(response).on('error', (err) => { |
63 | 35 | this.logger.error(err); |
64 | 36 | response.status(500).send(err); |
65 | 37 | }); |
|
0 commit comments