-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhushfile-common.js
189 lines (169 loc) · 6.41 KB
/
hushfile-common.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// main function to handle requests
function hfhandlerequest() {
if(window.location.pathname == "/") {
// show upload page
hfShowPage('upload.html','upload');
} else {
// this is not a request for a known url, get fileid and password
var fileid = window.location.pathname.substr(1);
// check if fileid exists
$.ajax({
url: '/api/exists?fileid='+fileid,
type: 'GET',
dataType: 'json',
success: function(responseobject) {
if (responseobject.exists) {
// fileid exists
if(window.location.hash.substr(1)=="") {
content = '<div class="alert alert-info">Enter password:</div>\n';
content += '<input type="text" id="password">\n';
content += '<button type="button" class="btn btn-large btn-success" onclick="hfPwRedirect(\'' + fileid + '\');">Go</button>\n';
hfSetContent(content,'download');
} else {
// show download page
hfShowPage('download.html','download', function(key) {
$('#downloadbtn').click(function(){
hfDownload(fileid, responseobject.chunks, responseobject.totalsize);
});
});
}
} else {
// fileid does not exist
hfSetContent('<div class="alert alert-error">Invalid fileid. Expired ?</div>\n','download');
return;
}
}
});
};
};
// function to set page content
function hfSetContent(content,menuitem) {
// set main page content
document.getElementById('content').innerHTML=content
// get all menuitems
var menuitems = document.getElementsByClassName('menuitem');
// loop through menuitems
for(var i=0; i<menuitems.length; i++) {
// this is the active menuitem
if(menuitems[i].id == menuitem) {
menuitems[i].className="menuitem active";
} else {
menuitems[i].className="menuitem";
};
};
};
// return a random password of the given length
function hfRandomPassword(length){
var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-";
var pass="";
var randomBuf = new Uint8Array(length);
window.crypto.getRandomValues(randomBuf);
for(var i=0;i<length;i++)
pass += chars.charAt(Math.floor(randomBuf[i]/4));
return pass;
};
// function to show pages from custom menu items
function hfShowPage(url, key, success, erroŕ) {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/'+url, true);
xhr.onload = function(e) {
if (this.status == 200) {
// display page content
hfSetContent(xhr.responseText,key);
// some logic is put here to make sure the page is done
// loading before the functions are called
if(url == 'upload.html') {
// create random password
document.getElementById('password').value=hfRandomPassword(40);
//wait for a file to be selected
document.getElementById('files').addEventListener('change', hfHandleFileSelect, false);
}
if(key == 'download') {
// get metadata
var fileid = window.location.pathname.substr(1);
hfGetMetadata(fileid);
}
if(success) success(key)
} else {
alert("Unable to get content, check client config!");
};
};
xhr.send();
};
// load and apply client config
function hfGetConfigs() {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/hushfile-webclient-config.json', true);
xhr.onload = function(e) {
if (this.status == 200) {
config = JSON.parse(xhr.responseText);
// handle footer config
if(!(config.footer.showfooter)) {
document.getElementById('navbarbottom').style.display = 'none';
} else {
if(config.footer.footer == "default") {
// show the default footer, set email
document.getElementById('operatoremail').href = 'mailto:'+config.footer.operatoremail;
} else {
// get and show custom footer
footerxhr = new XMLHttpRequest();
footerxhr.open('GET', '/'+config.footer.footer, true);
footerxhr.onload = function(e) {
if (this.status == 200) {
// footer fetched OK, replace default footer
document.getElementById('navbarbottominner').innerHTML=footerxhr.responseText;
} else {
document.getElementById('navbarbottominner').innerHTML='<div class="alert alert-error">Unable to get footer :( Check client config!</div>';
};
};
};
};
// handle menu config, loop through custom menu items
for(var i=0;i<config.menuitems.length;i++){
var obj = config.menuitems[i];
for(var key in obj){
// create divider li
li = document.createElement("li");
li.className = "divider-vertical";
document.getElementById('navmenu').appendChild(li);
// create link li
li = document.createElement("li");
li.id = key;
li.className = "menuitem";
a = document.createElement("a");
a.href="javascript:hfShowPage('" + obj[key] + "','" + key + "');";
linkText = document.createTextNode(key);
a.appendChild(linkText);
li.appendChild(a);
document.getElementById('navmenu').appendChild(li);
}
}
} else {
// unable to get config, show error
hfSetContent('<div class="alert alert-error">Unable to get client config, contact operator.</div>\n','upload');
return;
};
};
// send initial request to get client config
xhr.send();
// load serverinfo
var xhr2 = new XMLHttpRequest();
xhr2.open('GET', '/api/serverinfo', true);
xhr2.onload = function(e) {
if (this.status == 200) {
serverinfo = JSON.parse(xhr2.responseText);
// OK, handle request
hfhandlerequest();
} else {
// unable to get serverinfo, show error
hfSetContent('<div class="alert alert-error">Unable to get serverinfo, contact operator</div>\n','upload');
return;
};
};
// send initial request to get server settings
xhr2.send();
if(typeof(Worker) == "undefined") {
// web workers not supported
alert("web workers not supported in this browser");
};
};