@aled1023/udl-sdk 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +15 -7
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.UdlStorage = void 0;
|
|
13
|
-
// D:\UDL-Governance\sdk-project\src\index.ts
|
|
14
13
|
const configuration_1 = require("./api/configuration");
|
|
15
14
|
const api_1 = require("./api/api");
|
|
16
15
|
class UdlStorage {
|
|
@@ -24,25 +23,34 @@ class UdlStorage {
|
|
|
24
23
|
}
|
|
25
24
|
upload(file, onProgress) {
|
|
26
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
// 1. 初始化
|
|
27
27
|
const initRes = yield this.api.initUpload({
|
|
28
28
|
fileName: file.name,
|
|
29
29
|
fileSize: file.size
|
|
30
30
|
});
|
|
31
|
-
//
|
|
31
|
+
// --- 核心修复:防御性校验,消除 undefined 隐患 ---
|
|
32
32
|
const data = initRes.data.data;
|
|
33
33
|
if (!data || !data.uploadId || data.totalChunks === undefined) {
|
|
34
|
-
throw new Error("SDK Error:
|
|
34
|
+
throw new Error("SDK Error: 后端返回的初始化数据不完整 (uploadId 或 totalChunks 缺失)");
|
|
35
35
|
}
|
|
36
36
|
const { uploadId, totalChunks } = data;
|
|
37
|
+
// 2. 循环分片上传
|
|
37
38
|
for (let i = 0; i < totalChunks; i++) {
|
|
38
39
|
const start = i * this.CHUNK_SIZE;
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
const end = Math.min(file.size, start + this.CHUNK_SIZE);
|
|
41
|
+
const chunk = file.slice(start, end);
|
|
42
|
+
// 构造 FormData 以匹配后端的 Multipart 接收
|
|
43
|
+
const formData = new FormData();
|
|
44
|
+
formData.append('file', chunk, file.name);
|
|
45
|
+
// 此时 uploadId 确定是 string, totalChunks 确定是 number
|
|
46
|
+
yield this.api.uploadChunk(uploadId, i, formData);
|
|
42
47
|
if (onProgress) {
|
|
43
|
-
|
|
48
|
+
// 防止 totalChunks 为 0 导致除以零错误
|
|
49
|
+
const percent = totalChunks > 0 ? Math.round(((i + 1) / totalChunks) * 100) : 100;
|
|
50
|
+
onProgress(percent);
|
|
44
51
|
}
|
|
45
52
|
}
|
|
53
|
+
// 3. 完成合并
|
|
46
54
|
const completeRes = yield this.api.completeUpload({ uploadId });
|
|
47
55
|
return completeRes.data;
|
|
48
56
|
});
|