-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (64 loc) · 1.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { visit } from 'unist-util-visit'
import { GetObjectCommand } from '@aws-sdk/client-s3'
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
function getSignedUrls (options) {
const s3Client = options.s3
const bucket = options.bucket
const expiration = options.expiration
let cacheAdd = function (item) {
return item
}
if (options.cacheAdd) {
cacheAdd = options.cacheAdd
}
const cacheRead = options.cacheRead
return async (tree, file) => {
const promises = []
visit(tree, 'image', visitor)
await Promise.all(promises)
return tree;
function visitor (node, index, parent) {
// check the address format
if (isValidHttpUrl(node.url)) {
return
}
const p = getUrl(node.url).then(res => node.url = res)
promises.push(p)
return true
function getUrl (key) {
if (cacheRead) {
return cacheRead(key).then(res => {
if (res === null) {
const params = { Bucket: bucket, Key: key }
const command = new GetObjectCommand(params)
// make requests
// edit link text
const p = getSignedUrl(s3Client, command, { expiresIn: expiration })
return p
} else {
return Promise.resolve(res)
}
})
} else {
// get aws
var params = { Bucket: bucket, Key: key }
var command = new GetObjectCommand(params)
// make requests
// edit link text
const p = getSignedUrl(s3Client, command, { expiresIn: expiration })
return p
}
}
}
}
function isValidHttpUrl (string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
}
export { getSignedUrls }