From 58d29f4d6254c5feb48bbf92bc86f7e49e2ea5b7 Mon Sep 17 00:00:00 2001 From: Yechao LI Date: Mon, 24 Jul 2023 14:13:46 +0800 Subject: [PATCH] fix: avoid concurrent webp converting (#46) --- CHANGELOG.md | 4 ++++ api/index.js | 11 ++++++++++- blocklet.yml | 2 +- package.json | 2 +- version | 2 +- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34cf647..9fcb9ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.9.3 (July 24, 2023) + +- fix: avoid concurrent webp converting + ## 0.9.2 (July 24, 2023) - fix: convert to webp using query params diff --git a/api/index.js b/api/index.js index 785df53..d57a929 100644 --- a/api/index.js +++ b/api/index.js @@ -25,6 +25,9 @@ app.use(cookieParser()); app.use(express.json({ limit: env.maxUploadSize })); app.use(express.urlencoded({ extended: true, limit: env.maxUploadSize })); +// Tasks of converting webp +const webpTasks = {}; + // Convert images to webp on the fly // eslint-disable-next-line consistent-return app.use('/uploads/:filename', (req, res, next) => { @@ -54,7 +57,13 @@ app.use('/uploads/:filename', (req, res, next) => { } // do the convert - any2webp(srcPath, destPath) + webpTasks[destPath] ??= any2webp(srcPath, destPath).finally(() => { + setTimeout(() => { + delete webpTasks[destPath]; + }, 1000); + }); + + webpTasks[destPath] .then(() => { logger.info(`Converted ${srcPath} to webp`); res.sendFile(destPath, { maxAge: '356d', immutable: true }); diff --git a/blocklet.yml b/blocklet.yml index ce85150..8f24e98 100644 --- a/blocklet.yml +++ b/blocklet.yml @@ -1,5 +1,5 @@ name: image-bin -version: 0.9.2 +version: 0.9.3 title: Image Bin description: Paste and share your images keywords: diff --git a/package.json b/package.json index 0546487..e96774a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "image-bin", - "version": "0.9.2", + "version": "0.9.3", "scripts": { "dev": "blocklet dev", "build": "vite build", diff --git a/version b/version index 2003b63..965065d 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.9.2 +0.9.3