function uploadFile(filename, file, sha256) {
/* send a GET request first to ask if the file
already exists and get the upload URL */
var data = {};
if (sha256)
data = {'sha256': sha256};
var upload_url = $('#frm-file').attr('action');
$.ajax({
type: 'GET',
async: true,
url: upload_url,
dataType: 'json',
data: data,
context: {'filename': filename},
cache: false,
success: function(response){
if (response.file_exists) {
$('#btn-file-reanalyse').attr(
'href', response.reanalyse_url + '&filename=' + this.filename);
$('#btn-file-view-last-analysis').attr(
'href', response.last_analysis_url);
$('#dlg-upload-progress').modal('hide');
$('div#dlg-file-analysis-confirmation span#last-analysis-date').html(
response.last_analysis_date);
$('div#dlg-file-analysis-confirmation span#detection-ratio').html(
'' + response.detection_ratio[0] + '/' + response.detection_ratio[1]);
$('#dlg-file-analysis-confirmation').modal('show');
}
else {
/* if browser have FormData support send the file via XMLHttpRequest
with upload progress bar, else send it the standard way */
if (file && window.FormData) {
var fd = new FormData();
fd.append('file', file);
fd.append('ajax','true');
/* Due to a bug in AppEngine we have to send the IP of the user as a
param in this post. The server is sending us the IP it saw in the
GET request, so we can send it back in the POST. This workaround
should be removed when the issue is solved. See:
(http://code.google.com/p/googleappengine/issues/detail?id=5175) */
fd.append('remote_addr', response.remote_addr);
if (sha256)
fd.append('sha256', sha256);
if (selectedFile.lastModifiedDate != undefined)
fd.append(
'last_modified', selectedFile.lastModifiedDate.toISOString());
currentUpload = new XMLHttpRequest();
currentUpload.upload.addEventListener(
'progress',
uploadProgress,
false);
currentUpload.addEventListener('load', uploadComplete, false);
currentUpload.addEventListener('error', uploadFailed, false);
currentUpload.open('POST', response.upload_url);
currentUpload.send(fd);
} else {
$('#frm-file').attr('action', response.upload_url);
$('#frm-file').submit();
/* in IE 7 animated GIFs freeze immediately after submit, we need
this hack to reload the GIF and make the animation work during
the file upload */
$('#gif-upload-progress-bar span').html(
'<img style="display:block" src="/static/img/bar.gif">');
}
}
}
}); // $.ajax()
}
function canUserWorker() {
if (window.FileReader && window.Worker) {
var major_version = parseInt(jQuery.browser.version, 10);
if (jQuery.browser.opera)
return false;
if (jQuery.browser.mozilla && major_version >= 8)
return true;
if (jQuery.browser.webkit && major_version >= 535)
return true;
}
return false;
}
function scanFile(evt) {
if (!selectedFileName) {
return;
}
if (selectedFile && selectedFile.size > 64*1024*1024) {
$('#dlg-file-too-large').modal('show');
return;
}
$('#dlg-upload-progress').modal('show');
/* if browser has support for File API and Web Workers, calculate hash before
upload in a separate thread. Opera supports both, but its postMessage
implementation doesn't allow to pass a File object as a parameter, so we
can't send the file to the worker */
if (canUserWorker()){
$('#upload-progress-bar').hide();
$('#hash-progress').css('width','0%');
$('#hash-progress-bar').show();
worker = new Worker('/static/js/sha256.js');
worker.onmessage = function(e) {
if (e.data.progress) {
$('#hash-progress').css('width', e.data.progress + '%');
}
else {
$('#hash-progress-bar').hide();
$('#upload-progress').css('width','0%');
$('#upload-progress-bar').show();
uploadFile(selectedFileName, selectedFile, e.data.sha256);
}
};
worker.postMessage({file: selectedFile});
}
else {
$('#gif-upload-progress-bar').show();
uploadFile(selectedFileName, null, null);
}
}