-
Notifications
You must be signed in to change notification settings - Fork 3
/
hushfile-download.js
164 lines (146 loc) · 5.44 KB
/
hushfile-download.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
// function to download and decrypt metadata
function hfGetMetadata(fileid) {
var password = window.location.hash.substr(1);
// download and decrypt metadata
var xhr2 = new XMLHttpRequest();
xhr2.open('GET', '/api/metadata?fileid='+fileid, true);
xhr2.onload = function(e) {
if (this.status == 200) {
// decrypt metadata
try {
metadata = CryptoJS.AES.decrypt(this.response, password).toString(CryptoJS.enc.Utf8);
} catch(err) {
content = '<div class="alert alert-error">Unable to decrypt metadata, invalid password.</div>\n';
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');
return;
};
if(metadata != 'undefined') {
try {
var jsonmetadata = JSON.parse(metadata);
$('#metadata').show();
$('#filename').html(jsonmetadata.filename);
$('#mimetype').html(jsonmetadata.mimetype);
$('#filesize').html(jsonmetadata.filesize);
$('#deletepassword').html(jsonmetadata.deletepassword);
} catch(err) {
hfSetContent('<div class="alert alert-error">Unable to parse metadata, sorry.</div>\n','download');
return;
};
};
} else {
hfSetContent('<div class="alert alert-error">Unable to download metadata, sorry.</div>\n','download');
return;
};
};
xhr2.send();
// create XHR to get IP
var ipxhr = new XMLHttpRequest();
ipxhr.open('GET', '/api/ip?fileid='+fileid, true);
ipxhr.onload = function(e) {
if (this.status == 200) {
var jsonip = JSON.parse(ipxhr.responseText);
document.getElementById('clientip').innerHTML = jsonip.uploadip;
} else {
alert("An error was encountered getting uploader ip.");
};
};
// send IP request
ipxhr.send();
}
//function that downloads the file to the browser, and decrypts and shows download button
function hfDownload(fileid, totalchunks, totalsize) {
// get password from window.location
var password = window.location.hash.substr(1);
var downloader = new HushFileDownloader({
onloadstart: function(e) {
console.log("download started");
$('#downloadbtn').addClass("btn btn-large btn-primary btn-success disabled");
$('#downloading').show();
},
onprogress: function(e) {
console.log("progress" + e.loaded + '/' + e.total);
temp = Math.round((e.loaded / e.total) * 100);
$('#download_progress_bar_percent').css('width',temp + '%');
$('#download_progress_bar_percent').text(temp + '%');
},
onload: function(e) {
console.log("download complete");
//done downloading, make downloading div green and change icon
$('#downloading').css('color', 'green');
$('#downloadingdone').removeClass('icon-spinner icon-spin').addClass("icon-check"); //explicitly clear classes?
//make the decrypting div visible
$('#decrypting').show().css('color', 'green');
//done decrypting, change icon and make div green
$('#decryptingdone').removeClass('icon-spinner icon-spin').addClass("icon-check");
// download button
a = document.createElement("a");
a.href = e.url;
a.download = $('#filename').html();
linkText = document.createTextNode(" Download");
i = document.createElement("i");
i.className="icon-save icon-large";
a.appendChild(i);
a.appendChild(linkText);
a.className = "btn btn-large btn-primary btn-success";
$('#downloaddiv').append(a);
//make div visible
$('#downloaddiv').show();
// if this is an image, make a preview
if((/image/i).test($('#mimetype').html())){
img = document.createElement("img");
img.className="img-rounded";
img.src = e.url;
a = document.createElement("a");
a.href = e.url;
a.download = document.getElementById('filename').innerHTML;
a.appendChild(img);
$('#filepreview').append(a);
$('#previewdiv').show();
};
}
});
downloader.download(fileid, password);
}
// function to redirect the browser after a new password has been entered
function hfPwRedirect(fileid) {
password = $('#password').value;
window.location = "/"+fileid+"#"+password;
// show download page
hfShowPage('download.html','download');
};
//function that deletes the file
function hfDeleteFile(fileid) {
// disable the delete button
$('#delete').addClass("btn btn-large btn-primary btn-success disabled");
$('#deleting').show();
var xhr = new XMLHttpRequest();
xhr.open('GET', '/api/delete?fileid='+fileid+'&deletepassword='+$('#deletepassword').html(), true);
xhr.onload = function(e) {
$('#deleteresponse').show();
if (this.status == 200) {
//parse response json
var responseobject = JSON.parse(xhr.responseText);
if(responseobject.deleted) {
//file deleted OK
$('#deletingdone').removeClass('icon-spinner icon-spin').addClass("icon-check");
$('#deleteresponse').html("<div class='alert alert-success'>File deleted successfully</div>\n");
} else {
//unable to delete file
$('#deletingdone').removeClass('icon-spinner icon-spin').addClass("icon-warning-sign");
$('#deleteresponse').html("<div class='alert alert-error'>Unable to delete file</div>\n");
};
} else if (this.status == 401) {
$('#deletingdone').addClass("icon-warning-sign");
$('#deleteresponse').html("<div class='alert alert-error'>Incorrect deletepassword</div>\n");
};
};
xhr.send();
}
function hfDeleteConfirm(result) {
if (result === true) {
hfDeleteFile(window.location.pathname.substr(1));
}
}