var Uploader = { roomId: null, createRoom: function() { var self = this; $.post('/room', {}, function(response) { self.roomId = $.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+'/'+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(); CryptoUtils.initialize() .then(function(){ window.location = window.location.href+"#"+Uploader.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()) { $("