forked from Avaiga/taipy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileupload.worker.ts
118 lines (107 loc) · 3.88 KB
/
fileupload.worker.ts
1
2
3
4
5
6
7
8
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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2021-2024 Avaiga Private Limited
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
import { FileUploadData, FileUploadReturn } from "./fileupload.utils";
const uploadFile = (
blobOrFile: Blob,
uploadUrl: string,
varName: string,
context: string | undefined,
onAction: string | undefined,
uploadData: string | undefined,
part: number,
total: number,
fileName: string,
filePath: string,
multiple: boolean,
id: string,
progressCb: (uploaded: number) => void
) => {
const xhr = new XMLHttpRequest();
xhr.open("POST", `${uploadUrl}?client_id=${id}`, false);
xhr.onerror = (e) => self.postMessage({ message: "Error: " + e, error: true });
xhr.onload = (e) => progressCb(e.lengthComputable ? e.loaded : 0);
const fdata = new FormData();
fdata.append("blob", blobOrFile, fileName);
fdata.append("part", part.toString());
fdata.append("total", total.toString());
fdata.append("var_name", varName);
context && fdata.append("context", context);
onAction && fdata.append("on_action", onAction);
uploadData && fdata.append("upload_data", uploadData);
fdata.append("multiple", multiple ? "True" : "False");
fdata.append("path", filePath)
xhr.send(fdata);
};
// 1MB chunk sizes.
const BYTES_PER_CHUNK = 1024 * 1024;
const getProgressCallback = (globalSize: number, offset: number) => (uploaded: number) =>
self.postMessage({
progress: ((offset + uploaded) * 100) / globalSize,
done: false,
} as FileUploadReturn);
const process = (
files: FileList,
uploadUrl: string,
varName: string,
context: string | undefined,
onAction: string | undefined,
uploadData: string | undefined,
id: string
) => {
if (files) {
let globalSize = 0;
for (let i = 0; i < files.length; i++) {
globalSize += files[i].size;
}
const uploadedFiles = [];
for (let i = 0; i < files.length; i++) {
const blob = files[i];
const size = blob.size;
let start = 0;
let end = BYTES_PER_CHUNK;
const tot = Math.ceil(size / BYTES_PER_CHUNK);
while (start < size) {
const chunk = blob.slice(start, end);
const progressCallback = getProgressCallback(globalSize, start);
progressCallback(0);
uploadFile(
chunk,
uploadUrl,
varName,
context,
onAction,
uploadData,
Math.floor(start / BYTES_PER_CHUNK),
tot,
blob.name,
blob.webkitRelativePath,
i == 0 ? false : files.length > 0,
id,
progressCallback
);
progressCallback(chunk.size);
start = end;
end = start + BYTES_PER_CHUNK;
}
uploadedFiles.push(blob.name);
}
self.postMessage({
progress: 100,
message: uploadedFiles.join(", ") + " Uploaded Successfully",
done: true,
} as FileUploadReturn);
}
};
self.onmessage = (e: MessageEvent<FileUploadData>) => {
process(e.data.files, e.data.uploadUrl, e.data.varName, e.data.context, e.data.onAction, e.data.uploadData, e.data.id);
};