2015-06-11 06:02:46 +02:00
|
|
|
upload.modules.addmodule({
|
|
|
|
name: 'updown',
|
|
|
|
init: function () {
|
2015-06-25 19:44:20 +02:00
|
|
|
// We do this to try to hide the fragment from the referral in IE
|
|
|
|
this.requestframe = document.createElement('iframe')
|
|
|
|
this.requestframe.src = 'about:blank'
|
|
|
|
this.requestframe.style.visibility = 'hidden'
|
|
|
|
document.body.appendChild(this.requestframe)
|
2015-06-11 06:02:46 +02:00
|
|
|
},
|
|
|
|
downloadfromident: function(seed, progress, done, ident) {
|
2015-06-25 19:44:20 +02:00
|
|
|
var xhr = new this.requestframe.contentWindow.XMLHttpRequest()
|
2015-06-11 06:02:46 +02:00
|
|
|
xhr.onload = this.downloaded.bind(this, seed, progress, done)
|
|
|
|
xhr.open('GET', (upload.config.server ? upload.config.server : '') + 'i/' + ident.ident)
|
|
|
|
xhr.responseType = 'blob'
|
2015-06-11 08:47:11 +02:00
|
|
|
xhr.onerror = this.onerror.bind(this, progress)
|
2015-06-11 06:02:46 +02:00
|
|
|
xhr.addEventListener('progress', progress, false)
|
|
|
|
xhr.send()
|
|
|
|
},
|
2015-06-11 08:47:11 +02:00
|
|
|
onerror: function(progress) {
|
|
|
|
progress('error')
|
|
|
|
},
|
2015-06-11 06:02:46 +02:00
|
|
|
downloaded: function (seed, progress, done, response) {
|
2015-06-11 08:47:11 +02:00
|
|
|
if (response.target.status != 200) {
|
|
|
|
this.onerror(progress)
|
|
|
|
} else {
|
2015-06-12 00:14:59 +02:00
|
|
|
this.cache(seed, response.target.response)
|
2015-06-11 08:47:11 +02:00
|
|
|
progress('decrypting')
|
2015-06-12 00:14:59 +02:00
|
|
|
crypt.decrypt(response.target.response, seed).done(done)
|
2015-06-11 08:47:11 +02:00
|
|
|
}
|
2015-06-11 06:02:46 +02:00
|
|
|
},
|
|
|
|
encrypted: function(progress, done, data) {
|
|
|
|
var formdata = new FormData()
|
2015-06-24 01:13:41 +02:00
|
|
|
formdata.append('api_key', upload.config.api_key)
|
2015-06-11 06:02:46 +02:00
|
|
|
formdata.append('ident', data.ident)
|
|
|
|
formdata.append('file', data.encrypted)
|
|
|
|
$.ajax({
|
|
|
|
url: (upload.config.server ? upload.config.server : '') + 'up',
|
|
|
|
data: formdata,
|
|
|
|
cache: false,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
dataType: 'json',
|
|
|
|
xhr: function () {
|
|
|
|
var xhr = new XMLHttpRequest()
|
|
|
|
xhr.upload.addEventListener('progress', progress, false)
|
|
|
|
return xhr
|
|
|
|
},
|
|
|
|
type: 'POST'
|
|
|
|
}).done(done.bind(undefined, data))
|
|
|
|
},
|
2015-06-11 09:12:04 +02:00
|
|
|
cache: function(seed, data) {
|
|
|
|
this.cached = data
|
2015-06-12 00:06:15 +02:00
|
|
|
this.cached_seed = seed
|
2015-06-11 09:12:04 +02:00
|
|
|
},
|
|
|
|
cacheresult: function(data) {
|
2015-06-11 09:13:25 +02:00
|
|
|
this.cache(data.seed, data.encrypted)
|
2015-06-11 09:12:04 +02:00
|
|
|
},
|
2015-06-11 06:02:46 +02:00
|
|
|
download: function (seed, progress, done) {
|
2015-06-11 09:12:04 +02:00
|
|
|
if (this.cached_seed == seed) {
|
|
|
|
progress('decrypting')
|
2015-06-24 02:55:58 +02:00
|
|
|
crypt.decrypt(this.cached, seed).done(done).progress(progress)
|
2015-06-11 09:12:04 +02:00
|
|
|
} else {
|
|
|
|
crypt.ident(seed).done(this.downloadfromident.bind(this, seed, progress, done))
|
|
|
|
}
|
2015-06-11 06:02:46 +02:00
|
|
|
},
|
|
|
|
upload: function (blob, progress, done) {
|
2015-06-24 02:55:58 +02:00
|
|
|
crypt.encrypt(blob).done(this.encrypted.bind(this, progress, done)).done(this.cacheresult.bind(this)).progress(progress)
|
2015-06-11 06:02:46 +02:00
|
|
|
}
|
|
|
|
})
|