@hackthedev/file-manager 1.0.0 → 1.0.2
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/FileManager.js +16 -15
- package/README.md +0 -0
- package/package.json +4 -2
package/FileManager.js
CHANGED
|
@@ -98,13 +98,14 @@ class FileManager {
|
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
static async uploadFile(files, {
|
|
101
|
-
|
|
102
|
-
accountToken = null,
|
|
101
|
+
authObj = {},
|
|
103
102
|
onProgress = null,
|
|
104
|
-
params = {}
|
|
103
|
+
params = {},
|
|
104
|
+
type = "upload",
|
|
105
|
+
host = null,
|
|
105
106
|
} = {}) {
|
|
106
|
-
const file = files[0]
|
|
107
|
-
const chunkSize = 1024 * 256;
|
|
107
|
+
const file = files[0] ?? files;
|
|
108
|
+
const chunkSize = 1024 * 256;
|
|
108
109
|
const totalChunks = Math.ceil(file.size / chunkSize);
|
|
109
110
|
const fileId = crypto.randomUUID();
|
|
110
111
|
|
|
@@ -120,23 +121,22 @@ class FileManager {
|
|
|
120
121
|
const arrayBuf = await chunk.arrayBuffer();
|
|
121
122
|
|
|
122
123
|
const search = new URLSearchParams({
|
|
123
|
-
filename,
|
|
124
|
-
chunkIndex: i,
|
|
125
|
-
totalChunks,
|
|
126
|
-
fileId,
|
|
127
124
|
...params
|
|
128
125
|
});
|
|
129
126
|
|
|
130
|
-
const url =
|
|
131
|
-
|
|
127
|
+
const url = `${host ?? ""}/upload${search.toString() ? `?${search.toString()}` : ""}`;
|
|
132
128
|
|
|
133
129
|
const res = await fetch(url, {
|
|
134
130
|
method: "POST",
|
|
135
|
-
body: arrayBuf,
|
|
136
131
|
headers: {
|
|
137
|
-
|
|
138
|
-
"x-
|
|
132
|
+
...authObj,
|
|
133
|
+
"x-upload-type": type,
|
|
134
|
+
"x-file-name": encodeURIComponent(filename),
|
|
135
|
+
"x-chunk-index": String(i),
|
|
136
|
+
"x-total-chunks": String(totalChunks),
|
|
137
|
+
"x-file-id": fileId
|
|
139
138
|
},
|
|
139
|
+
body: arrayBuf
|
|
140
140
|
});
|
|
141
141
|
|
|
142
142
|
const json = await res.json();
|
|
@@ -145,10 +145,11 @@ class FileManager {
|
|
|
145
145
|
return json;
|
|
146
146
|
|
|
147
147
|
const percent = Math.round(((i + 1) / totalChunks) * 100);
|
|
148
|
+
|
|
148
149
|
if (percent !== lastPercent) {
|
|
149
150
|
lastPercent = percent;
|
|
150
151
|
|
|
151
|
-
if(onProgress && typeof onProgress === "function"){
|
|
152
|
+
if (onProgress && typeof onProgress === "function") {
|
|
152
153
|
await onProgress(percent);
|
|
153
154
|
}
|
|
154
155
|
}
|
package/README.md
ADDED
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackthedev/file-manager",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Frontend library with a few helper functions and dSyncFiles integration",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "",
|
|
7
|
-
"keywords": [
|
|
7
|
+
"keywords": [
|
|
8
|
+
"frontend"
|
|
9
|
+
],
|
|
8
10
|
"main": "FileManager.js",
|
|
9
11
|
"scripts": {
|
|
10
12
|
"test": "echo \"Error: no test specified\" && exit 1"
|