更新版本至 0.0.42,修改依赖项版本并在 QueryResources 类中新增 onProcess 回调以支持上传进度通知

This commit is contained in:
2026-02-02 16:57:30 +08:00
parent 916bb083af
commit 171847a46c
3 changed files with 27 additions and 17 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/api",
"version": "0.0.41",
"version": "0.0.42",
"description": "",
"main": "mod.ts",
"scripts": {
@@ -25,10 +25,10 @@
"@kevisual/query": "^0.0.39",
"@kevisual/router": "^0.0.66",
"@kevisual/types": "^0.0.12",
"@kevisual/use-config": "^1.0.28",
"@kevisual/use-config": "^1.0.30",
"@types/bun": "^1.3.8",
"@types/crypto-js": "^4.2.2",
"@types/node": "^25.1.0",
"@types/node": "^25.2.0",
"crypto-js": "^4.2.0",
"dotenv": "^17.2.3",
"fast-glob": "^3.3.3"

22
pnpm-lock.yaml generated
View File

@@ -49,8 +49,8 @@ importers:
specifier: ^0.0.12
version: 0.0.12
'@kevisual/use-config':
specifier: ^1.0.28
version: 1.0.28(dotenv@17.2.3)
specifier: ^1.0.30
version: 1.0.30(dotenv@17.2.3)
'@types/bun':
specifier: ^1.3.8
version: 1.3.8
@@ -58,8 +58,8 @@ importers:
specifier: ^4.2.2
version: 4.2.2
'@types/node':
specifier: ^25.1.0
version: 25.1.0
specifier: ^25.2.0
version: 25.2.0
crypto-js:
specifier: ^4.2.0
version: 4.2.0
@@ -134,8 +134,8 @@ packages:
'@kevisual/types@0.0.12':
resolution: {integrity: sha512-zJXH2dosir3jVrQ6QG4i0+iLQeT9gJ3H+cKXs8ReWboxBSYzUZO78XssVeVrFPsJ33iaAqo4q3DWbSS1dWGn7Q==}
'@kevisual/use-config@1.0.28':
resolution: {integrity: sha512-ngF+LDbjxpXWrZNmnShIKF/jPpAa+ezV+DcgoZIIzHlRnIjE+rr9sLkN/B7WJbiH9C/j1tQXOILY8ujBqILrow==}
'@kevisual/use-config@1.0.30':
resolution: {integrity: sha512-kPdna0FW/X7D600aMdiZ5UTjbCo6d8d4jjauSc8RMmBwUU6WliFDSPUNKVpzm2BsDX5Nth1IXFPYMqH+wxqAmw==}
peerDependencies:
dotenv: ^17
@@ -350,8 +350,8 @@ packages:
'@types/node@22.15.27':
resolution: {integrity: sha512-5fF+eu5mwihV2BeVtX5vijhdaZOfkQTATrePEaXTcKqI16LhJ7gi2/Vhd9OZM0UojcdmiOCVg5rrax+i1MdoQQ==}
'@types/node@25.1.0':
resolution: {integrity: sha512-t7frlewr6+cbx+9Ohpl0NOTKXZNV9xHRmNOvql47BFJKcEG1CxtxlPEEe+gR9uhVWM4DwhnvTF110mIL4yP9RA==}
'@types/node@25.2.0':
resolution: {integrity: sha512-DZ8VwRFUNzuqJ5khrvwMXHmvPe+zGayJhr2CDNiKB1WBE1ST8Djl00D0IC4vvNmHMdj6DlbYRIaFE7WHjlDl5w==}
'@types/resolve@1.20.2':
resolution: {integrity: sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q==}
@@ -765,7 +765,7 @@ snapshots:
'@kevisual/types@0.0.12': {}
'@kevisual/use-config@1.0.28(dotenv@17.2.3)':
'@kevisual/use-config@1.0.30(dotenv@17.2.3)':
dependencies:
'@kevisual/load': 0.0.6
dotenv: 17.2.3
@@ -921,7 +921,7 @@ snapshots:
dependencies:
undici-types: 6.21.0
'@types/node@25.1.0':
'@types/node@25.2.0':
dependencies:
undici-types: 7.16.0
@@ -945,7 +945,7 @@ snapshots:
bun-types@1.3.8:
dependencies:
'@types/node': 25.1.0
'@types/node': 25.2.0
call-bind-apply-helpers@1.0.2:
dependencies:

View File

@@ -2,15 +2,18 @@ import { adapter, DataOpts, Result } from '@kevisual/query';
import path from 'path-browserify-esm';
import { hashContent } from './utils';
type Process = {}
type QueryResourcesOptions = {
prefix?: string;
storage?: Storage;
username?: string;
onProcess?: (opts?: Process) => void;
[key: string]: any;
};
export class QueryResources {
prefix: string; // root/resources
storage: Storage;
onProcess?: (opts?: Process) => void;
constructor(opts: QueryResourcesOptions) {
if (opts.username) {
this.prefix = `/${opts.username}/resources/`;
@@ -18,6 +21,7 @@ export class QueryResources {
this.prefix = opts.prefix || '';
}
this.storage = opts.storage || localStorage;
this.onProcess = opts.onProcess || (() => { });
}
setUsername(username: string) {
this.prefix = `/${username}/resources/`;
@@ -81,6 +85,7 @@ export class QueryResources {
// 使用分块上传
return this.uploadChunkedFile(filepath, content, hash, { chunkSize, ...restOpts });
}
this.onProcess?.({ type: 'uploadBegin', filename, size: fileSize, process: 0 });
const formData = new FormData();
if (isBlob) {
@@ -88,7 +93,7 @@ export class QueryResources {
} else {
formData.append('file', new Blob([content], { type }));
}
return adapter({
const res = await adapter({
url: url.toString(),
isPostFile: true,
method: 'POST',
@@ -101,6 +106,8 @@ export class QueryResources {
...restOpts?.params,
},
});
this.onProcess?.({ type: 'uploadFinish', filename, size: fileSize, process: 100 });
return res;
}
async uploadChunkedFile(filepath: string, file: Blob, hash: string, opts?: DataOpts & { chunkSize?: number }): Promise<Result<any>> {
const pathname = `${this.prefix}${filepath}`;
@@ -114,8 +121,10 @@ export class QueryResources {
const { chunkSize: _chunkSize, ...restOpts } = opts || {};
const chunkSize = _chunkSize ?? 5 * 1024 * 1024; // 5MB
const totalChunks = Math.ceil(file.size / chunkSize);
this.onProcess?.({ type: 'uploadBegin', filename, size: file.size, process: 0 });
for (let currentChunk = 0; currentChunk < totalChunks; currentChunk++) {
this.onProcess?.({ type: 'uploadChunkedFile', filename, size: file.size, process: 0, totalChunks, currentChunk: currentChunk + 1 });
const start = currentChunk * chunkSize;
const end = Math.min(start + chunkSize, file.size);
const chunkBlob = file.slice(start, end);
@@ -145,15 +154,16 @@ export class QueryResources {
},
});
if (res.code !== 200) {
throw new Error(`Chunk upload failed with code ${res!.code}, message: ${res!.message}`);
throw new Error(`Chunk 上传失败 code ${res!.code}, 错误信息是: ${res!.message}`);
}
console.log(`Chunk ${currentChunk + 1}/${totalChunks} uploaded`, res);
this.onProcess?.({ type: 'uploadChunkedFile', filename, size: file.size, process: Math.round(((currentChunk + 1) / totalChunks) * 100), totalChunks, currentChunk: currentChunk + 1 });
} catch (error) {
console.error(`Error uploading chunk ${currentChunk + 1}/${totalChunks}`, error);
return { code: 500, message: `分块上传失败: ${(error as Error).message}` };
}
}
this.onProcess?.({ type: 'uploadFinish', filename, size: file.size, process: 100 });
return { code: 200, message: '上传成功' };
}