polgo-upload-kit 1.0.0
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 +18 -0
- package/src/polgoUploadClient.js +53 -0
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "polgo-upload-kit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
},
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"description": "",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"axios": "^1.7.7",
|
|
14
|
+
"file-type": "^16.5.4",
|
|
15
|
+
"form-data": "^4.0.1",
|
|
16
|
+
"mime-types": "^2.1.35"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const axios = require("axios");
|
|
2
|
+
const fileType = require("file-type");
|
|
3
|
+
|
|
4
|
+
class PolgoUploadClient {
|
|
5
|
+
constructor(apiUrl) {
|
|
6
|
+
this.apiUrl =
|
|
7
|
+
apiUrl ||
|
|
8
|
+
"https://mkgplyz3tc.execute-api.us-east-1.amazonaws.com/lambdaUploadProducao/arquivo/upload";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async uploadFile(uint8Array, options = {}) {
|
|
12
|
+
let fileBlob;
|
|
13
|
+
|
|
14
|
+
if (uint8Array instanceof Uint8Array) {
|
|
15
|
+
const mimeType =
|
|
16
|
+
(await fileType.fromBuffer(uint8Array))?.mime ||
|
|
17
|
+
"application/octet-stream";
|
|
18
|
+
fileBlob = new Blob([uint8Array], { type: mimeType });
|
|
19
|
+
} else {
|
|
20
|
+
throw new TypeError("The provided file is not a valid Uint8Array.");
|
|
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);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const finalUrl = `${this.apiUrl}${
|
|
32
|
+
queryParams.toString() ? `?${queryParams.toString()}` : ""
|
|
33
|
+
}`;
|
|
34
|
+
|
|
35
|
+
const form = new FormData();
|
|
36
|
+
const fileName = options.nomeArquivo || "uploaded_file";
|
|
37
|
+
form.append("file", fileBlob, fileName);
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const response = await axios.post(finalUrl, form, {
|
|
41
|
+
headers: {
|
|
42
|
+
"Content-Type": "multipart/form-data",
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
return response.data;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.error("Error uploading file:", error.message);
|
|
48
|
+
throw new Error(`Failed to upload file: ${error.message}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
module.exports = { PolgoUploadClient };
|