Skip to content

Commit 7bad10b

Browse files
committed
perf(backend:files): use webp encoding for thumbnails
1 parent 7f0238c commit 7bad10b

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

backend/src/applications/files/files.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
UseGuards
2424
} from '@nestjs/common'
2525
import { FastifyReply } from 'fastify'
26-
import { pngMimeType } from '../../common/image'
26+
import { webpMimeType } from '../../common/image'
2727
import { SkipSpaceGuard } from '../spaces/decorators/space-skip-guard.decorator'
2828
import { SkipSpacePermissionsCheck } from '../spaces/decorators/space-skip-permissions.decorator'
2929
import { GetSpace } from '../spaces/decorators/space.decorator'
@@ -114,7 +114,7 @@ export class FilesController {
114114
): Promise<StreamableFile> {
115115
if (size > 1024) size = 1024
116116
const thumb = await this.filesMethods.genThumbnail(space, size)
117-
res.type(pngMimeType)
117+
res.type(webpMimeType)
118118
return res.send(thumb)
119119
}
120120

backend/src/common/image.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ sharp.concurrency(Math.min(2, os.cpus()?.length || 1))
1919
// Constants
2020
export const pngMimeType = 'image/png'
2121
export const svgMimeType = 'image/svg+xml'
22+
export const webpMimeType = 'image/webp'
2223
const avatarSize = 256
2324
const fontPath = path.join(__dirname, 'fonts', 'avatar.ttf')
2425
const loadTextToSVG = promisify(TextToSVG.load.bind(TextToSVG))
@@ -34,14 +35,11 @@ export async function generateThumbnail(filePath: string, size: number): Promise
3435
width: size,
3536
height: size,
3637
fit: 'inside',
37-
kernel: 'lanczos3',
38+
kernel: 'nearest',
3839
withoutEnlargement: true,
3940
fastShrinkOnLoad: true // true by default, added for clarity
4041
})
41-
.png({
42-
compressionLevel: 1,
43-
palette: true
44-
})
42+
.webp({ quality: 80, effort: 0, alphaQuality: 90 })
4543
}
4644

4745
export async function generateAvatar(initials: string): Promise<NodeJS.ReadableStream> {

0 commit comments

Comments
 (0)