Skip to content

Commit

Permalink
v1.1.1
Browse files Browse the repository at this point in the history
If the key does not exist, the page will return 404 and throw an exception, marking the key -2 after anger, no more access
  • Loading branch information
supine0703 committed Aug 2, 2024
1 parent d92e58a commit 08beca4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
23 changes: 8 additions & 15 deletions src/compress/tinypng/tinypng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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;
});
});
Expand All @@ -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);
}

Expand All @@ -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),
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
// 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',
Expand All @@ -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;
},
},
];
},
Expand Down

0 comments on commit 08beca4

Please sign in to comment.