From 08beca46402a7f77b60159b8d23d80363dbd3347 Mon Sep 17 00:00:00 2001 From: supine0703 Date: Sat, 3 Aug 2024 04:51:52 +0800 Subject: [PATCH] v1.1.1 If the key does not exist, the page will return 404 and throw an exception, marking the key -2 after anger, no more access --- package.json | 2 +- src/compress/tinypng/tinypng.ts | 23 ++++++++--------------- src/index.ts | 10 +++++++--- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index d61007e..25baf67 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "picgo-plugin-compress-next", - "version": "1.1.0", + "version": "1.1.1", "description": "Image compression plugin for PicGo(>=^2.3.0). Update, adapt and optimize. Better support and richer features", "main": "dist/index.js", "type": "commonjs", diff --git a/src/compress/tinypng/tinypng.ts b/src/compress/tinypng/tinypng.ts index 6f2fd80..69641a7 100644 --- a/src/compress/tinypng/tinypng.ts +++ b/src/compress/tinypng/tinypng.ts @@ -13,12 +13,11 @@ interface TinyPngOptions { } interface TinyCacheConfig { - [key: string]: { - key: string; - num: number; - }; + [key: string]: number; } +const errCodes = [-1, -2]; + class TinyPng { // private cacheConfigPath = join(dirname(fileURLToPath(import.meta.url)), 'config.json'); // ES6 private cacheConfigPath = join(__dirname, 'config.json'); // CommonJs @@ -48,7 +47,7 @@ class TinyPng { // Get key with available usage count private async getKey() { const config = await this.readOrWriteConfig(); - const innerKeys = Object.keys(config).filter((key) => config[key].num >= 0); + const innerKeys = Object.keys(config).filter((key) => !errCodes.includes(config[key])); if (innerKeys.length <= 0) { throw new Error('No available keys'); } @@ -95,13 +94,13 @@ class TinyPng { return getImageBuffer(this.IPicGo, response.headers.location as any); } if (response.statusCode === 429) { - this.setConfig(options.key, -1); + this.setConfig(options.key, errCodes[0]); return this.upload(options.originalUrl); } }) .catch((err) => { this.IPicGo.log.warn('this key is invalid, status code:', err.response.statusCode || err.response.status); - return this.setConfig(options.key, -2).then(() => { + return this.setConfig(options.key, errCodes[1]).then(() => { return this.upload(options.originalUrl) as any; }); }); @@ -110,10 +109,7 @@ class TinyPng { // Set configuration with key and usage count private async setConfig(key: string, num: number) { const config = await this.readOrWriteConfig(); - config[key] = { - key, - num, - }; + config[key] = num; await fs.writeJSON(this.cacheConfigPath, config); } @@ -130,10 +126,7 @@ class TinyPng { this.cacheConfigPath, keys.reduce((res, key) => { if (!res[key]) { - res[key] = { - key, - num: 0, - }; + res[key] = 0; } return res; }, config), diff --git a/src/index.ts b/src/index.ts index 935e42d..c8bef97 100644 --- a/src/index.ts +++ b/src/index.ts @@ -73,14 +73,15 @@ const handle = async (ctx: IPicGo): Promise => { // Export plugin function const CompressTransformers: IPicGoPlugin = (ctx: IPicGo) => { return { - transformer: 'compress-next', + transformer: PROJ_CONF, register(ctx: IPicGo) { // Register compression transformer - ctx.helper.transformer.register('compress-next', { handle }); + ctx.helper.transformer.register(PROJ_CONF, { handle }); }, config(ctx: IPicGo): IPluginConfig[] { let config: IConfig = getConfig(ctx); + // input, confirm, password, list, checkbox return [ { name: 'compress', @@ -93,9 +94,12 @@ const CompressTransformers: IPicGoPlugin = (ctx: IPicGo) => { { name: 'key', type: 'input', - message: 'Enter API key(s). Leave blank to use Web API. Separate multiple keys with commas.', + message: 'Enter API key(s). This is required if tinypng. Separate multiple keys with commas.', default: config?.key || config?.tinypngKey || null, required: false, + when(answer: any): boolean { + return answer.compress === CompressType.tinypng; + }, }, ]; },