triangle-utils 1.0.18 → 1.0.20
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/utils/Utils_Misc.js +2 -2
- package/utils/Utils_S3.js +39 -20
package/package.json
CHANGED
package/utils/Utils_Misc.js
CHANGED
|
@@ -25,7 +25,7 @@ export default class Utils_Misc {
|
|
|
25
25
|
return new Promise(resolve => setTimeout(resolve, duration))
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
get_current_time(
|
|
28
|
+
get_current_time(milliseconds = false) {
|
|
29
29
|
if (milliseconds) {
|
|
30
30
|
return (new Date()).getTime()
|
|
31
31
|
}
|
|
@@ -36,7 +36,7 @@ export default class Utils_Misc {
|
|
|
36
36
|
for (let i = 0; i < 3; i++) {
|
|
37
37
|
try {
|
|
38
38
|
await this.transporter.sendMail({
|
|
39
|
-
from:
|
|
39
|
+
from: this.config.alerts_email,
|
|
40
40
|
to: recipient,
|
|
41
41
|
subject: subject,
|
|
42
42
|
text: text
|
package/utils/Utils_S3.js
CHANGED
|
@@ -1,29 +1,44 @@
|
|
|
1
1
|
import { S3, NoSuchKey } from "@aws-sdk/client-s3"
|
|
2
2
|
|
|
3
|
+
function parse_s3_filename(s3_filename) {
|
|
4
|
+
if (s3_filename.substring(0, 5) !== "s3://") {
|
|
5
|
+
return undefined
|
|
6
|
+
}
|
|
7
|
+
const bucket_filename = s3_filename.substring(5)
|
|
8
|
+
const bucket = bucket_filename.split("/")[0]
|
|
9
|
+
const filename = bucket_filename.split("/").slice(1).join("/")
|
|
10
|
+
return { Bucket : bucket, Key : filename }
|
|
11
|
+
}
|
|
12
|
+
|
|
3
13
|
export default class Utils_S3 {
|
|
4
14
|
|
|
5
15
|
constructor(config) {
|
|
6
16
|
this.s3 = new S3({ region : config.region })
|
|
7
17
|
}
|
|
8
18
|
|
|
9
|
-
|
|
19
|
+
|
|
20
|
+
async file_exists(s3_filename) {
|
|
21
|
+
const s3_key = parse_s3_filename(s3_filename)
|
|
22
|
+
if (s3_key === undefined) {
|
|
23
|
+
return undefined
|
|
24
|
+
}
|
|
10
25
|
try {
|
|
11
|
-
const head = await s3.headObject(
|
|
12
|
-
Bucket : bucket,
|
|
13
|
-
Key : filename
|
|
14
|
-
})
|
|
26
|
+
const head = await s3.headObject(s3_key)
|
|
15
27
|
return true
|
|
16
28
|
} catch (error) {
|
|
17
29
|
return false
|
|
18
30
|
}
|
|
19
31
|
}
|
|
20
32
|
|
|
21
|
-
|
|
33
|
+
|
|
34
|
+
async get_file(s3_filename, json=false) {
|
|
35
|
+
const s3_key = parse_s3_filename(s3_filename)
|
|
36
|
+
if (s3_key === undefined) {
|
|
37
|
+
return undefined
|
|
38
|
+
}
|
|
22
39
|
try {
|
|
23
|
-
return await s3.getObject(
|
|
24
|
-
|
|
25
|
-
Key : filename
|
|
26
|
-
}).then(response => response.Body.transformToString("utf-8"))
|
|
40
|
+
return await s3.getObject(s3_key)
|
|
41
|
+
.then(response => response.Body.transformToString("utf-8"))
|
|
27
42
|
.then(text => json ? JSON.parse(text) : text)
|
|
28
43
|
} catch (error) {
|
|
29
44
|
if (error instanceof NoSuchKey) {
|
|
@@ -33,23 +48,27 @@ export default class Utils_S3 {
|
|
|
33
48
|
}
|
|
34
49
|
}
|
|
35
50
|
|
|
36
|
-
async create_file(
|
|
37
|
-
const exists = await file_exists(
|
|
51
|
+
async create_file(s3_filename, content) {
|
|
52
|
+
const exists = await file_exists(s3_filename)
|
|
38
53
|
if (exists) {
|
|
39
|
-
return
|
|
54
|
+
return undefined
|
|
55
|
+
}
|
|
56
|
+
const s3_key = parse_s3_filename(s3_filename)
|
|
57
|
+
if (s3_key === undefined) {
|
|
58
|
+
return undefined
|
|
40
59
|
}
|
|
41
60
|
return await s3.putObject({
|
|
42
|
-
|
|
43
|
-
Key : filename,
|
|
61
|
+
...s3_key,
|
|
44
62
|
Body : content
|
|
45
63
|
})
|
|
46
64
|
}
|
|
47
65
|
|
|
48
|
-
async delete_file(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
66
|
+
async delete_file(s3_filename) {
|
|
67
|
+
const s3_key = parse_s3_filename(s3_filename)
|
|
68
|
+
if (s3_key === undefined) {
|
|
69
|
+
return undefined
|
|
70
|
+
}
|
|
71
|
+
return await s3.deleteObject(s3_key)
|
|
53
72
|
}
|
|
54
73
|
|
|
55
74
|
}
|