-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
167 lines (151 loc) · 5.35 KB
/
index.html
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html>
<head>
<title>S3 Bucket Listing</title>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
</head>
<body>
<div id="jstree_div"></div>
<script>
const regexMatchLastSegement = /(?!\/)[^\/]*(?=\/?$)/;
const thisFileName = window.location.pathname.match(regexMatchLastSegement);
function getPrettySize(size) {
let magnitude = Math.floor(Math.log(size) / Math.log(1024));
var unit = "Byte";
switch (magnitude) {
case 1: unit = "KB"; break;
case 2: unit = "MB"; break;
case 3: unit = "GB"; break;
}
let resized = Math.round(size / Math.pow(1024, magnitude));
let result = resized + unit;
return result;
}
function createFileNode(completeURL, size, lastmodified) {
let name = completeURL.match(regexMatchLastSegement);
let tooltip = getPrettySize(size) + ' ' + lastmodified;
let icon = getNodeIcon(completeURL);
let node = {
"id": completeURL,
"text": name,
"icon": icon,
"state": {
"opened": false,
"disabled": false,
"loaded": true
},
"a_attr": {
"href": completeURL,
"title": tooltip
}
};
return node;
};
function createDirectoryNode(completeURL) {
let text = completeURL.match(regexMatchLastSegement);
let node = {
"id": completeURL,
"text": text,
"icon": "fa fa-folder-open-o",
"state": {
"opened": false,
"disabled": false,
"loaded": false
},
"a_attr": null,
"children": true
};
return node;
};
function getNodeIcon(completeURL) {
let extensionMatches = completeURL.match(/\.[^\.\/]*$/);
let extension = (extensionMatches && extensionMatches[0])? extensionMatches[0] : "";
switch (extension) {
case ".zip":
return "fa fa-file-archive-o";
case ".pdf":
return "fa fa-file-pdf-o";
}
return "fa fa-file-o";
}
let jstree_div = $('#jstree_div');
jstree_div.jstree({
'core': {
'data': (obj, cb) => {
loadTreeNodes(obj, cb)
}
},
"search": {
"case_insensitive": true,
"show_only_matches" : true
},
"plugins": ["search"]
})
.bind("select_node.jstree", function (e, data) {
var href = data.node.a_attr.href;
if(href == '#') {
return '';
}
window.open(href);
});
async function loadTreeNodes(obj, cb) {
let locationToLoad = obj.id == "#" ? window.location.href : obj.id;
let nodes = await loadChildren(locationToLoad);
cb.call(this, nodes);
};
async function loadChildren(parentURL) {
let parser = document.createElement('a');
parser.href = parentURL;
let indexHtml = "/index.html";
let host = parser.hostname;
let bucket = host.split('.')[0];
let pathname = parser.pathname.replace(/index\.html/g, '').replace(/^\//, '');
let urlSuffix = '.s3.amazonaws.com';
let urlNoSlash = 'https://' + bucket + urlSuffix;
let url = urlNoSlash + '/';
let delimiter = 'delimiter=/';
let prefixValue = pathname;
let prefix = 'prefix=' + prefixValue;
let query = urlNoSlash + '?' + delimiter + '&' + prefix;
let allChildren = new Array();
let fetchResult = await fetch(query);
let text = await fetchResult.text();
let oParser = new DOMParser();
let oDOM = oParser.parseFromString(text, "text/xml");
var containsDirectoryLATEST = false;
oDOM.childNodes.forEach((value, index, listObj) => {
value.childNodes.forEach((value1, index1, listObj1) => {
value1.childNodes.forEach((value2, index2, listObj2) => {
let completeFileName = value2.textContent;
let completeURL = url + completeFileName;
if (value2.localName === 'Prefix') {
allChildren.push(createDirectoryNode(completeURL));
if (completeFileName.endsWith('/LATEST/')) {
containsDirectoryLATEST = true;
}
}
if (value2.localName === 'Key' && completeFileName != prefixValue) {
if (!completeFileName.endsWith(thisFileName)) {
let siblings = [...value2.parentNode.children];
let sizeNode = siblings.find((node) => { return node.localName == "Size"; });
let lastmodifiedNode = siblings.find((node) => { return node.localName == "LastModified"; });
let size = sizeNode && sizeNode.textContent;
let lastmodified = lastmodifiedNode && lastmodifiedNode.textContent;
allChildren.push(createFileNode(completeURL, size, lastmodified));
}
}
})
})
});
allChildren.sort();
if (containsDirectoryLATEST) {
allChildren.reverse();
}
return allChildren;
};
</script>
</body>
</html>