144 lines
3.4 KiB
JavaScript
144 lines
3.4 KiB
JavaScript
var Uploader = {
|
|
|
|
roomId: null,
|
|
|
|
createRoom: function(callback) {
|
|
var self = this;
|
|
$.post('/room', {}, function(response) {
|
|
callback($.parseJSON(response).id);
|
|
})
|
|
},
|
|
|
|
getRoom: function(callback) {
|
|
$.getJSON('/room/'+this.roomId, callback)
|
|
},
|
|
|
|
getFile: function(fileName, callback) {
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.open('GET', '/room/'+this.roomId+'/'+encodeURIComponent(fileName), true);
|
|
xhr.responseType = 'arraybuffer';
|
|
xhr.onload = callback;
|
|
xhr.send();
|
|
},
|
|
|
|
deleteRoom: function() {
|
|
$.ajax({
|
|
type: 'DELETE',
|
|
url: '/room/'+this.roomId
|
|
})
|
|
},
|
|
|
|
uploadFile: function(name, data){
|
|
var formData = new FormData();
|
|
formData.append("file", data, name);
|
|
$.ajax({
|
|
url: '/room/'+this.roomId,
|
|
data: formData,
|
|
cache: false,
|
|
processData: false,
|
|
contentType: false,
|
|
xhr: function () {
|
|
var xhr = new XMLHttpRequest()
|
|
//xhr.upload.addEventListener('progress', progress, false)
|
|
return xhr
|
|
},
|
|
type: 'POST'
|
|
});
|
|
},
|
|
|
|
deleteFile: function() {
|
|
console.log('NOPE');
|
|
},
|
|
}
|
|
|
|
var CryptoUploader = {
|
|
|
|
createRoom: function() {
|
|
Uploader.createRoom(function(roomId){
|
|
CryptoUtils.initialize()
|
|
.then(function(){
|
|
window.location = window.location.href+"#"+roomId+","+CryptoUtils.urlSafeKey;
|
|
window.location.reload();
|
|
})
|
|
});
|
|
},
|
|
|
|
uploadFile: function() {
|
|
var fileReader = new FileReader();
|
|
var data;
|
|
var file = document.getElementById("file-selector").files[0];
|
|
var self = this;
|
|
fileReader.onload = function(e) {
|
|
this.data = fileReader.result;
|
|
CryptoUtils.encrypt(this.data)
|
|
.then(function(encrypted){
|
|
Uploader.uploadFile(file.name, encrypted);
|
|
self.showRoomContent();
|
|
}).catch(function(err){
|
|
console.error(err);
|
|
});
|
|
};
|
|
fileReader.readAsArrayBuffer(file);
|
|
},
|
|
|
|
downloadFile: function(fileName) {
|
|
Uploader.getFile(fileName, function(e){
|
|
if(this.status == 200) {
|
|
CryptoUtils.decrypt(this.response).
|
|
then(function(decrypted){
|
|
var a = document.createElement("a");
|
|
document.body.appendChild(a);
|
|
a.style = "display: none";
|
|
blob = new Blob([decrypted], {type: "application/octet-binary"}),
|
|
url = window.URL.createObjectURL(blob);
|
|
a.href = url;
|
|
a.download = fileName;
|
|
a.click();
|
|
window.URL.revokeObjectURL(url);
|
|
}).catch(function(err){
|
|
console.error(err);
|
|
})
|
|
}
|
|
})
|
|
},
|
|
|
|
showRoomContent: function() {
|
|
$("#file-list > ul").empty();
|
|
Uploader.getRoom(function(data){
|
|
for(let f of data.entries()) {
|
|
$("<li><a>"+f[1]+"</a></li>")
|
|
.appendTo("#file-list > ul")
|
|
.on('click', function(e){CryptoUploader.downloadFile(e.target.text)});
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
$(function(){
|
|
if(window.location.hash) {
|
|
var params = window.location.hash.substr(1).split(',');
|
|
var roomId = params.shift();
|
|
var key = params.shift();
|
|
var file = params.shift();
|
|
$("#create-room").hide();
|
|
$("#room").show();
|
|
Uploader.roomId = roomId;
|
|
CryptoUtils.initialize(key).then(function(){
|
|
$("#woot").on('click', function(){
|
|
CryptoUploader.uploadFile();
|
|
});
|
|
CryptoUploader.showRoomContent();
|
|
if(file !== undefined) {
|
|
CryptoUploader.downloadFile(file);
|
|
}
|
|
}).catch(function(err){
|
|
console.error(err);
|
|
});
|
|
}
|
|
else {
|
|
$("#room").hide();
|
|
$("#create-room > input").show().on('click', function(e){
|
|
CryptoUploader.createRoom();
|
|
});
|
|
}
|
|
})
|