Skip to content

Commit a4b94a1

Browse files
committed
perf(backend:files): disable sharp cache
1 parent 2d74512 commit a4b94a1

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

backend/src/applications/files/services/files-manager.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import archiver, { Archiver, ArchiverError } from 'archiver'
1010
import { AxiosResponse } from 'axios'
1111
import fs from 'node:fs'
1212
import path from 'node:path'
13+
import { Readable } from 'node:stream'
1314
import { pipeline } from 'node:stream/promises'
1415
import { extract as extractTar } from 'tar'
1516
import { FastifyAuthenticatedRequest } from '../../../authentication/interfaces/auth-request.interface'
@@ -573,7 +574,7 @@ export class FilesManager {
573574
}
574575
}
575576

576-
async generateThumbnail(space: SpaceEnv, size: number): Promise<Buffer> {
577+
async generateThumbnail(space: SpaceEnv, size: number): Promise<Readable> {
577578
if (!(await isPathExists(space.realPath))) {
578579
throw new FileError(HttpStatus.NOT_FOUND, 'Location not found')
579580
}

backend/src/applications/files/services/files-methods.service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import { HttpException, HttpStatus, Injectable, Logger, StreamableFile } from '@nestjs/common'
88
import { FastifyReply } from 'fastify'
99
import path from 'node:path'
10-
import { Readable } from 'node:stream'
1110
import { pngMimeType } from '../../../common/image'
1211
import { FastifySpaceRequest } from '../../spaces/interfaces/space-request.interface'
1312
import { SpaceEnv } from '../../spaces/models/space-env.model'
@@ -149,7 +148,7 @@ export class FilesMethods {
149148

150149
async genThumbnail(space: SpaceEnv, size: number): Promise<StreamableFile> {
151150
try {
152-
const pngStream = Readable.from(await this.filesManager.generateThumbnail(space, size))
151+
const pngStream = await this.filesManager.generateThumbnail(space, size)
153152
return new StreamableFile(pngStream, { type: pngMimeType })
154153
} catch (e) {
155154
this.handleError(space, this.genThumbnail.name, e)

backend/src/common/image.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
* See the LICENSE file for licensing details
55
*/
66

7-
import sharp from 'sharp'
87
import { Resvg } from '@resvg/resvg-js'
98
import fs from 'node:fs/promises'
109
import path from 'node:path'
10+
import { Readable } from 'node:stream'
11+
import sharp from 'sharp'
1112

1213
export const pngMimeType = 'image/png'
1314
export const svgMimeType = 'image/svg+xml'
15+
sharp.cache(false)
1416

15-
export async function generateThumbnail(filePath: string, size: number) {
16-
const image = sharp(filePath).rotate()
17+
export async function generateThumbnail(filePath: string, size: number): Promise<Readable> {
18+
const image = sharp(filePath, { autoOrient: true })
1719
let { width, height } = await image.metadata()
1820

1921
if (!width || !height) throw new Error('Invalid image dimensions')
@@ -31,7 +33,7 @@ export async function generateThumbnail(filePath: string, size: number) {
3133
}
3234
}
3335

34-
return image.resize(width, height, { fit: 'inside' }).png({ compressionLevel: 0 }).toBuffer()
36+
return image.resize(width, height, { fit: 'inside' }).png({ compressionLevel: 0 })
3537
}
3638

3739
export async function generateAvatar(initials: string): Promise<Buffer> {

0 commit comments

Comments
 (0)