add pdf upload support
This commit is contained in:
parent
2b0697883f
commit
8eb70f5512
6 changed files with 399 additions and 7 deletions
205
static/js/pandora/chunkupload.js
Normal file
205
static/js/pandora/chunkupload.js
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
// vi:si:et:sw=4:sts=4:ts=4
|
||||
// GPL2+/MIT 2012
|
||||
'use strict';
|
||||
/*
|
||||
Usage:
|
||||
pandora.chunkupload({
|
||||
file: file,
|
||||
url: '/add',
|
||||
data: {'name': file.name}
|
||||
}).bindEvent({
|
||||
progress: function(data) {
|
||||
console.log(data.progress);
|
||||
},
|
||||
done: function(result) {
|
||||
if(result.progress == 1) {
|
||||
var response = JSON.parse(result.responseText);
|
||||
if(response.resultUrl) {
|
||||
document.location.href = response.resultUrl;
|
||||
} else {
|
||||
alert(response.status;
|
||||
}
|
||||
} else {
|
||||
alert('!!!');
|
||||
}
|
||||
}
|
||||
});
|
||||
*/
|
||||
pandora.chunkupload = function(options) {
|
||||
var chunkSize = options.size || 1024*1024,
|
||||
chunkUrl,
|
||||
file = options.file,
|
||||
maxRetry = -1,
|
||||
retries = 0,
|
||||
request,
|
||||
that = Ox.Element();
|
||||
|
||||
initUpload();
|
||||
|
||||
function done() {
|
||||
that.triggerEvent('done', {
|
||||
status: that.status,
|
||||
progress: that.progress,
|
||||
responseText: that.responseText
|
||||
});
|
||||
}
|
||||
|
||||
function initUpload() {
|
||||
//request upload slot from server
|
||||
that.status = 'requesting chunk upload';
|
||||
that.progress = 0;
|
||||
request = new XMLHttpRequest();
|
||||
request.addEventListener('load', function (evt) {
|
||||
var response = {};
|
||||
that.responseText = evt.target.responseText;
|
||||
try {
|
||||
response = JSON.parse(evt.target.responseText);
|
||||
} catch(e) {
|
||||
response = {};
|
||||
that.status = 'failed to parse response';
|
||||
that.progress = -1;
|
||||
done();
|
||||
}
|
||||
if (response.maxRetry) {
|
||||
maxRetry = response.maxRetry;
|
||||
}
|
||||
chunkUrl = response.uploadUrl;
|
||||
if (document.location.protocol == 'https:') {
|
||||
chunkUrl = chunkUrl.replace(/http:\/\//, 'https://');
|
||||
}
|
||||
if (chunkUrl) {
|
||||
that.status = 'uploading';
|
||||
that.progress = 0.0;
|
||||
//start upload
|
||||
uploadChunk(0);
|
||||
} else {
|
||||
that.status = 'upload failed, no upload url provided';
|
||||
that.progress = -1;
|
||||
done();
|
||||
}
|
||||
}, false);
|
||||
request.addEventListener('error', function (evt) {
|
||||
that.status = 'uplaod failed';
|
||||
that.progress = -1;
|
||||
that.responseText = evt.target.responseText;
|
||||
done();
|
||||
}, false);
|
||||
request.addEventListener('abort', function (evt) {
|
||||
that.status = 'aborted';
|
||||
that.progress = -1;
|
||||
done();
|
||||
}, false);
|
||||
var formData = new FormData();
|
||||
|
||||
Object.keys(options.data).forEach(function(key) {
|
||||
formData.append(key, options.data[key]);
|
||||
});
|
||||
request.open('POST', options.url);
|
||||
request.send(formData);
|
||||
}
|
||||
|
||||
function progress(p) {
|
||||
that.progress = p;
|
||||
that.triggerEvent('progress', {
|
||||
progress: that.progress,
|
||||
status: that.status
|
||||
});
|
||||
}
|
||||
|
||||
function uploadChunk(chunkId) {
|
||||
var bytesAvailable = file.size,
|
||||
chunk,
|
||||
chunkOffset = chunkId * chunkSize;
|
||||
|
||||
if(file.mozSlice) {
|
||||
chunk = file.mozSlice(chunkOffset, chunkOffset+chunkSize, file.type);
|
||||
} else if(file.webkitSlice) {
|
||||
chunk = file.webkitSlice(chunkOffset, chunkOffset+chunkSize, file.type);
|
||||
} else if(file.slice) {
|
||||
chunk = file.slice(chunkOffset, chunkOffset+chunkSize, file.type);
|
||||
} else {
|
||||
that.status = 'Sorry, your browser is currently not supported.';
|
||||
done()
|
||||
}
|
||||
|
||||
progress(parseFloat(chunkOffset)/bytesAvailable);
|
||||
|
||||
request = new XMLHttpRequest();
|
||||
request.addEventListener('load', function (evt) {
|
||||
var response;
|
||||
that.responseText = evt.target.responseText;
|
||||
try {
|
||||
response = JSON.parse(evt.target.responseText);
|
||||
} catch(e) {
|
||||
response = {};
|
||||
}
|
||||
if (response.done == 1) {
|
||||
//upload finished
|
||||
that.resultUrl = response.resultUrl;
|
||||
that.progress = 1;
|
||||
that.status = 'done';
|
||||
done();
|
||||
} else if (response.result == 1) {
|
||||
//reset retry counter
|
||||
retries = 0;
|
||||
//start uploading next chunk
|
||||
uploadChunk(chunkId + 1);
|
||||
} else {
|
||||
//failed to upload, try again in 5 second
|
||||
retries++;
|
||||
if (maxRetry > 0 && retries > maxRetry) {
|
||||
that.status = 'uplaod failed';
|
||||
that.progress = -1;
|
||||
done();
|
||||
} else {
|
||||
setTimeout(function() {
|
||||
uploadChunk(chunkId);
|
||||
}, 5000);
|
||||
}
|
||||
}
|
||||
}, false);
|
||||
request.addEventListener('error', function (evt) {
|
||||
//failed to upload, try again in 3 second
|
||||
retries++;
|
||||
if (maxRetry > 0 && retries > maxRetry) {
|
||||
that.status = 'uplaod failed';
|
||||
that.progress = -1;
|
||||
done();
|
||||
} else {
|
||||
setTimeout(function() {
|
||||
uploadChunk(chunkId);
|
||||
}, 3000);
|
||||
}
|
||||
}, false);
|
||||
request.upload.addEventListener('progress', function (evt) {
|
||||
if (evt.lengthComputable) {
|
||||
progress(parseFloat(chunkOffset + evt.loaded) / bytesAvailable);
|
||||
}
|
||||
}, false);
|
||||
request.addEventListener('abort', function (evt) {
|
||||
that.status = 'aborted';
|
||||
that.progress = -1;
|
||||
done();
|
||||
}, false);
|
||||
|
||||
var formData = new FormData();
|
||||
Object.keys(options.data).forEach(function(key) {
|
||||
formData.append(key, options.data[key]);
|
||||
});
|
||||
formData.append('chunkId', chunkId);
|
||||
if (bytesAvailable <= chunkOffset + chunkSize) {
|
||||
formData.append('done', 1);
|
||||
}
|
||||
formData.append('chunk', chunk);
|
||||
request.open('POST', chunkUrl, true);
|
||||
request.send(formData);
|
||||
}
|
||||
|
||||
that.abort = function() {
|
||||
if (request) {
|
||||
request.abort();
|
||||
request = null;
|
||||
}
|
||||
};
|
||||
return that;
|
||||
};
|
||||
|
|
@ -25,13 +25,32 @@ pandora.ui.text = function() {
|
|||
pandora.UI.set({text: ''});
|
||||
} else if (result.data.type == 'pdf') {
|
||||
$text && $text.remove();
|
||||
$text = Ox.Editable({
|
||||
clickLink: pandora.clickLink,
|
||||
editable: false,
|
||||
type: 'textarea',
|
||||
value: 'REPLACE ME WITH PDF VIEWER'
|
||||
})
|
||||
.appendTo(that);
|
||||
$text = Ox.Element().appendTo(that);
|
||||
if(result.data.available) {
|
||||
$text.append(
|
||||
$('<iframe>').attr({
|
||||
src: '/text/' + ui.text + '/text.pdf',
|
||||
frameBorder: 0
|
||||
}).css({
|
||||
width: '100%',
|
||||
height: '100%'
|
||||
})
|
||||
);
|
||||
} else {
|
||||
Ox.FileButton({
|
||||
maxFiles: 1,
|
||||
title: 'Select PDF'
|
||||
}).bindEvent({
|
||||
click: function(data) {
|
||||
if(data.files.length) {
|
||||
pandora.$ui.uploadPDFDialog = pandora.ui.uploadPDFDialog({
|
||||
file: data.files[0],
|
||||
id: ui.text
|
||||
}).open();
|
||||
}
|
||||
}
|
||||
}).appendTo($text);
|
||||
}
|
||||
} else {
|
||||
var text = result.data ? result.data.text : '';
|
||||
$text && $text.remove();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue