polgo-upload-kit 1.0.2 → 1.0.4
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/polgoUploadClient.js +32 -28
- package/package.json +2 -2
- package/.babelrc +0 -12
- package/src/polgoUploadClient.js +0 -57
|
@@ -3,42 +3,46 @@
|
|
|
3
3
|
const axios = require("axios");
|
|
4
4
|
const fileType = require("file-type");
|
|
5
5
|
class PolgoUploadClient {
|
|
6
|
-
constructor(
|
|
7
|
-
this.
|
|
6
|
+
constructor(isProd, stack) {
|
|
7
|
+
isProd ? this.ambiente = "producao" : this.ambiente = "dev";
|
|
8
|
+
this.url = "https://mkgplyz3tc.execute-api.us-east-1.amazonaws.com/lambdaUploadProducao/arquivo/upload";
|
|
9
|
+
this.stack = stack;
|
|
8
10
|
}
|
|
9
|
-
uploadFile(uint8Array, options = {}) {
|
|
10
|
-
let
|
|
11
|
+
async uploadFile(uint8Array, options = {}) {
|
|
12
|
+
let fileBlob;
|
|
11
13
|
if (uint8Array instanceof Uint8Array) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
type: mimeType
|
|
16
|
-
});
|
|
14
|
+
const mimeType = (await fileType.fromBuffer(uint8Array))?.mime || "application/octet-stream";
|
|
15
|
+
fileBlob = new Blob([uint8Array], {
|
|
16
|
+
type: mimeType
|
|
17
17
|
});
|
|
18
18
|
} else {
|
|
19
|
-
|
|
19
|
+
throw new TypeError("O arquivo não é um Uint8Array válido.");
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
21
|
+
const queryParams = new URLSearchParams();
|
|
22
|
+
queryParams.append("ambiente", this.ambiente);
|
|
23
|
+
queryParams.append("stack", this.stack);
|
|
24
|
+
if (options.diretorio) {
|
|
25
|
+
queryParams.append("diretorio", options.diretorio);
|
|
26
|
+
}
|
|
27
|
+
if (options.nomeArquivo) {
|
|
28
|
+
queryParams.append("nomeArquivo", options.nomeArquivo);
|
|
29
|
+
}
|
|
30
|
+
const finalUrl = `${this.url}${queryParams.toString() ? `?${queryParams.toString()}` : ""}`;
|
|
31
|
+
const form = new FormData();
|
|
32
|
+
const fileName = options.nomeArquivo || "uploaded_file";
|
|
33
|
+
form.append("file", fileBlob, fileName);
|
|
34
|
+
try {
|
|
35
|
+
const response = await axios.post(finalUrl, form, {
|
|
34
36
|
headers: {
|
|
35
|
-
"Content-Type": "multipart/form-data"
|
|
37
|
+
"Content-Type": "multipart/form-data",
|
|
38
|
+
Authorization: `Bearer 025154864ea17d1f294b69eb1defe112b5fa9a3c2bd88a40e70ebd29991da8aa`
|
|
36
39
|
}
|
|
37
|
-
}).then(response => response.data).catch(error => {
|
|
38
|
-
console.error("Error uploading file:", error.message);
|
|
39
|
-
throw new Error(`Failed to upload file: ${error.message}`);
|
|
40
40
|
});
|
|
41
|
-
|
|
41
|
+
return response.data;
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error("Erro ao fazer upload do arquivo:", error.message);
|
|
44
|
+
throw new Error(`Falha ao realizar o upload do arquivo: ${error.message}`);
|
|
45
|
+
}
|
|
42
46
|
}
|
|
43
47
|
}
|
|
44
48
|
module.exports = {
|
package/package.json
CHANGED
package/.babelrc
DELETED
package/src/polgoUploadClient.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
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
|
-
uploadFile(uint8Array, options = {}) {
|
|
12
|
-
let fileBlobPromise;
|
|
13
|
-
|
|
14
|
-
if (uint8Array instanceof Uint8Array) {
|
|
15
|
-
fileBlobPromise = fileType.fromBuffer(uint8Array).then((result) => {
|
|
16
|
-
const mimeType = result?.mime || "application/octet-stream";
|
|
17
|
-
return new Blob([uint8Array], { type: mimeType });
|
|
18
|
-
});
|
|
19
|
-
} else {
|
|
20
|
-
return Promise.reject(
|
|
21
|
-
new TypeError("The provided file is not a valid Uint8Array.")
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
|
|
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
|
-
});
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
module.exports = { PolgoUploadClient };
|