From 0007c3fc88d617ea75c3c758f43cc255e09c0606 Mon Sep 17 00:00:00 2001 From: vec Date: Sat, 6 Jan 2024 16:38:20 +0800 Subject: [PATCH] [improve] add 'default_supported_formats' config --- server/config/config.default.js | 13 +++++++++---- server/test/1x1.gif | Bin 0 -> 43 bytes server/test/image-upload.test.js | 30 ++++++++++++------------------ 3 files changed, 21 insertions(+), 22 deletions(-) create mode 100644 server/test/1x1.gif diff --git a/server/config/config.default.js b/server/config/config.default.js index ff8defd..401dd61 100644 --- a/server/config/config.default.js +++ b/server/config/config.default.js @@ -40,6 +40,10 @@ module.exports = appInfo => { const imagePath = path.join(config.static.prefix, 'src'); const imageThumbPath = path.join(config.static.prefix, 'thumb'); + const popularly_formats = Object.freeze([ 'jpg', 'jpeg', 'png', 'gif' ]); + const next_gen_formats = Object.freeze([ 'avif', 'webp' ]); + const supported_formats = Object.freeze([ ...popularly_formats, ...next_gen_formats ]); + config.multipart = { fieldNameSize: 256, fieldSize: '15MB', @@ -52,10 +56,7 @@ module.exports = appInfo => { fileExtensions: [], // 如果希望覆盖框架内置的白名单,可以配置 whitelist 属性.当重写了 whitelist 时,fileExtensions 不生效。 - whitelist: [ - '.jpg', '.jpeg', '.png', '.gif', - '.bmp', '.webp', '.tif', '.avif', - ], + whitelist: supported_formats.map(format => `.${format}`), }; config.development = { @@ -75,6 +76,10 @@ module.exports = appInfo => { default_image_thumb_size: 640, + popularly_formats, + next_gen_formats, + supported_formats, + imageThumbSavePath, imageSavePath, diff --git a/server/test/1x1.gif b/server/test/1x1.gif new file mode 100644 index 0000000000000000000000000000000000000000..3c51d740ca8120d8d798f4b7060f586a1fdfdbff GIT binary patch literal 43 mcmZ?wbhEHbWMp7u_`m=Kia%KxK};PG0g_>0Vsc?*um%8taRh7t literal 0 HcmV?d00001 diff --git a/server/test/image-upload.test.js b/server/test/image-upload.test.js index 1d10d81..28c072a 100644 --- a/server/test/image-upload.test.js +++ b/server/test/image-upload.test.js @@ -78,16 +78,15 @@ describe('controller/admin/image', () => { assert(thumb_metadata.width === app.config.default_image_thumb_size) }) - const __SUPPORTED_FORMATS__ = ['webp', 'avif'] - - it('should successfully convert thumb to webp/avif fomat', async () => { + it.only('should successfully convert thumb to next-generation fomat', async () => { const { app, token } = await constructPlainEnvironment(true) + const { next_gen_formats } = app.config const { thumb, imageThumbPath } = await uploadImage(token, app, test_avatar_image_path) const [ default_thumb_meta ] = await loadImage(app, imageThumbPath, thumb) const thumb_name = path.parse(thumb).name - for (const format of __SUPPORTED_FORMATS__) { + for (const format of next_gen_formats) { const [downloaded_meta] = await loadImage(app, imageThumbPath, `${thumb_name}.${format}`) assert(downloaded_meta.width === default_thumb_meta.width) assert(downloaded_meta.height === default_thumb_meta.height) @@ -96,14 +95,15 @@ describe('controller/admin/image', () => { } }) - it('should successfully convert src to webp/avif fomat', async () => { + it.only('should successfully convert src to next-generation fomat', async () => { const { app, token } = await constructPlainEnvironment(true) + const { next_gen_formats } = app.config const { src, imagePath } = await uploadImage(token, app, test_avatar_image_path) const [ default_src_meta ] = await loadImage(app, imagePath, src) const image_filename = path.parse(src).name - for (const format of __SUPPORTED_FORMATS__) { + for (const format of next_gen_formats) { const [downloaded_meta] = await loadImage(app, imagePath, `${image_filename}.${format}`) assert(downloaded_meta.width === default_src_meta.width) assert(downloaded_meta.height === default_src_meta.height) @@ -112,7 +112,7 @@ describe('controller/admin/image', () => { } }) - it('should thumb always is .jpg format', async () => { + it.only('should thumb always is .jpg format', async () => { try { setEnvironmentSystem('2000/01/01 00:00:00') @@ -145,17 +145,11 @@ describe('controller/admin/image', () => { assert(/(.*)\.jpg/i.test(submission_photo.thumb)) } - setEnvironmentSystem('2000/01/01 00:00:01') - await testFormat('webp') - - setEnvironmentSystem('2000/01/01 00:00:02') - await testFormat('avif') - - setEnvironmentSystem('2000/01/01 00:00:03') - await testFormat('jpeg') - - setEnvironmentSystem('2000/01/01 00:00:04') - await testFormat('jpg') + const { supported_formats } = app.config + for (const [idx, format] of supported_formats.entries()) { + setEnvironmentSystem(`2000/01/01 00:00:${`${idx + 1}`.padStart(2, '0')}`) + await testFormat(format) + } } finally { resetEnvironmentDate() }