-
-
Save thekiur/362ea4cfc45ae6a61762 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EKEY = 'test123456'; | |
if(!window.requestFileSystem) window.requestFileSystem = window.webkitRequestFileSystem; | |
Cloud = { | |
errors : { | |
EID : 'Invalid file id', | |
ENODE : 'Invalid node id', | |
ENOENT : 'File not found', | |
EACCESS : 'Access denied', | |
EQUOTA : 'Quota exceeded', | |
EUCHUNK : 'Bad upload chunk', | |
EAGAIN : 'Try again', | |
EFAIL : 'Transmission failed' | |
}, | |
fileReader : { | |
entries : [], | |
readFile_webkit : function (file, rStart, rEnd, onload) { | |
var start = parseInt(opt_startByte) || 0; | |
var stop = parseInt(opt_stopByte) || file.size - 1; | |
if(stop > file.size - 1) stop = file.size - 1; | |
Cloud.fileReader.entries.push({file : file, rStart : rStart, rEnd : rEnd}); | |
var entry = Cloud.fileReader.entries[Cloud.fileReader.entries.length-1]; | |
var reader = new FileReader(); | |
reader.onloadend = function(e) { | |
if (e.target.readyState == FileReader.DONE) { | |
entry.rawData = e.target.result; | |
entry.cipherText = Aes.Ctr.encrypt(e.target.result, EKEY, 256, true); // true = return raw ciphertext | |
entry.cipherText64 = btoa(entry.cipherText); // convert to Base64 for safe transmission | |
document.getElementById('base64_content').innerHTML = entry.cipherText64; | |
typeof onload === 'function' && onload(entry); | |
} | |
} | |
var blob = file.slice(start, stop + 1); | |
reader.readAsBinaryString(blob); // ? | |
} | |
} | |
} | |
Upload = function(name, destNode, key, chunkSize, parralel) { | |
var self = this; | |
this.name = name || 'Unnamed File'; | |
this.destNode = destNode || '.'; | |
this.key = key || ''; | |
this.chunkSize = parseInt(chunkSize || 1024*1024); | |
this.parralel = parseInt(parralel || 1); | |
this.finished = false; | |
this.failed = false; | |
this.xhr = new XMLHttpRequest(); | |
this.xhr.open('GET', '/upload.php?name='+encodeURIComponent(this.name)+'&node='+encodeURIComponent(this.destNode)+'&chunkSize='+this.chunkSize, true); | |
this.xhr.onload = function(e) { | |
if(this.status == 200) { | |
var data = JSON.parse(this.response); | |
self.uploadURL = '/upload.php?id=' + data.token; | |
console.log(data); | |
console.log('Initialized upload of file ' + data.token); | |
} | |
else { | |
console.log(this.status); | |
} | |
} | |
console.log('Requesting upload channel...'); | |
this.xhr.send(); | |
} | |
Upload.prototype.sendChunk = function(chunkData, chunkNumber, onload) { | |
chunkNumber = chunkNumber || 0; | |
var self = this; | |
var xhr = new XMLHttpRequest(); | |
var rStart = chunkNumber*this.chunkSize; | |
var rEnd = (chunkNumber+1)*this.chunkSize; | |
rEnd = rEnd > this.size ? this.size - 1 : rEnd; | |
xhr.open('POST', this.uploadURL + '&range='+rStart+'-'+rEnd, true); | |
xhr.onload = function(e) { | |
if(this.status == 200) { | |
self.finished = true; | |
var data = JSON.parse(this.response); | |
typeof onload === 'function' && onload(data); | |
} | |
else { | |
self.failed = true; | |
console.log('Unexpected HTTP status code: ' + this.status); | |
} | |
} | |
xhr.send(chunkData); | |
} | |
Download = function(id, key, name, type, chunkSize, parralel) { | |
var self = this; | |
if(!id) | |
return false; | |
this.id = id; | |
this.key = key || ''; | |
this.name = name || 'Unnamed File'; | |
this.type = type || 'application/octet-stream'; | |
this.chunkSize = parseInt(chunkSize || 1024*1024); | |
this.parralel = parseInt(parralel || 1); | |
console.log('Initialized download of file ' + id + ' as ' + this.type); | |
} | |
Download.prototype.start = function() { | |
var self = this; | |
this.xhr = new XMLHttpRequest(); | |
this.xhr.open('GET', '/download.php?id=' + this.id, true); | |
this.xhr.onprogress = function(e) { | |
if(e.lengthComputable) { | |
var progress = Math.floor((e.loaded/e.total)*100); | |
} | |
else { | |
console.log('Could not compute progress'); | |
} | |
} | |
this.xhr.onload = function(e) { | |
console.log('Download finished, decrypting...'); | |
var result = Aes.Ctr.decrypt(xhr.response, self.key, 256); | |
saveAs(new Blob([result], {type : self.type}), self.name); | |
console.log('File saved'); | |
} | |
this.xhr.send(); | |
} | |
Download.prototype.abort = function() { | |
this.xhr.abort(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment