polgo-upload-kit 1.0.0 → 1.0.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/package.json +1 -1
- package/src/polgoUploadClient.js +38 -34
package/package.json
CHANGED
package/src/polgoUploadClient.js
CHANGED
|
@@ -8,45 +8,49 @@ class PolgoUploadClient {
|
|
|
8
8
|
"https://mkgplyz3tc.execute-api.us-east-1.amazonaws.com/lambdaUploadProducao/arquivo/upload";
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
let
|
|
11
|
+
uploadFile(uint8Array, options = {}) {
|
|
12
|
+
let fileBlobPromise;
|
|
13
13
|
|
|
14
14
|
if (uint8Array instanceof Uint8Array) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
fileBlobPromise = fileType.fromBuffer(uint8Array).then((result) => {
|
|
16
|
+
const mimeType = result?.mime || "application/octet-stream";
|
|
17
|
+
return new Blob([uint8Array], { type: mimeType });
|
|
18
|
+
});
|
|
19
19
|
} else {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const queryParams = new URLSearchParams();
|
|
24
|
-
if (options.diretorio) {
|
|
25
|
-
queryParams.append("diretorio", options.diretorio);
|
|
26
|
-
}
|
|
27
|
-
if (options.nomeArquivo) {
|
|
28
|
-
queryParams.append("nomeArquivo", options.nomeArquivo);
|
|
20
|
+
return Promise.reject(
|
|
21
|
+
new TypeError("The provided file is not a valid Uint8Array.")
|
|
22
|
+
);
|
|
29
23
|
}
|
|
30
24
|
|
|
31
|
-
|
|
32
|
-
queryParams
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
25
|
+
return fileBlobPromise.then((fileBlob) => {
|
|
26
|
+
const queryParams = new URLSearchParams();
|
|
27
|
+
if (options.diretorio) {
|
|
28
|
+
queryParams.append("diretorio", options.diretorio);
|
|
29
|
+
}
|
|
30
|
+
if (options.nomeArquivo) {
|
|
31
|
+
queryParams.append("nomeArquivo", options.nomeArquivo);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const finalUrl = `${this.apiUrl}${
|
|
35
|
+
queryParams.toString() ? `?${queryParams.toString()}` : ""
|
|
36
|
+
}`;
|
|
37
|
+
|
|
38
|
+
const form = new FormData();
|
|
39
|
+
const fileName = options.nomeArquivo || "uploaded_file";
|
|
40
|
+
form.append("file", fileBlob, fileName);
|
|
41
|
+
|
|
42
|
+
return axios
|
|
43
|
+
.post(finalUrl, form, {
|
|
44
|
+
headers: {
|
|
45
|
+
"Content-Type": "multipart/form-data",
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
.then((response) => response.data)
|
|
49
|
+
.catch((error) => {
|
|
50
|
+
console.error("Error uploading file:", error.message);
|
|
51
|
+
throw new Error(`Failed to upload file: ${error.message}`);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
50
54
|
}
|
|
51
55
|
}
|
|
52
56
|
|