partial-uploader 0.0.4 → 0.0.6
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.d.ts +6 -6
- package/dist/index.js +92 -165
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
declare
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}>;
|
|
6
|
-
export { uploadWithPartialFile };
|
|
1
|
+
declare const uploadWithPartialFile: (url: string, file: any, headers?: any, chunkSize?: number) => Promise<{
|
|
2
|
+
success: boolean;
|
|
3
|
+
id: string;
|
|
4
|
+
message: string;
|
|
5
|
+
}>;
|
|
6
|
+
export { uploadWithPartialFile };
|
package/dist/index.js
CHANGED
|
@@ -1,165 +1,92 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
exports.uploadWithPartialFile =
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return
|
|
92
|
-
|
|
93
|
-
formData.append('file', chunk, "".concat(filename, "_chunk_").concat(index));
|
|
94
|
-
formData.append('fileGuid', fileGuid);
|
|
95
|
-
isDone = chunksLength === index;
|
|
96
|
-
formData.append('isDone', isDone.toString());
|
|
97
|
-
formData.append('totalSize', fileSize.toString());
|
|
98
|
-
formData.append('totalChunks', chunksLength.toString());
|
|
99
|
-
formData.append('filename', filename);
|
|
100
|
-
return [2 /*return*/, getRes(url, formData, headers)];
|
|
101
|
-
});
|
|
102
|
-
}); };
|
|
103
|
-
var getRes = function (url, formData, headers) { return __awaiter(void 0, void 0, void 0, function () {
|
|
104
|
-
var res, e_1, res;
|
|
105
|
-
return __generator(this, function (_a) {
|
|
106
|
-
switch (_a.label) {
|
|
107
|
-
case 0:
|
|
108
|
-
_a.trys.push([0, 2, , 5]);
|
|
109
|
-
return [4 /*yield*/, uploadSubscribe(url, formData, headers)];
|
|
110
|
-
case 1:
|
|
111
|
-
res = _a.sent();
|
|
112
|
-
if (res.status === 406 || res.status === 401)
|
|
113
|
-
return [2 /*return*/, false];
|
|
114
|
-
return [3 /*break*/, 5];
|
|
115
|
-
case 2:
|
|
116
|
-
e_1 = _a.sent();
|
|
117
|
-
return [4 /*yield*/, delay(500)];
|
|
118
|
-
case 3:
|
|
119
|
-
_a.sent();
|
|
120
|
-
return [4 /*yield*/, uploadSubscribe(url, formData, headers)];
|
|
121
|
-
case 4:
|
|
122
|
-
res = _a.sent();
|
|
123
|
-
if (!res.ok)
|
|
124
|
-
return [2 /*return*/, false];
|
|
125
|
-
return [3 /*break*/, 5];
|
|
126
|
-
case 5:
|
|
127
|
-
;
|
|
128
|
-
return [2 /*return*/, true];
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
}); };
|
|
132
|
-
var delay = function (ms) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
133
|
-
switch (_a.label) {
|
|
134
|
-
case 0: return [4 /*yield*/, new Promise(function (f) { return setTimeout(f, ms); })];
|
|
135
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
136
|
-
}
|
|
137
|
-
}); }); };
|
|
138
|
-
var uploadSubscribe = function (url, formData, headers) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
139
|
-
switch (_a.label) {
|
|
140
|
-
case 0: return [4 /*yield*/, fetch(url, { method: 'POST', body: formData, headers: headers ? headers : {} })];
|
|
141
|
-
case 1: return [2 /*return*/, (_a.sent())];
|
|
142
|
-
}
|
|
143
|
-
}); }); };
|
|
144
|
-
var splitFileIntoChunks = function (file, chunkSize) {
|
|
145
|
-
var chunks = [];
|
|
146
|
-
var start = 0;
|
|
147
|
-
while (start < file.size) {
|
|
148
|
-
var end = Math.min(start + chunkSize, file.size);
|
|
149
|
-
var chunk = file.slice(start, end);
|
|
150
|
-
chunks.push(chunk);
|
|
151
|
-
start = end;
|
|
152
|
-
}
|
|
153
|
-
;
|
|
154
|
-
return chunks;
|
|
155
|
-
};
|
|
156
|
-
var generateGuid = function () {
|
|
157
|
-
var chars = '0123456789abcdef';
|
|
158
|
-
var guid = '';
|
|
159
|
-
for (var i = 0; i < 40; i++) {
|
|
160
|
-
var randomIndex = Math.floor(Math.random() * chars.length);
|
|
161
|
-
guid += chars.charAt(randomIndex);
|
|
162
|
-
}
|
|
163
|
-
;
|
|
164
|
-
return guid;
|
|
165
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.uploadWithPartialFile = void 0;
|
|
13
|
+
const uploadWithPartialFile = (url, file, headers, chunkSize = 26214400) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
yield delay(50);
|
|
15
|
+
const id = generateGuid();
|
|
16
|
+
if (file.size <= chunkSize) {
|
|
17
|
+
// small file
|
|
18
|
+
let res = yield upload(url, file, 1, file.size, file.name, id, 0, headers);
|
|
19
|
+
if (!res)
|
|
20
|
+
return { success: false, id, message: "file could not be loaded" };
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
// big file
|
|
24
|
+
const chunks = splitFileIntoChunks(file, chunkSize);
|
|
25
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
26
|
+
let chunk = chunks[i];
|
|
27
|
+
if (!chunk)
|
|
28
|
+
return { success: false, id, message: "chunk is undefined" };
|
|
29
|
+
let res = yield upload(url, chunk, chunks.length, file.size, file.name, id, i, headers);
|
|
30
|
+
if (!res)
|
|
31
|
+
return { success: false, id, message: "file could not be loaded" };
|
|
32
|
+
yield delay(550);
|
|
33
|
+
}
|
|
34
|
+
;
|
|
35
|
+
}
|
|
36
|
+
;
|
|
37
|
+
return { success: true, id, message: "file uploaded successfully" };
|
|
38
|
+
});
|
|
39
|
+
exports.uploadWithPartialFile = uploadWithPartialFile;
|
|
40
|
+
const upload = (url, chunk, chunksLength, fileSize, filename, fileGuid, index, headers) => __awaiter(void 0, void 0, void 0, function* () {
|
|
41
|
+
let formData = new FormData();
|
|
42
|
+
formData.append('file', chunk, `${filename}_chunk_${index}`);
|
|
43
|
+
formData.append('fileGuid', fileGuid);
|
|
44
|
+
let isDone = chunksLength === index + 1;
|
|
45
|
+
formData.append('isDone', isDone.toString());
|
|
46
|
+
formData.append('totalSize', fileSize.toString());
|
|
47
|
+
formData.append('totalChunks', chunksLength.toString());
|
|
48
|
+
formData.append('filename', filename);
|
|
49
|
+
formData.append('index', index.toString());
|
|
50
|
+
return getRes(url, formData, headers);
|
|
51
|
+
});
|
|
52
|
+
const getRes = (url, formData, headers) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
try {
|
|
54
|
+
let res = yield uploadSubscribe(url, formData, headers);
|
|
55
|
+
if (res.status === 406 || res.status === 401)
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
yield delay(500);
|
|
60
|
+
let res = yield uploadSubscribe(url, formData, headers);
|
|
61
|
+
if (!res.ok)
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
;
|
|
65
|
+
return true;
|
|
66
|
+
});
|
|
67
|
+
const delay = (ms) => __awaiter(void 0, void 0, void 0, function* () {
|
|
68
|
+
yield new Promise(f => setTimeout(f, ms));
|
|
69
|
+
});
|
|
70
|
+
const uploadSubscribe = (url, formData, headers) => __awaiter(void 0, void 0, void 0, function* () { return (yield fetch(url, { method: 'POST', body: formData, headers: headers ? headers : {} })); });
|
|
71
|
+
const splitFileIntoChunks = (file, chunkSize) => {
|
|
72
|
+
const chunks = [];
|
|
73
|
+
let start = 0;
|
|
74
|
+
while (start < file.size) {
|
|
75
|
+
const end = Math.min(start + chunkSize, file.size);
|
|
76
|
+
const chunk = file.slice(start, end);
|
|
77
|
+
chunks.push(chunk);
|
|
78
|
+
start = end;
|
|
79
|
+
}
|
|
80
|
+
;
|
|
81
|
+
return chunks;
|
|
82
|
+
};
|
|
83
|
+
const generateGuid = () => {
|
|
84
|
+
var chars = '0123456789abcdef';
|
|
85
|
+
var guid = '';
|
|
86
|
+
for (var i = 0; i < 40; i++) {
|
|
87
|
+
var randomIndex = Math.floor(Math.random() * chars.length);
|
|
88
|
+
guid += chars.charAt(randomIndex);
|
|
89
|
+
}
|
|
90
|
+
;
|
|
91
|
+
return guid;
|
|
92
|
+
};
|