-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
57 lines (50 loc) · 1.98 KB
/
main.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
const picContainerDiv = document.getElementById('pics');
const s3FolderLocation = 'https://app-that-makes-ya-go-aw.s3.eu-west-2.amazonaws.com';
const picUrls = [
'./assets/aw.jpg', // local file
// 'https://app-that-makes-ya-go-aw.s3.eu-west-2.amazonaws.com/folder/1583086050747_aw.jpg',
];
// this function takes an array of filenames and adds them as images into the document.
function addPics (picUrls) {
picUrls.forEach (url => {
const newImg = document.createElement('img');
newImg.src = url;
newImg.style="max-height:500px; max-width:500px;";
picContainerDiv.appendChild(newImg);
});
}
// Always add the pics from the URLs in the array above
addPics (picUrls);
// we'll only run this if there are no picture URLs in that array.
if (picUrls.length === 0)
listBucketContents( s3FolderLocation )
.then(newUrls=> addPics(newUrls) );
// _______________________________________________________________________________
// Function definitions
function listBucketContents (location) {
// will return a Promise that resolves to an array of URLs.
return (
// Perform an HTTP request to s3FolderLocation
fetch (location)
// Take the contents of the response
.then(response=> response.text())
// Turn the response contents from XML into a tree of Nodes
.then(text=> (new window.DOMParser()).parseFromString(text, "text/xml"))
// Pick the leaf Nodes we want from the tree
.then(xml=> filterNodes(xml,'ListBucketResult'))
.then(xml=> filterNodes(xml[0],'Contents'))
.then(arr=> arr.map(xml=> filterNodes(xml,'Key')))
// Retrieve the text content from the Nodes
.then(arr=> arr.map(xml=> xml[0].innerHTML))
// Create an absolute URL from a relative URL
.then (filenames=> filenames.map(file=> `${location}/${file}`))
)
}
function filterNodes(nodeList, name) {
const returnList = [];
nodeList.childNodes.forEach( node=> {
if (node.nodeName === name)
returnList.push(node)
})
return returnList
}