Use lufi-api and WebCrypto
This commit is contained in:
parent
c79f2c5662
commit
9695a615da
File diff suppressed because one or more lines are too long
|
@ -1,31 +1,47 @@
|
||||||
// vim:set sw=4 ts=4 sts=4 ft=javascript expandtab:
|
// vim:set sw=4 ts=4 sts=4 ft=javascript expandtab:
|
||||||
|
|
||||||
|
import * as lufiApi from "/js/lufi-api.browser.js"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return the deciphering key stored in anchor part of the URL
|
* Return the deciphering key stored in anchor part of the URL
|
||||||
* Stolen from https://github.com/sebsauvage/ZeroBin/blob/master/js/zerobin.js
|
* Stolen from https://github.com/sebsauvage/ZeroBin/blob/master/js/zerobin.js
|
||||||
*/
|
*/
|
||||||
function pageKey() {
|
function pageKey() {
|
||||||
var key = window.location.hash.substring(1); // Get key
|
var key = window.location.hash.substring(1); // Get key
|
||||||
|
let i;
|
||||||
|
|
||||||
// Some stupid web 2.0 services and redirectors add data AFTER the anchor
|
// Some stupid web 2.0 services and redirectors add data AFTER the anchor
|
||||||
// (such as &utm_source=...).
|
// (such as &utm_source=...).
|
||||||
// We will strip any additional data.
|
// We will strip any additional data.
|
||||||
|
|
||||||
// First, strip everything after the equal sign (=) which signals end of base64 string.
|
// First, strip everything after the equal sign (=) which signals end of base64 string.
|
||||||
i = key.indexOf('='); if (i>-1) { key = key.substring(0, i + 1); }
|
i = key.indexOf('=');
|
||||||
|
let isb64 = false
|
||||||
|
|
||||||
|
if (i>-1) {
|
||||||
|
key = key.substring(0, i + 1);
|
||||||
|
|
||||||
|
isb64 = true
|
||||||
|
}
|
||||||
|
|
||||||
// If the equal sign was not present, some parameters may remain:
|
// If the equal sign was not present, some parameters may remain:
|
||||||
i = key.indexOf('&'); if (i>-1) { key = key.substring(0, i); }
|
i = key.indexOf('&'); if (i>-1) { key = key.substring(0, i); }
|
||||||
|
|
||||||
// Then add trailing equal sign if it's missing
|
// Then add trailing equal sign if it's missing and was using the Sjcl algorithm
|
||||||
if (key.charAt(key.length-1)!=='=') key += '=';
|
if (isb64) {
|
||||||
|
if (key.charAt(key.length-1)!=='=') key += '=';
|
||||||
|
}
|
||||||
|
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
function base64ToArrayBuffer(base64) {
|
function base64ToArrayBuffer(base64) {
|
||||||
var binary_string = window.atob(base64);
|
base64 = base64 instanceof ArrayBuffer ? new TextDecoder().decode(base64) : base64; // Is it using Lufi API?
|
||||||
|
|
||||||
|
var binary_string = window.atob(base64);
|
||||||
var len = binary_string.length;
|
var len = binary_string.length;
|
||||||
var bytes = new Uint8Array( len );
|
var bytes = new Uint8Array(len);
|
||||||
for (var i = 0; i < len; i++) {
|
for (var i = 0; i < len; i++) {
|
||||||
bytes[i] = binary_string.charCodeAt(i);
|
bytes[i] = binary_string.charCodeAt(i);
|
||||||
}
|
}
|
||||||
return bytes.buffer;
|
return bytes.buffer;
|
||||||
|
@ -48,11 +64,11 @@ function addAlert(msg) {
|
||||||
// Spawn WebSocket
|
// Spawn WebSocket
|
||||||
function spawnWebsocket(pa) {
|
function spawnWebsocket(pa) {
|
||||||
console.log('Spawning websocket…');
|
console.log('Spawning websocket…');
|
||||||
var ws = new WebSocket(ws_url);
|
var ws = new WebSocket(ws_url);
|
||||||
ws.onopen = function() {
|
ws.onopen = function () {
|
||||||
console.log('Connection is established!');
|
console.log('Connection is established!');
|
||||||
|
|
||||||
var l = $('#loading');
|
var l = $('#loading');
|
||||||
l.html(i18n.loading.replace(/XX1/, (pa + 1)));
|
l.html(i18n.loading.replace(/XX1/, (pa + 1)));
|
||||||
if ($('#file_pwd').length === 1) {
|
if ($('#file_pwd').length === 1) {
|
||||||
val = $('#file_pwd').val();
|
val = $('#file_pwd').val();
|
||||||
|
@ -61,7 +77,7 @@ function spawnWebsocket(pa) {
|
||||||
window.ws.send(`{"part":${pa}}`);
|
window.ws.send(`{"part":${pa}}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ws.onclose = function() {
|
ws.onclose = function () {
|
||||||
console.log('Connection is closed');
|
console.log('Connection is closed');
|
||||||
if (!window.completed) {
|
if (!window.completed) {
|
||||||
window.attempts++;
|
window.attempts++;
|
||||||
|
@ -73,13 +89,13 @@ function spawnWebsocket(pa) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ws.onmessage = function(e) {
|
ws.onmessage = function (e) {
|
||||||
var res = e.data.split('XXMOJOXX');
|
var res = e.data.split('XXMOJOXX');
|
||||||
var json = res.shift();
|
var json = res.shift();
|
||||||
var data = JSON.parse(json);
|
var data = JSON.parse(json);
|
||||||
|
|
||||||
// Reset counter since we succeded to open a websocket and got a message
|
// Reset counter since we succeded to open a websocket and got a message
|
||||||
window.attempts = 0;
|
window.attempts = 0;
|
||||||
|
|
||||||
if (data.msg !== undefined) {
|
if (data.msg !== undefined) {
|
||||||
addAlert(data.msg);
|
addAlert(data.msg);
|
||||||
|
@ -88,10 +104,16 @@ function spawnWebsocket(pa) {
|
||||||
$('.file-abort').addClass('hide');
|
$('.file-abort').addClass('hide');
|
||||||
}
|
}
|
||||||
window.onbeforeunload = null;
|
window.onbeforeunload = null;
|
||||||
window.attempts = 10;
|
window.attempts = 10;
|
||||||
} else {
|
} else {
|
||||||
console.log(`Getting slice ${data.part + 1} of ${data.total}`);
|
console.log(`Getting slice ${data.part + 1} of ${data.total}`);
|
||||||
var slice = JSON.parse(res.shift());
|
var slice = JSON.parse(res.shift());
|
||||||
|
|
||||||
|
// If file was used using Lufi API
|
||||||
|
if (slice.iv) {
|
||||||
|
slice.iv = new Uint8Array(Object.values(slice.iv))
|
||||||
|
}
|
||||||
|
|
||||||
var percent = Math.round(1000 * (data.part + 1)/data.total)/10;
|
var percent = Math.round(1000 * (data.part + 1)/data.total)/10;
|
||||||
var wClass = percent.toString().replace('.', '-');
|
var wClass = percent.toString().replace('.', '-');
|
||||||
var pb = $('#pb');
|
var pb = $('#pb');
|
||||||
|
@ -101,14 +123,17 @@ function spawnWebsocket(pa) {
|
||||||
pb.attr('aria-valuenow', percent);
|
pb.attr('aria-valuenow', percent);
|
||||||
$('#pbt').html(`${percent}%`);
|
$('#pbt').html(`${percent}%`);
|
||||||
try {
|
try {
|
||||||
var b64 = sjcl.decrypt(window.key, slice);
|
lufiApi.lufiCrypto.decrypt(window.key, slice).then((decrypted) => {
|
||||||
window.a[data.part] = base64ToArrayBuffer(b64);
|
var b64 = decrypted;
|
||||||
if (data.part + 1 === data.total) {
|
|
||||||
var blob = new Blob(a, {type: data.type});
|
|
||||||
|
|
||||||
notify(i18n.fileDownloaded, data.name);
|
window.a[data.part] = base64ToArrayBuffer(b64);
|
||||||
$('#please-wait').remove();
|
|
||||||
$('#loading').remove();
|
if (data.part + 1 === data.total) {
|
||||||
|
var blob = new Blob(a, { type: data.type });
|
||||||
|
|
||||||
|
notify(i18n.fileDownloaded, data.name);
|
||||||
|
$('#please-wait').remove();
|
||||||
|
$('#loading').remove();
|
||||||
|
|
||||||
var pbd = $('#pbd');
|
var pbd = $('#pbd');
|
||||||
pbd.attr('class', 'center-align');
|
pbd.attr('class', 'center-align');
|
||||||
|
@ -181,32 +206,35 @@ function spawnWebsocket(pa) {
|
||||||
window.onbeforeunload = null;
|
window.onbeforeunload = null;
|
||||||
window.completed = true;
|
window.completed = true;
|
||||||
$('#abort').remove();
|
$('#abort').remove();
|
||||||
} else {
|
|
||||||
var l = $('#loading');
|
|
||||||
l.html(i18n.loading.replace(/XX1/, (data.part + 1)));
|
|
||||||
if (ws.readyState === 3) {
|
|
||||||
window.ws = spawnWebsocket(data.part + 1);
|
|
||||||
} else {
|
} else {
|
||||||
window.ws.onclose = function() {
|
var l = $('#loading');
|
||||||
console.log('Connection is closed');
|
l.html(i18n.loading.replace(/XX1/, (data.part + 1)));
|
||||||
if (!window.completed) {
|
if (ws.readyState === 3) {
|
||||||
console.log(`Connection closed. Retrying to get slice ${data.part + 1}`);
|
window.ws = spawnWebsocket(data.part + 1);
|
||||||
|
} else {
|
||||||
|
window.ws.onclose = function() {
|
||||||
|
console.log('Connection is closed');
|
||||||
|
if (!window.completed) {
|
||||||
|
console.log(`Connection closed. Retrying to get slice ${data.part + 1}`);
|
||||||
|
window.ws = spawnWebsocket(data.part + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window.ws.onerror = function() {
|
||||||
|
console.log(`Error. Retrying to get slice ${data.part + 1}`);
|
||||||
window.ws = spawnWebsocket(data.part + 1);
|
window.ws = spawnWebsocket(data.part + 1);
|
||||||
|
};
|
||||||
|
if ($('#file_pwd').length === 1) {
|
||||||
|
val = $('#file_pwd').val();
|
||||||
|
window.ws.send(`{"part":${data.part + 1}, "file_pwd": "${val}"}`);
|
||||||
|
} else {
|
||||||
|
window.ws.send(`{"part":${data.part + 1}}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window.ws.onerror = function() {
|
|
||||||
console.log(`Error. Retrying to get slice ${data.part + 1}`);
|
|
||||||
window.ws = spawnWebsocket(data.part + 1);
|
|
||||||
};
|
|
||||||
if ($('#file_pwd').length === 1) {
|
|
||||||
val = $('#file_pwd').val();
|
|
||||||
window.ws.send(`{"part":${data.part + 1}, "file_pwd": "${val}"}`);
|
|
||||||
} else {
|
|
||||||
window.ws.send(`{"part":${data.part + 1}}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}).catch((e) => {
|
||||||
} catch(err) {
|
console.error(e);
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
if (err.message === 'ccm: tag doesn\'t match') {
|
if (err.message === 'ccm: tag doesn\'t match') {
|
||||||
addAlert(i18n.badkey);
|
addAlert(i18n.badkey);
|
||||||
} else {
|
} else {
|
||||||
|
@ -216,7 +244,7 @@ function spawnWebsocket(pa) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ws.onerror = function() {
|
ws.onerror = function () {
|
||||||
window.attempts++;
|
window.attempts++;
|
||||||
if (window.attempts < 10) {
|
if (window.attempts < 10) {
|
||||||
console.log(`Error. Retrying to get slice ${pa}`);
|
console.log(`Error. Retrying to get slice ${pa}`);
|
||||||
|
@ -228,9 +256,9 @@ function spawnWebsocket(pa) {
|
||||||
return ws;
|
return ws;
|
||||||
}
|
}
|
||||||
// When it's ready
|
// When it's ready
|
||||||
$(document).ready(function(){
|
$(document).ready(function () {
|
||||||
$('#abort').click(function() {
|
$('#abort').click(function () {
|
||||||
window.ws.onclose = function() {};
|
window.ws.onclose = function () { };
|
||||||
window.ws.close();
|
window.ws.close();
|
||||||
$('#please-wait, #loading, #pbd, #abort').remove();
|
$('#please-wait, #loading, #pbd, #abort').remove();
|
||||||
$('#filesize').parent().append(`<h4>${i18n.aborted1}</h4>
|
$('#filesize').parent().append(`<h4>${i18n.aborted1}</h4>
|
||||||
|
@ -241,22 +269,22 @@ $(document).ready(function(){
|
||||||
</a>
|
</a>
|
||||||
</p>`);
|
</p>`);
|
||||||
window.onbeforeunload = null;
|
window.onbeforeunload = null;
|
||||||
$('#reloadLocation').on('click', function(e) {
|
$('#reloadLocation').on('click', function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
$('#filesize').html(filesize($('#filesize').attr('data-filesize'), {base: 10}));
|
$('#filesize').html(filesize($('#filesize').attr('data-filesize'), { base: 10 }));
|
||||||
window.a = new Array();
|
window.a = new Array();
|
||||||
window.key = pageKey();
|
window.key = pageKey();
|
||||||
window.completed = false;
|
window.completed = false;
|
||||||
window.attempts = 0;
|
window.attempts = 0;
|
||||||
|
|
||||||
if (key !== '=') {
|
if (key !== '=') {
|
||||||
var go = true;
|
var go = true;
|
||||||
if ($('#file_pwd').length === 1) {
|
if ($('#file_pwd').length === 1) {
|
||||||
go = false;
|
go = false;
|
||||||
$('#go').click(function() {
|
$('#go').click(function () {
|
||||||
$('.file-progress, .file-abort').removeClass('hide');
|
$('.file-progress, .file-abort').removeClass('hide');
|
||||||
$('#file_pwd').parent().parent().addClass('hide');
|
$('#file_pwd').parent().parent().addClass('hide');
|
||||||
// Set websocket
|
// Set websocket
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
// vim:set sw=4 ts=4 sts=4 ft=javascript expandtab:
|
// vim:set sw=4 ts=4 sts=4 ft=javascript expandtab:
|
||||||
|
|
||||||
|
import * as lufiApi from "/js/lufi-api.browser.js"
|
||||||
|
|
||||||
// total file counter
|
// total file counter
|
||||||
window.fc = 0;
|
window.fc = 0;
|
||||||
// Cancelled files indexes
|
// Cancelled files indexes
|
||||||
window.cancelled = [];
|
window.cancelled = [];
|
||||||
// Set websocket
|
// Set websocket
|
||||||
window.ws = spawnWebsocket(0, function() {return null;});
|
window.ws = spawnWebsocket(0, function () { return null; });
|
||||||
// Use slice of 0.75MB
|
// Use slice of 0.75MB
|
||||||
window.sliceLength = 750000;
|
window.sliceLength = 750000;
|
||||||
// Global zip objects for currently created zip file
|
// Global zip objects for currently created zip file
|
||||||
|
@ -146,7 +148,7 @@ function updateZipname() {
|
||||||
// Create blob from zip
|
// Create blob from zip
|
||||||
function uploadZip(e) {
|
function uploadZip(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
var delay = $('#delete-day');
|
var delay = $('#delete-day');
|
||||||
var del_at_first_view = $('#first-view');
|
var del_at_first_view = $('#first-view');
|
||||||
$('#zip-files').attr('disabled', 'disabled');
|
$('#zip-files').attr('disabled', 'disabled');
|
||||||
$('#file-browser-button').attr('disabled', 'disabled');
|
$('#file-browser-button').attr('disabled', 'disabled');
|
||||||
|
@ -155,8 +157,8 @@ function uploadZip(e) {
|
||||||
$('#zip-parts').text('');
|
$('#zip-parts').text('');
|
||||||
|
|
||||||
$('#zip-compressing').removeClass('hide');
|
$('#zip-compressing').removeClass('hide');
|
||||||
window.zip.generateAsync({type:"blob"})
|
window.zip.generateAsync({ type: "blob" })
|
||||||
.then(function(zipFile) {
|
.then(function (zipFile) {
|
||||||
// if $('#zipping') is hidden, the zipping has been aborted
|
// if $('#zipping') is hidden, the zipping has been aborted
|
||||||
if (!$('#zipping').hasClass('hide')) {
|
if (!$('#zipping').hasClass('hide')) {
|
||||||
window.zip = null;
|
window.zip = null;
|
||||||
|
@ -169,7 +171,7 @@ function uploadZip(e) {
|
||||||
$('#zip-files').attr('disabled', null);
|
$('#zip-files').attr('disabled', null);
|
||||||
|
|
||||||
var zipname = getZipname();
|
var zipname = getZipname();
|
||||||
var file = new File([zipFile], zipname, {type: 'application/zip'});
|
var file = new File([zipFile], zipname, { type: 'application/zip' });
|
||||||
|
|
||||||
Materialize.toast(i18n.enqueued.replace('XXX', zipname), 3000, 'teal accent-3');
|
Materialize.toast(i18n.enqueued.replace('XXX', zipname), 3000, 'teal accent-3');
|
||||||
if (window.fileList === undefined || window.fileList === null) {
|
if (window.fileList === undefined || window.fileList === null) {
|
||||||
|
@ -206,7 +208,7 @@ function sendFilesURLs() {
|
||||||
data: {
|
data: {
|
||||||
urls: window.filesURLs
|
urls: window.filesURLs
|
||||||
},
|
},
|
||||||
success: function(data, textStatus, jqXHR) {
|
success: function (data, textStatus, jqXHR) {
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
Materialize.toast(data.msg, 6000, 'teal accent-3');
|
Materialize.toast(data.msg, 6000, 'teal accent-3');
|
||||||
} else {
|
} else {
|
||||||
|
@ -220,8 +222,8 @@ function sendFilesURLs() {
|
||||||
|
|
||||||
// Start uploading the files (called from <input> and from drop zone)
|
// Start uploading the files (called from <input> and from drop zone)
|
||||||
function handleFiles(f) {
|
function handleFiles(f) {
|
||||||
var delay = $('#delete-day');
|
var delay = $('#delete-day');
|
||||||
var zip_files = $('#zip-files');
|
var zip_files = $('#zip-files');
|
||||||
var del_at_first_view = $('#first-view');
|
var del_at_first_view = $('#first-view');
|
||||||
|
|
||||||
delay.attr('disabled', 'disabled');
|
delay.attr('disabled', 'disabled');
|
||||||
|
@ -234,11 +236,11 @@ function handleFiles(f) {
|
||||||
$('#zipping').removeClass('hide');
|
$('#zipping').removeClass('hide');
|
||||||
$('#files').removeClass('m12').addClass('m6');
|
$('#files').removeClass('m12').addClass('m6');
|
||||||
for (var i = 0; i < f.length; i++) {
|
for (var i = 0; i < f.length; i++) {
|
||||||
var element = f.item(i);
|
var element = f.item(i);
|
||||||
var filename = element.name;
|
var filename = element.name;
|
||||||
var origname = filename;
|
var origname = filename;
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
while (typeof(window.zip.files[filename]) !== 'undefined') {
|
while (typeof (window.zip.files[filename]) !== 'undefined') {
|
||||||
counter += 1;
|
counter += 1;
|
||||||
filename = `${origname.substring(0, origname.lastIndexOf('.'))}_(${counter})${origname.substring(origname.lastIndexOf('.'))}`;
|
filename = `${origname.substring(0, origname.lastIndexOf('.'))}_(${counter})${origname.substring(origname.lastIndexOf('.'))}`;
|
||||||
}
|
}
|
||||||
|
@ -258,7 +260,7 @@ function handleFiles(f) {
|
||||||
var file = window.fileList[i];
|
var file = window.fileList[i];
|
||||||
Materialize.toast(i18n.enqueued.replace('XXX', escapeHtml(file.name)), 3000, 'teal accent-3');
|
Materialize.toast(i18n.enqueued.replace('XXX', escapeHtml(file.name)), 3000, 'teal accent-3');
|
||||||
}
|
}
|
||||||
window.nbFiles = window.fileList.length;
|
window.nbFiles = window.fileList.length;
|
||||||
$('#results').show();
|
$('#results').show();
|
||||||
uploadFile(0, delay.val(), del_at_first_view.is(':checked'));
|
uploadFile(0, delay.val(), del_at_first_view.is(':checked'));
|
||||||
} else {
|
} else {
|
||||||
|
@ -267,27 +269,24 @@ function handleFiles(f) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create random key
|
|
||||||
function genRandomKey() {
|
|
||||||
return sjcl.codec.base64.fromBits(sjcl.random.randomWords(8, 10), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create progress bar and call slicing and uploading function
|
// Create progress bar and call slicing and uploading function
|
||||||
function uploadFile(i, delay, del_at_first_view) {
|
function uploadFile(i, delay, del_at_first_view) {
|
||||||
// Prevent exiting page before full upload
|
// Prevent exiting page before full upload
|
||||||
window.onbeforeunload = confirmExit;
|
window.onbeforeunload = confirmExit;
|
||||||
|
|
||||||
// Create a random key, different for all files
|
// Create a random key, different for all files
|
||||||
var randomkey = genRandomKey();
|
|
||||||
|
|
||||||
// Get the file and properties
|
lufiApi.lufiCrypto.generateKey().then((random) => {
|
||||||
var file = window.fileList[i];
|
var randomKey = random;
|
||||||
var name = escapeHtml(file.name);
|
|
||||||
var size = filesize(file.size);
|
// Get the file and properties
|
||||||
var parts = Math.ceil(file.size/window.sliceLength);
|
var file = window.fileList[i];
|
||||||
if (parts === 0) {
|
var name = escapeHtml(file.name);
|
||||||
parts = 1;
|
var size = filesize(file.size);
|
||||||
}
|
var parts = Math.ceil(file.size / window.sliceLength);
|
||||||
|
if (parts === 0) {
|
||||||
|
parts = 1;
|
||||||
|
}
|
||||||
|
|
||||||
// Create a progress bar for the file
|
// Create a progress bar for the file
|
||||||
var r = $('#ul-results');
|
var r = $('#ul-results');
|
||||||
|
@ -306,7 +305,7 @@ function uploadFile(i, delay, del_at_first_view) {
|
||||||
</div>
|
</div>
|
||||||
<div class="progress">
|
<div class="progress">
|
||||||
<div id="progress-${window.fc}"
|
<div id="progress-${window.fc}"
|
||||||
data-key="${randomkey}"
|
data-key="${randomKey}"
|
||||||
data-name="${name}"
|
data-name="${name}"
|
||||||
aria-valuemax="100"
|
aria-valuemax="100"
|
||||||
aria-valuemin="0"
|
aria-valuemin="0"
|
||||||
|
@ -324,113 +323,126 @@ function uploadFile(i, delay, del_at_first_view) {
|
||||||
destroyBlock(this);
|
destroyBlock(this);
|
||||||
});
|
});
|
||||||
|
|
||||||
sliceAndUpload(randomkey, i, parts, 0, delay, del_at_first_view, null, null);
|
sliceAndUpload(randomKey, i, parts, 0, delay, del_at_first_view, null, null);
|
||||||
|
}).catch((e) => {
|
||||||
|
console.error(e);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a slice of file and send it
|
// Get a slice of file and send it
|
||||||
function sliceAndUpload(randomkey, i, parts, j, delay, del_at_first_view, short, mod_token) {
|
function sliceAndUpload(randomKey, i, parts, j, delay, del_at_first_view, short, mod_token) {
|
||||||
if (mod_token !== null && window.cancelled.includes(i)) {
|
if (mod_token !== null && window.cancelled.includes(i)) {
|
||||||
var data = JSON.stringify({
|
var data = JSON.stringify({
|
||||||
id: short,
|
id: short,
|
||||||
mod_token: mod_token,
|
mod_token: mod_token,
|
||||||
cancel: true,
|
cancel: true,
|
||||||
i: i
|
i: i
|
||||||
})+'XXMOJOXXuseless';
|
}) + 'XXMOJOXXuseless';
|
||||||
// Verify that we have a websocket and send json
|
// Verify that we have a websocket and send json
|
||||||
if (window.ws.readyState === 3) {
|
if (window.ws.readyState === 3) {
|
||||||
window.ws = spawnWebsocket(0, function() {
|
window.ws = spawnWebsocket(0, function () {
|
||||||
window.ws.send(data);
|
window.ws.send(data);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
window.ws.onclose = function() {
|
window.ws.onclose = function () {
|
||||||
console.log('Websocket closed, waiting 10sec.');
|
console.log('Websocket closed, waiting 10sec.');
|
||||||
window.ws = spawnWebsocket(0, function() {return null;});
|
window.ws = spawnWebsocket(0, function () { return null; });
|
||||||
};
|
};
|
||||||
window.ws.onerror = function() {
|
window.ws.onerror = function () {
|
||||||
console.log('Error on Websocket, waiting 10sec.');
|
console.log('Error on Websocket, waiting 10sec.');
|
||||||
window.ws = spawnWebsocket(0, function() {return null;});
|
window.ws = spawnWebsocket(0, function () { return null; });
|
||||||
};
|
};
|
||||||
window.ws.send(data);
|
window.ws.send(data);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var file = window.fileList[i];
|
var file = window.fileList[i];
|
||||||
var slice = file.slice(j * window.sliceLength, (j + 1) * window.sliceLength, file.type);
|
var slice = file.slice(j * window.sliceLength, (j + 1) * window.sliceLength, file.type);
|
||||||
var fr = new FileReader();
|
var fr = new FileReader();
|
||||||
|
|
||||||
fr.onloadend = function() {
|
fr.onloadend = function() {
|
||||||
var sl = $(`#parts-${window.fc}`);
|
var sl = $(`#parts-${window.fc}`);
|
||||||
|
|
||||||
// Get the binary result, different result in IE browsers (see default.html.ep line 27:48)
|
// Get the binary result, different result in IE browsers (see default.html.ep line 27:48)
|
||||||
if (isIE == true){
|
if (isIE == true) {
|
||||||
var bin = fr.content;
|
var bin = fr.content;
|
||||||
} else {
|
} else {
|
||||||
var bin = fr.result;
|
var bin = fr.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transform it in base64
|
// Transform it in base64
|
||||||
var b = window.btoa(bin);
|
var b = window.btoa(bin);
|
||||||
|
|
||||||
// Encrypt it
|
// Encrypt it
|
||||||
var encrypted = sjcl.encrypt(randomkey, b);
|
lufiApi.lufiCrypto.encrypt(randomKey, new TextEncoder().encode(b).buffer).then((encryptedFile) => {
|
||||||
|
let encrypted = encryptedFile;
|
||||||
|
|
||||||
// Prepare json
|
// Prepare json
|
||||||
var data = {
|
var data = {
|
||||||
// number of parts
|
// number of parts
|
||||||
total: parts,
|
total: parts,
|
||||||
// part X of total
|
// part X of total
|
||||||
part: j,
|
part: j,
|
||||||
size: file.size,
|
size: file.size,
|
||||||
name: file.name,
|
name: file.name,
|
||||||
type: file.type,
|
type: file.type,
|
||||||
delay: delay,
|
delay: delay,
|
||||||
del_at_first_view: del_at_first_view,
|
del_at_first_view: del_at_first_view,
|
||||||
zipped: $('#zip-files').is(':checked'),
|
zipped: $('#zip-files').is(':checked'),
|
||||||
id: short,
|
id: short,
|
||||||
// number of the sent file in the queue
|
// number of the sent file in the queue
|
||||||
i: i
|
i: i
|
||||||
};
|
};
|
||||||
if ($('#file_pwd').length === 1) {
|
|
||||||
var pwd = $('#file_pwd').val();
|
if ($('#file_pwd').length === 1) {
|
||||||
if (pwd !== undefined && pwd !== null && pwd !== '') {
|
var pwd = $('#file_pwd').val();
|
||||||
data['file_pwd'] = $('#file_pwd').val();
|
|
||||||
|
if (pwd !== undefined && pwd !== null && pwd !== '') {
|
||||||
|
data['file_pwd'] = $('#file_pwd').val();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
data = `${JSON.stringify(data)}XXMOJOXX${JSON.stringify(encrypted)}`;
|
|
||||||
|
|
||||||
var percent = Math.round(1000 * j/parts)/10;
|
data = `${JSON.stringify(data)}XXMOJOXX${JSON.stringify(encrypted)}`;
|
||||||
console.log(`sending slice ${j + 1}/${parts} of file ${file.name} (${percent}%)`);
|
|
||||||
|
|
||||||
sl.html(`${percent.toFixed(1)}%`);
|
var percent = Math.round(1000 * j/parts)/10;
|
||||||
|
console.log(`sending slice ${j + 1}/${parts} of file ${file.name} (${percent}%)`);
|
||||||
|
|
||||||
|
sl.html(`${percent.toFixed(1)}%`);
|
||||||
|
|
||||||
|
// Verify that we have a websocket and send json
|
||||||
|
if (window.ws.readyState === 3) {
|
||||||
|
window.ws = spawnWebsocket(0, function() {
|
||||||
|
window.ws.send(data);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
window.ws.onclose = function() {
|
||||||
|
console.log('Websocket closed, waiting 10sec.');
|
||||||
|
window.ws = spawnWebsocket(0, function() {
|
||||||
|
console.log(`sending again slice ${j + 1}/${parts} of file ${file.name}`);
|
||||||
|
window.ws.send(data);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
window.ws.onerror = function() {
|
||||||
|
console.log('Error on Websocket, waiting 10sec.');
|
||||||
|
window.ws = spawnWebsocket(0, function() {
|
||||||
|
console.log(`sending again slice ${j + 1}/${parts} of file ${file.name}`);
|
||||||
|
window.ws.send(data);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// Verify that we have a websocket and send json
|
|
||||||
if (window.ws.readyState === 3) {
|
|
||||||
window.ws = spawnWebsocket(0, function() {
|
|
||||||
window.ws.send(data);
|
window.ws.send(data);
|
||||||
});
|
}
|
||||||
} else {
|
}).catch((e) => {
|
||||||
window.ws.onclose = function() {
|
console.error(e);
|
||||||
console.log('Websocket closed, waiting 10sec.');
|
})
|
||||||
window.ws = spawnWebsocket(0, function() {
|
|
||||||
console.log(`sending again slice ${j + 1}/${parts} of file ${file.name}`);
|
|
||||||
window.ws.send(data);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
window.ws.onerror = function() {
|
|
||||||
console.log('Error on Websocket, waiting 10sec.');
|
|
||||||
window.ws = spawnWebsocket(0, function() {
|
|
||||||
console.log(`sending again slice ${j + 1}/${parts} of file ${file.name}`);
|
|
||||||
window.ws.send(data);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
window.ws.send(data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fr.readAsBinaryString(slice);
|
fr.readAsBinaryString(slice);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the progress bar
|
// Update the progress bar
|
||||||
function updateProgressBar(data) {
|
function updateProgressBar(data) {
|
||||||
if (typeof(data.action) !== 'undefined' && data.action === 'cancel') {
|
if (typeof (data.action) !== 'undefined' && data.action === 'cancel') {
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
console.log('Upload successfully cancelled');
|
console.log('Upload successfully cancelled');
|
||||||
} else {
|
} else {
|
||||||
|
@ -460,14 +472,14 @@ function updateProgressBar(data) {
|
||||||
$('#results').hide();
|
$('#results').hide();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var i = data.i;
|
var i = data.i;
|
||||||
var sent_delay = data.sent_delay;
|
var sent_delay = data.sent_delay;
|
||||||
var del_at_first_view = data.del_at_first_view;
|
var del_at_first_view = data.del_at_first_view;
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
var j = data.j;
|
var j = data.j;
|
||||||
var delay = data.delay;
|
var delay = data.delay;
|
||||||
var parts = data.parts;
|
var parts = data.parts;
|
||||||
var short = data.short;
|
var short = data.short;
|
||||||
var created_at = data.created_at;
|
var created_at = data.created_at;
|
||||||
|
|
||||||
console.log(`getting response for slice ${j + 1}/${parts} of file ${data.name} (${data.duration} sec)`);
|
console.log(`getting response for slice ${j + 1}/${parts} of file ${data.name} (${data.duration} sec)`);
|
||||||
|
@ -476,8 +488,7 @@ function updateProgressBar(data) {
|
||||||
var key = dp.attr('data-key');
|
var key = dp.attr('data-key');
|
||||||
|
|
||||||
if (j + 1 === parts) {
|
if (j + 1 === parts) {
|
||||||
//
|
window.ws.onclose = function () {
|
||||||
window.ws.onclose = function() {
|
|
||||||
console.log('Connection is closed.');
|
console.log('Connection is closed.');
|
||||||
};
|
};
|
||||||
window.ws.onerror = function() {
|
window.ws.onerror = function() {
|
||||||
|
@ -582,8 +593,8 @@ function updateProgressBar(data) {
|
||||||
} else {
|
} else {
|
||||||
j++;
|
j++;
|
||||||
// Update progress bar
|
// Update progress bar
|
||||||
var percent = Math.round(1000 * j/parts)/10;
|
var percent = Math.round(1000 * j / parts) / 10;
|
||||||
var wClass = percent.toString().replace('.', '-');
|
var wClass = percent.toString().replace('.', '-');
|
||||||
dp.removeClass();
|
dp.removeClass();
|
||||||
dp.addClass('determinate');
|
dp.addClass('determinate');
|
||||||
dp.addClass(`width-${wClass}`);
|
dp.addClass(`width-${wClass}`);
|
||||||
|
@ -649,20 +660,20 @@ function spawnWebsocket(i, callback) {
|
||||||
if (i === undefined || i === null) {
|
if (i === undefined || i === null) {
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
var ws = new WebSocket(ws_url);
|
var ws = new WebSocket(ws_url);
|
||||||
ws.onopen = function() {
|
ws.onopen = function () {
|
||||||
console.log('Connection is established!');
|
console.log('Connection is established!');
|
||||||
if (callback !== undefined) {
|
if (callback !== undefined) {
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ws.onclose = function() {
|
ws.onclose = function () {
|
||||||
console.log('Connection is closed.');
|
console.log('Connection is closed.');
|
||||||
}
|
}
|
||||||
ws.onmessage = function(e) {
|
ws.onmessage = function (e) {
|
||||||
updateProgressBar(JSON.parse(e.data));
|
updateProgressBar(JSON.parse(e.data));
|
||||||
}
|
}
|
||||||
ws.onerror = function() {
|
ws.onerror = function () {
|
||||||
console.log('error');
|
console.log('error');
|
||||||
if (i < 5 && callback !== undefined) {
|
if (i < 5 && callback !== undefined) {
|
||||||
console.log(`Retrying to send file (try ${i} of 5)`);
|
console.log(`Retrying to send file (try ${i} of 5)`);
|
||||||
|
@ -680,29 +691,17 @@ function bindDropZone() {
|
||||||
$('#file-browser-span').removeClass('disabled');
|
$('#file-browser-span').removeClass('disabled');
|
||||||
$('#file-browser-span').addClass('cyan');
|
$('#file-browser-span').addClass('cyan');
|
||||||
$('#file-browser-button').attr('disabled', null);
|
$('#file-browser-button').attr('disabled', null);
|
||||||
$('#file-browser-button').on('change', function(e) {
|
$('#file-browser-button').on('change', function (e) {
|
||||||
handleFiles(this.files);
|
handleFiles(this.files);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// When it's ready
|
// When it's ready
|
||||||
$(document).ready(function() {
|
$(document).ready(function () {
|
||||||
$('#zip-files').prop('checked', false);
|
$('#zip-files').prop('checked', false);
|
||||||
$('#first-view').prop('checked', false);
|
$('#first-view').prop('checked', false);
|
||||||
$('#zipname').val('documents.zip');
|
$('#zipname').val('documents.zip');
|
||||||
if (!sjcl.random.isReady(10)) {
|
bindDropZone();
|
||||||
var loop = setInterval(function() {
|
|
||||||
if (!sjcl.random.isReady(10)) {
|
|
||||||
$('#not-enough-entropy').removeClass('hiddendiv');
|
|
||||||
} else {
|
|
||||||
$('#not-enough-entropy').addClass('hiddendiv');
|
|
||||||
bindDropZone();
|
|
||||||
clearInterval(loop);
|
|
||||||
}
|
|
||||||
}, 1000);
|
|
||||||
} else {
|
|
||||||
bindDropZone();
|
|
||||||
}
|
|
||||||
if (maxSize > 0) {
|
if (maxSize > 0) {
|
||||||
$('#max-file-size').text(i18n.maxSize.replace('XXX', filesize(maxSize)));
|
$('#max-file-size').text(i18n.maxSize.replace('XXX', filesize(maxSize)));
|
||||||
}
|
}
|
||||||
|
@ -710,7 +709,7 @@ $(document).ready(function() {
|
||||||
$('label[for="zip-files"]').on('click', zipClicking);
|
$('label[for="zip-files"]').on('click', zipClicking);
|
||||||
$('#zipname').on('input', updateZipname);
|
$('#zipname').on('input', updateZipname);
|
||||||
$('#uploadZip').on('click', uploadZip);
|
$('#uploadZip').on('click', uploadZip);
|
||||||
$('#reset-zipping').on('click', function() {
|
$('#reset-zipping').on('click', function () {
|
||||||
window.zip = null;
|
window.zip = null;
|
||||||
$('label[for="zip-files"]').click();
|
$('label[for="zip-files"]').click();
|
||||||
$('#zip-files').attr('disabled', null);
|
$('#zip-files').attr('disabled', null);
|
||||||
|
|
|
@ -165,5 +165,5 @@
|
||||||
%= javascript '/js/filesize.min.js'
|
%= javascript '/js/filesize.min.js'
|
||||||
%= javascript '/js/jszip.min.js'
|
%= javascript '/js/jszip.min.js'
|
||||||
%= javascript '/js/lufi-notifications.js'
|
%= javascript '/js/lufi-notifications.js'
|
||||||
%= javascript '/js/lufi-up.js'
|
|
||||||
% }
|
% }
|
||||||
|
<script type="module" src="/js/lufi-up.js"></script>
|
||||||
|
|
|
@ -46,8 +46,8 @@
|
||||||
%= javascript '/js/sjcl.js'
|
%= javascript '/js/sjcl.js'
|
||||||
%= javascript '/js/jszip.min.js'
|
%= javascript '/js/jszip.min.js'
|
||||||
%= javascript '/js/lufi-notifications.js'
|
%= javascript '/js/lufi-notifications.js'
|
||||||
%= javascript '/js/lufi-down.js'
|
|
||||||
% }
|
% }
|
||||||
% }
|
% }
|
||||||
|
<script type="module" src="/js/lufi-down.js"></script>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue