feat(transcode): update preview thumbnail resolution to 300p with max length constraint (#281)

* feat(transcode): update preview thumbnail resolution to 300p with max length constraint

* fix(transcode): address code review feedback on preview thumbnail transcoding
This commit is contained in:
Yiling-J
2026-08-08 15:17:51 +08:00
committed by GitHub
parent c91867b8eb
commit ee4933a4f8
10 changed files with 179 additions and 24 deletions
+88 -6
View File
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { transcodeService } from './transcode'
import { transcodeService, calculatePreviewDimensions } from './transcode'
import { s3Service } from '@shumai/core/src/s3/s3'
import * as path from 'path'
import * as child_process from 'child_process'
@@ -185,18 +185,100 @@ describe('TranscodeService', () => {
)
})
it('should use sharp for image transcoding with sRGB conversion', async () => {
it('should use sharp for image transcoding with sRGB conversion (production 300p spec)', async () => {
const outputFile = path.join(tempDir, 'output.webp')
await transcodeService.transcodeImage('input.png', outputFile, 480, 80)
await transcodeService.transcodeImage('input.png', outputFile, 300, 80, { isPreview: true })
expect(sharp).toHaveBeenCalledWith('input.png', { limitInputPixels: false })
const mockSharp = vi.mocked(sharp).mock.results[0].value
expect(mockSharp.toColorspace).toHaveBeenCalledWith('srgb')
expect(mockSharp.resize).toHaveBeenCalledWith(480, 7680, expect.any(Object))
expect(mockSharp.resize).toHaveBeenCalledWith(400, 300, expect.any(Object))
expect(mockSharp.webp).toHaveBeenCalledWith({ quality: 80 })
expect(mockSharp.toFile).toHaveBeenCalledWith(outputFile)
})
it('should support legacy 480 width fallback shim', async () => {
const outputFile = path.join(tempDir, 'output_legacy.webp')
await transcodeService.transcodeImage('input.png', outputFile, 480, 80)
const mockSharp = vi.mocked(sharp).mock.results[vi.mocked(sharp).mock.results.length - 1].value
expect(mockSharp.resize).toHaveBeenCalledWith(400, 300, expect.any(Object))
})
it('should handle preview and full-res proxy dimension calculations correctly', async () => {
const outputFile = path.join(tempDir, 'out.webp')
// 16:9 source (1920x1080) in preview mode -> 533x300
const mockSharp169 = {
resize: vi.fn().mockReturnThis(),
toColorspace: vi.fn().mockReturnThis(),
webp: vi.fn().mockReturnThis(),
toFile: vi.fn().mockResolvedValue({}),
metadata: vi.fn().mockResolvedValue({ width: 1920, height: 1080 }),
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(sharp).mockReturnValueOnce(mockSharp169 as any)
await transcodeService.transcodeImage('169.png', outputFile, 300, 80, { isPreview: true })
expect(mockSharp169.resize).toHaveBeenCalledWith(533, 300, expect.any(Object))
// Small image (200x150) in preview mode -> 200x150
const mockSharpSmall = {
resize: vi.fn().mockReturnThis(),
toColorspace: vi.fn().mockReturnThis(),
webp: vi.fn().mockReturnThis(),
toFile: vi.fn().mockResolvedValue({}),
metadata: vi.fn().mockResolvedValue({ width: 200, height: 150 }),
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(sharp).mockReturnValueOnce(mockSharpSmall as any)
await transcodeService.transcodeImage('small.png', outputFile, 300, 80, { isPreview: true })
expect(mockSharpSmall.resize).toHaveBeenCalledWith(200, 150, expect.any(Object))
// 1:10 Tall screenshot (1000x10000) in preview mode -> 53x533
const mockSharpTall = {
resize: vi.fn().mockReturnThis(),
toColorspace: vi.fn().mockReturnThis(),
webp: vi.fn().mockReturnThis(),
toFile: vi.fn().mockResolvedValue({}),
metadata: vi.fn().mockResolvedValue({ width: 1000, height: 10000 }),
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(sharp).mockReturnValueOnce(mockSharpTall as any)
await transcodeService.transcodeImage('tall.png', outputFile, 300, 80, { isPreview: true })
expect(mockSharpTall.resize).toHaveBeenCalledWith(53, 533, expect.any(Object))
// Square full-resolution proxy (10000x10000, isPreview: false) -> capped at WEBP_MAX_DIMENSION (7680)
const mockSharpSquareProxy = {
resize: vi.fn().mockReturnThis(),
toColorspace: vi.fn().mockReturnThis(),
webp: vi.fn().mockReturnThis(),
toFile: vi.fn().mockResolvedValue({}),
metadata: vi.fn().mockResolvedValue({ width: 10000, height: 10000 }),
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(sharp).mockReturnValueOnce(mockSharpSquareProxy as any)
await transcodeService.transcodeImage('square.png', outputFile, 10000, 90, {
height: 10000,
isPreview: false,
})
expect(mockSharpSquareProxy.resize).toHaveBeenCalledWith(7680, 7680, expect.any(Object))
})
it('should calculate preview dimensions correctly for 300p (short side 300, max long side 533)', () => {
// 16:9 Landscape
expect(calculatePreviewDimensions(1920, 1080)).toEqual({ width: 533, height: 300 })
// 9:16 Portrait
expect(calculatePreviewDimensions(1080, 1920)).toEqual({ width: 300, height: 533 })
// 1:1 Square
expect(calculatePreviewDimensions(1000, 1000)).toEqual({ width: 300, height: 300 })
// 21:9 Ultrawide
expect(calculatePreviewDimensions(2560, 1080)).toEqual({ width: 533, height: 225 })
// 1:10 Tall screenshot (capped at max long side = 533)
expect(calculatePreviewDimensions(1000, 10000)).toEqual({ width: 53, height: 533 })
// Small image (no enlargement)
expect(calculatePreviewDimensions(200, 150)).toEqual({ width: 200, height: 150 })
})
it('should use ImageMagick for PSD image transcoding', async () => {
const outputFile = path.join(tempDir, 'output_psd.webp')
const psdBuffer = Buffer.from('8BPS-fake-psd-content')
@@ -264,7 +346,7 @@ describe('TranscodeService', () => {
expect(sharp).toHaveBeenCalledWith(inputBuffer, { limitInputPixels: false })
const mockSharp = vi.mocked(sharp).mock.results[vi.mocked(sharp).mock.results.length - 1].value
expect(mockSharp.toColorspace).toHaveBeenCalledWith('srgb')
expect(mockSharp.resize).toHaveBeenCalledWith(480, 7680, expect.any(Object))
expect(mockSharp.resize).toHaveBeenCalledWith(400, 300, expect.any(Object))
expect(mockSharp.webp).toHaveBeenCalledWith({ quality: 80 })
expect(mockSharp.toFile).toHaveBeenCalledWith(outputFile)
})
@@ -451,7 +533,7 @@ describe('TranscodeService', () => {
)
expect(child_process.execFile).toHaveBeenCalledWith(
'ffmpeg',
expect.arrayContaining(['-filter_complex', 'scale=w=480:h=-2,tile=10x10']),
expect.arrayContaining(['-filter_complex', 'scale=w=300:h=-2,tile=10x10']),
expect.any(Function),
)
expect(child_process.execFile).toHaveBeenCalledWith(