From 12f6075bc97c2a531cd6c4effec6f9eda2c0d0ad Mon Sep 17 00:00:00 2001 From: Yufan Sheng Date: Mon, 8 Jul 2024 22:29:15 +0800 Subject: [PATCH] fix: improve the uploading speed by reducing the API call. --- README.md | 13 +++++++++++++ src/index.ts | 18 ++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8072d3f..df061b3 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,19 @@ export default defineConfig({ }) ``` +### Vite Dependency Optimization + +If you have issues like '' in using this tool. Remember to change your Astro configuration for add the code shown below. + +```ts +export default defineConfig({ + vite: { + // Add this for avoiding the needless import optimize in Vite. + optimizeDeps: { exclude: ['opendal'] }, + }, +}); +``` + ## Options ```ts diff --git a/src/index.ts b/src/index.ts index 3273397..b3071a9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -96,17 +96,23 @@ class Uploader { } async isExist(key: string, size: number): Promise { - const exist = await this.operator.isExist(key); - if (exist) { - const { contentLength } = await this.operator.stat(key); + try { + const { contentLength, isFile } = await this.operator.stat(key); if ((contentLength !== null && contentLength !== BigInt(size)) || this.override) { await this.operator.delete(key); return false; } - return true; + return isFile(); + } catch (err) { + // Just ignore the error for now. If we find better solution for how to handle the opendal error. + if (err instanceof Error) { + const msg = err.toString(); + if (msg.includes('Error: NotFound')) { + return false; + } + } + throw err; } - - return false; } async write(key: string, body: Buffer) {