feat: add eager image processing functions (generateAllSizes, convertOriginalToWebp)
This commit is contained in:
@@ -97,3 +97,65 @@ describe('image-resize', () => {
|
||||
await fs.promises.unlink(first.path)
|
||||
})
|
||||
})
|
||||
|
||||
describe('eager image processing', () => {
|
||||
it('generateAllSizes creates all width+format combinations', async () => {
|
||||
const { generateAllSizes } = await import('../image-resize.js')
|
||||
const sharp = (await import('sharp')).default
|
||||
const uuid = 'test-eager-uuid-1'
|
||||
const testImagePath = path.join(UPLOADS_DIR, `${uuid}.png`)
|
||||
await sharp({ create: { width: 2000, height: 1500, channels: 3, background: { r: 255, g: 0, b: 0 } } })
|
||||
.png()
|
||||
.toFile(testImagePath)
|
||||
|
||||
await generateAllSizes(uuid, '', testImagePath)
|
||||
|
||||
const cacheDir = path.join(UPLOADS_DIR, '.cache')
|
||||
for (const width of [320, 640, 1024, 1600]) {
|
||||
for (const format of ['avif', 'webp']) {
|
||||
const cachePath = path.join(cacheDir, `${uuid}_w${width}.${format}`)
|
||||
const exists = await fs.promises.access(cachePath).then(() => true).catch(() => false)
|
||||
expect(exists).toBe(true)
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
await fs.promises.unlink(testImagePath)
|
||||
for (const width of [320, 640, 1024, 1600]) {
|
||||
for (const format of ['avif', 'webp']) {
|
||||
const cachePath = path.join(cacheDir, `${uuid}_w${width}.${format}`)
|
||||
try {
|
||||
await fs.promises.unlink(cachePath)
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('convertOriginalToWebp converts and deletes original', async () => {
|
||||
const { convertOriginalToWebp } = await import('../image-resize.js')
|
||||
const sharp = (await import('sharp')).default
|
||||
const uuid = 'test-eager-uuid-2'
|
||||
const testImagePath = path.join(UPLOADS_DIR, `${uuid}.png`)
|
||||
await sharp({ create: { width: 800, height: 600, channels: 3, background: { r: 0, g: 255, b: 0 } } })
|
||||
.png()
|
||||
.toFile(testImagePath)
|
||||
|
||||
const result = await convertOriginalToWebp(uuid, '')
|
||||
|
||||
expect(result).toBe(`/uploads/${uuid}.webp`)
|
||||
const pngExists = await fs.promises.access(testImagePath).then(() => true).catch(() => false)
|
||||
expect(pngExists).toBe(false)
|
||||
const webpPath = path.join(UPLOADS_DIR, `${uuid}.webp`)
|
||||
const webpExists = await fs.promises.access(webpPath).then(() => true).catch(() => false)
|
||||
expect(webpExists).toBe(true)
|
||||
|
||||
// Cleanup
|
||||
try {
|
||||
await fs.promises.unlink(webpPath)
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user