Skip to content

Commit

Permalink
[improve] add 'default_supported_formats' config
Browse files Browse the repository at this point in the history
  • Loading branch information
VecHK committed Jan 6, 2024
1 parent bb79fa0 commit 0007c3f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
13 changes: 9 additions & 4 deletions server/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 = {
Expand All @@ -75,6 +76,10 @@ module.exports = appInfo => {

default_image_thumb_size: 640,

popularly_formats,
next_gen_formats,
supported_formats,

imageThumbSavePath,
imageSavePath,

Expand Down
Binary file added server/test/1x1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 12 additions & 18 deletions server/test/image-upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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')

Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 0007c3f

Please sign in to comment.