Luc Didry 91e078
// vim:set sw=4 ts=4 sts=4 ft=javascript expandtab:
Luc Didry 91e078
Luc Didry d9928e
// total file counter
Luc Didry be8c84
window.fc = 0;
Luc Didry d9928e
// Cancelled files indexes
Luc Didry d9928e
window.cancelled = [];
Luc Didry 1923e9
// Set websocket
Luc Didry 1923e9
window.ws = spawnWebsocket(0, function() {return null;});
Luc Didry 409704
// Use slice of 0.75MB
Luc Didry 409704
window.sliceLength = 750000;
Luc Didry d9928e
// Global zip objects for currently created zip file
Luc Didry d9928e
window.zip = null;
Luc Didry d9928e
window.zipSize = 0;
Luc Didry 8b68d7
// Init the list of files (used by LDAP invitation feature)
Luc Didry 8b68d7
window.filesURLs = [];
Luc Didry 1923e9
Luc Didry be8c84
// Copy a link to clipboard
Luc Didry d3222b
function copyToClipboard(txt) {
Luc Didry d3222b
    var textArea = $('<textarea>');
Luc Didry d3222b
    textArea.addClass('textarea-hidden');
Luc Didry d3222b
    textArea.val(txt);
Luc Didry d3222b
Luc Didry d3222b
    $('body').append(textArea);
Luc Didry 91e078
    textArea.select();
Luc Didry 91e078
Luc Didry 91e078
    try {
Luc Didry 91e078
        var successful = document.execCommand('copy');
Luc Didry d3222b
        var msg = successful ? i18n.copySuccess : i18n.copyFail;
Luc Didry d3222b
        Materialize.toast(msg, 4000);
Luc Didry 91e078
    } catch (err) {
Luc Didry 91e078
        el.focus();
Luc Didry 91e078
        var len = el.value.length * 2;
Luc Didry 91e078
        el.setSelectionRange(0, len);
Luc Didry 5237e7
        alert(i18n.hit);
Luc Didry 91e078
    }
Luc Didry 91e078
Luc Didry d3222b
    textArea.remove();
Luc Didry 91e078
}
Luc Didry be8c84
Luc Didry be8c84
// Copy all links to clipboard
Luc Didry 377ba0
function copyAllToClipboard(event) {
Luc Didry 377ba0
    event.preventDefault();
Luc Didry 91e078
    var text = new Array();
Luc Didry d3222b
    var a = $('.link-input');
Luc Didry ae8934
    var i;
Luc Didry ae8934
    for (i = 0; i < a.length; i++) {
Luc Didry ae8934
        text.push(a[i].value);
Luc Didry ae8934
    }
Luc Didry d3222b
    var textArea = $('<textarea>');
Luc Didry d3222b
    textArea.addClass('textarea-hidden');
Luc Didry d3222b
    textArea.val(text.join("\n"));
Luc Didry d3222b
Luc Didry d3222b
    $('body').append(textArea);
Luc Didry 91e078
    textArea.select();
Luc Didry 91e078
Luc Didry 91e078
    try {
Luc Didry 91e078
        var successful = document.execCommand('copy');
Luc Didry d3222b
        var msg = successful ? i18n.copySuccess : i18n.copyFail;
Luc Didry d3222b
        Materialize.toast(msg, 4000);
Luc Didry 91e078
    } catch (err) {
Luc Didry 377ba0
        textArea.removeClass('textarea-hidden');
Luc Didry 377ba0
        textArea.addClass('white-background');
Luc Didry 5237e7
        alert(i18n.hits);
Luc Didry 91e078
    }
Luc Didry 91e078
Luc Didry d3222b
    textArea.remove();
Luc Didry 91e078
}
Luc Didry be8c84
Luc Didry be8c84
// Add item to localStorage
Luc Didry 91e078
function addItem(name, url, size, del_at_first_view, created_at, delay, short, token) {
Luc Didry 8d85cd
    var files = localStorage.getItem(window.prefix + 'files');
Luc Didry 91e078
    if (files === null) {
Luc Didry 91e078
        files = new Array();
Luc Didry 91e078
    } else {
Luc Didry 91e078
        files = JSON.parse(files);
Luc Didry 91e078
    }
Luc Didry 91e078
    files.push({ name: name, short: short, url: url, size: size, del_at_first_view: del_at_first_view, created_at: created_at, delay: delay, token: token });
Luc Didry 8d85cd
    localStorage.setItem(window.prefix + 'files', JSON.stringify(files));
Luc Didry 91e078
}
Luc Didry 91e078
Luc Didry 7585be
// Remove a file block
Luc Didry 7585be
function destroyBlock(el) {
Luc Didry d3222b
    $(el).parents('li').remove();
Luc Didry 7585be
Luc Didry d1627e
    if ($('.link-input').length === 0) {
Luc Didry d3222b
        $('#misc').empty();
Luc Didry d1627e
        if ($('#results li').length === 0 && window.fileList === null) {
Luc Didry d3222b
            $('#results').hide();
Luc Didry 40deab
        }
Luc Didry 7585be
    } else {
Luc Didry 7585be
        updateMailLink();
Luc Didry 7585be
    }
Luc Didry 7585be
}
Luc Didry 7585be
Luc Didry d9928e
// When clicking on del at first view checkbox
Luc Didry d9928e
function firstViewClicking() {
Luc Didry d9928e
    if ($('#first-view').attr('data-checked') && $('#first-view').attr('data-checked') === 'data-checked') {
Luc Didry d9928e
        $('#first-view').attr('data-checked', null);
Luc Didry d9928e
    } else {
Luc Didry d9928e
        $('#first-view').attr('data-checked', 'data-checked');
Luc Didry d9928e
    }
Luc Didry d9928e
}
Luc Didry d9928e
Luc Didry d9928e
// When clicking on zip checkbox
Luc Didry d9928e
function zipClicking () {
Luc Didry d9928e
    if ($('#zip-files').attr('data-checked') && $('#zip-files').attr('data-checked') === 'data-checked') {
Luc Didry d9928e
        window.zipSize = 0;
Luc Didry d9928e
        window.zip = null;
Luc Didry d9928e
        $('#zip-files').attr('data-checked', null);
Luc Didry d9928e
        $('#zipname').val('documents.zip');
Luc Didry d9928e
        $('#zipname-input').addClass('hide');
Luc Didry d9928e
        $('#zipping').addClass('hide');
Luc Didry 76ce6f
        $('#files').removeClass('m6').addClass('m12');
Luc Didry d9928e
        $('#zip-parts').html('');
Luc Didry d9928e
        $('#delete-day').attr('disabled', null);
Luc Didry d9928e
        $('#first-view').attr('disabled', null);
Luc Didry d9928e
    } else {
Luc Didry d9928e
        $('#zip-files').attr('data-checked', 'data-checked');
Luc Didry d9928e
        $('#zipname-input').removeClass('hide');
Luc Didry d9928e
        $('#zip-size').text(filesize(window.zipSize));
Luc Didry d9928e
    }
Luc Didry d9928e
}
Luc Didry d9928e
Luc Didry d9928e
// Get the zip file name
Luc Didry d9928e
function getZipname() {
Luc Didry d9928e
    var zipname = $('#zipname').val();
Luc Didry d9928e
    if (zipname === '') {
Luc Didry d9928e
        zipname = 'documents.zip';
Luc Didry d9928e
    }
Luc Didry d9928e
    if (!zipname.endsWith('.zip')) {
Luc Didry d9928e
        if (zipname.endsWith('.')) {
Luc Didry d9928e
            zipname += 'zip';
Luc Didry d9928e
        } else {
Luc Didry d9928e
            zipname += '.zip';
Luc Didry d9928e
        }
Luc Didry d9928e
    }
Luc Didry d9928e
Luc Didry d1627e
    return escapeHtml(zipname);
Luc Didry d9928e
}
Luc Didry d9928e
Luc Didry d9928e
// Update the zip name
Luc Didry d9928e
function updateZipname() {
Luc Didry d9928e
    $('#zip-name').text(getZipname());
Luc Didry d9928e
}
Luc Didry d9928e
Luc Didry d9928e
// Create blob from zip
Luc Didry d9928e
function uploadZip(e) {
Luc Didry d9928e
    e.preventDefault();
Luc Didry d9928e
    var delay             = $('#delete-day');
Luc Didry d9928e
    var del_at_first_view = $('#first-view');
Luc Didry d9928e
    $('#zip-files').attr('disabled', 'disabled');
Luc Didry d1627e
    $('#file-browser-button').attr('disabled', 'disabled');
Luc Didry d1627e
    $('#file-browser-span').addClass('disabled');
Luc Didry d1627e
    $('#uploadZip').addClass('hide');
Luc Didry d1627e
    $('#zip-parts').text('');
Luc Didry d9928e
Luc Didry d9928e
    $('#zip-compressing').removeClass('hide');
Luc Didry d9928e
    window.zip.generateAsync({type:"blob"})
Luc Didry d9928e
        .then(function(zipFile) {
Luc Didry d9928e
            // if $('#zipping') is hidden, the zipping has been aborted
Luc Didry d9928e
            if (!$('#zipping').hasClass('hide')) {
Luc Didry d1627e
                window.zip = null;
Luc Didry d9928e
                $('#zipping').addClass('hide');
Luc Didry 76ce6f
                $('#files').removeClass('m6').addClass('m12');
Luc Didry d9928e
                $('#zipname-input').addClass('hide');
Luc Didry d9928e
                $('#zip-compressing').addClass('hide');
Luc Didry d1627e
                $('#uploadZip').removeClass('hide');
Luc Didry d9928e
                $('#results').show();
Luc Didry d1627e
                $('#zip-files').attr('disabled', null);
Luc Didry d9928e
Luc Didry d9928e
                var zipname = getZipname();
Luc Didry d9928e
                var file = new File([zipFile], zipname, {type: 'application/zip'});
Luc Didry d9928e
Luc Didry d1627e
                Materialize.toast(i18n.enqueued.replace('XXX', zipname), 3000, 'teal accent-3');
Luc Didry d9928e
                if (window.fileList === undefined || window.fileList === null) {
Luc Didry d9928e
                    window.fileList = [file];
Luc Didry d9928e
                    uploadFile(0, delay.val(), del_at_first_view.is(':checked'));
Luc Didry d9928e
                } else {
Luc Didry d9928e
                    window.fileList.push(file);
Luc Didry d9928e
                }
Luc Didry d9928e
            }
Luc Didry d1627e
            $('#file-browser-button').attr('disabled', null);
Luc Didry d1627e
            $('#file-browser-span').removeClass('disabled');
Luc Didry d9928e
        });
Luc Didry d9928e
}
Luc Didry d9928e
Luc Didry 7585be
// Update the mail link
Luc Didry 7585be
function updateMailLink() {
Luc Didry d3222b
    var a = $('.link-input');
Luc Didry 7585be
    var l = new Array();
Luc Didry 7585be
    var i;
Luc Didry 7585be
    for (i = 0; i < a.length; i++) {
Luc Didry 7585be
        l.push(a[i].id);
Luc Didry 7585be
    }
Luc Didry a2a578
    var u = actionURL+'m?links='+JSON.stringify(l);
Luc Didry d3222b
    $('#mailto').attr('href', u);
Luc Didry 7585be
}
Luc Didry 7585be
Luc Didry 8b68d7
// [Invitation feature] Send URLs of files to server
Luc Didry 8b68d7
function sendFilesURLs() {
Luc Didry 8b68d7
    if (window.filesURLs.length > 0) {
Luc Didry 8b68d7
        $.ajax({
Luc Didry 8b68d7
            url: sendFilesURLsURL,
Luc Didry 8b68d7
            method: 'POST',
Luc Didry 8b68d7
            dataType: 'json',
Luc Didry 8b68d7
            data: {
Luc Didry 8b68d7
                urls: window.filesURLs
Luc Didry 8b68d7
            },
Luc Didry 8b68d7
            success: function(data, textStatus, jqXHR) {
Luc Didry 8b68d7
                if (data.success) {
Luc Didry 8b68d7
                    Materialize.toast(data.msg, 6000, 'teal accent-3');
Luc Didry 8b68d7
                } else {
Luc Didry 8b68d7
                    Materialize.toast(data.msg, 10000, 'red accent-2');
Luc Didry 8b68d7
                }
Luc Didry 8b68d7
            }
Luc Didry 8b68d7
        });
Luc Didry 8b68d7
    }
Luc Didry 8b68d7
}
Luc Didry 8b68d7
Luc Didry 8b68d7
Luc Didry 91e078
// Start uploading the files (called from <input> and from drop zone)
Luc Didry 91e078
function handleFiles(f) {
Luc Didry d3222b
    var delay             = $('#delete-day');
Luc Didry cf7993
    var zip_files         = $('#zip-files');
Luc Didry d3222b
    var del_at_first_view = $('#first-view');
Luc Didry cf7993
Luc Didry d3222b
    delay.attr('disabled', 'disabled');
Luc Didry d3222b
    del_at_first_view.attr('disabled', 'disabled');
Luc Didry 91e078
Luc Didry cf7993
    if (zip_files.is(':checked')) {
Luc Didry d9928e
        if (window.zip === null) {
Luc Didry d9928e
            window.zip = new JSZip();
Luc Didry cf7993
        }
Luc Didry d9928e
        $('#zipping').removeClass('hide');
Luc Didry 76ce6f
        $('#files').removeClass('m12').addClass('m6');
Luc Didry d9928e
        for (var i = 0; i < f.length; i++) {
Luc Didry d9928e
            var element  = f.item(i);
Luc Didry d9928e
            var filename = element.name;
Luc Didry d9928e
            var origname = filename;
Luc Didry d9928e
            var counter  = 0;
Luc Didry d9928e
            while (typeof(window.zip.files[filename]) !== 'undefined') {
Luc Didry d9928e
                counter += 1;
Luc Didry d9928e
                filename = origname.substring(0, origname.lastIndexOf('.')) + '_(' + counter + ')' + origname.substring(origname.lastIndexOf('.'));
Luc Didry d9928e
            }
Luc Didry cf7993
Luc Didry d9928e
            window.zip.file(filename, element);
Luc Didry d9928e
Luc Didry d9928e
            window.zipSize += element.size;
Luc Didry d9928e
            $('#zip-size').text(filesize(window.zipSize));
Luc Didry d9928e
            $('#zip-parts').append([
Luc Didry d9928e
                '
  • ',
  • Luc Didry d9928e
                        '— ', filename, ' (', filesize(element.size), ')',
    Luc Didry d9928e
                    ''
    Luc Didry d9928e
                ].join(''));
    Luc Didry d9928e
            }
    Luc Didry cf7993
        } else {
    Luc Didry cf7993
            if (window.fileList === undefined || window.fileList === null) {
    Luc Didry cf7993
                window.fileList = Array.prototype.slice.call(f);
    Luc Didry d1627e
                for (var i = 0; i < window.fileList.length; i++) {
    Luc Didry d1627e
                    var file = window.fileList[i];
    Luc Didry d1627e
                    Materialize.toast(i18n.enqueued.replace('XXX', escapeHtml(file.name)), 3000, 'teal accent-3');
    Luc Didry d1627e
                }
    Luc Didry 8b68d7
                window.nbFiles  = window.fileList.length;
    Luc Didry cf7993
                $('#results').show();
    Luc Didry cf7993
                uploadFile(0, delay.val(), del_at_first_view.is(':checked'));
    Luc Didry cf7993
            } else {
    Luc Didry cf7993
                window.fileList = window.fileList.concat(Array.prototype.slice.call(f));
    Luc Didry cf7993
            }
    Luc Didry 8ebaf9
        }
    Luc Didry 91e078
    }
    Luc Didry 91e078
    Luc Didry e9bff8
    // Create random key
    Luc Didry e9bff8
    function genRandomKey() {
    Luc Didry e9bff8
        return sjcl.codec.base64.fromBits(sjcl.random.randomWords(8, 10), 0);
    Luc Didry e9bff8
    }
    Luc Didry e9bff8
    Luc Didry 91e078
    // Create progress bar and call slicing and uploading function
    Luc Didry 91e078
    function uploadFile(i, delay, del_at_first_view) {
    Luc Didry 50aa76
        // Prevent exiting page before full upload
    Luc Didry 6eb504
        window.onbeforeunload = confirmExit;
    Luc Didry 91e078
    Luc Didry 91e078
        // Create a random key, different for all files
    Luc Didry e9bff8
        var randomkey = genRandomKey();
    Luc Didry 91e078
    Luc Didry 91e078
        // Get the file and properties
    Luc Didry 8ebaf9
        var file  = window.fileList[i];
    Luc Didry d94c2b
        var name  = escapeHtml(file.name);
    Luc Didry ed472d
        var size  = filesize(file.size);
    Luc Didry 91e078
        var parts = Math.ceil(file.size/window.sliceLength);
    Luc Didry 1d7d9e
        if (parts === 0) {
    Luc Didry 1d7d9e
            parts = 1;
    Luc Didry 1d7d9e
        }
    Luc Didry 91e078
    Luc Didry 91e078
        // Create a progress bar for the file
    Luc Didry d3222b
        var r  = $('#ul-results');
    Luc Didry d3222b
        var w  = $('
  • ');
  • Luc Didry d3222b
        w.addClass('list-group-item');
    Luc Didry d3222b
        w.html(['
    ',
    Luc Didry d3222b
                    '
    ',
    Luc Didry 377ba0
                        '',
    Luc Didry d3222b
                            '',
    Luc Didry d3222b
                        '',
    Luc Didry d3222b
                        '
    ',
    Luc Didry ed472d
                            '', name, ' (', size,')',
    Luc Didry d3222b
                            '

    ',
    Luc Didry d3222b
                        '',
    Luc Didry d3222b
                        '
    ',
    Luc Didry 377ba0
                            '
    ',
    Luc Didry d94c2b
                                '', name, '0%',
    Luc Didry d3222b
                            '',
    Luc Didry d3222b
                        '',
    Luc Didry d3222b
                '
    '].join(''));
    Luc Didry cc012f
        r.prepend(w);
    Luc Didry 377ba0
        $('#destroy-'+window.fc).on('click', function(event) {
    Luc Didry 377ba0
            event.preventDefault();
    Luc Didry d9928e
            window.cancelled.push(i);
    Luc Didry d9928e
            destroyBlock(this);
    Luc Didry 377ba0
        });
    Luc Didry 91e078
    Luc Didry d9928e
        sliceAndUpload(randomkey, i, parts, 0, delay, del_at_first_view, null, null);
    Luc Didry 91e078
    }
    Luc Didry 91e078
    Luc Didry 91e078
    // Get a slice of file and send it
    Luc Didry d9928e
    function sliceAndUpload(randomkey, i, parts, j, delay, del_at_first_view, short, mod_token) {
    Luc Didry d9928e
        if (mod_token !== null && window.cancelled.includes(i)) {
    Luc Didry d9928e
            var data = JSON.stringify({
    Luc Didry 91e078
                id: short,
    Luc Didry d9928e
                mod_token: mod_token,
    Luc Didry d9928e
                cancel: true,
    Luc Didry 91e078
                i: i
    Luc Didry d9928e
            })+'XXMOJOXXuseless';
    Luc Didry 5237e7
            // Verify that we have a websocket and send json
    Luc Didry 91e078
            if (window.ws.readyState === 3) {
    Luc Didry a594b9
                window.ws = spawnWebsocket(0, function() {
    Luc Didry d9928e
                    window.ws.send(data);
    Luc Didry 91e078
                });
    Luc Didry 91e078
            } else {
    Luc Didry 0c5602
                window.ws.onclose = function() {
    Luc Didry 0c5602
                    console.log('Websocket closed, waiting 10sec.');
    Luc Didry d9928e
                    window.ws = spawnWebsocket(0, function() {return null;});
    Luc Didry 0c5602
                };
    Luc Didry a594b9
                window.ws.onerror = function() {
    Luc Didry a594b9
                    console.log('Error on Websocket, waiting 10sec.');
    Luc Didry d9928e
                    window.ws = spawnWebsocket(0, function() {return null;});
    Luc Didry d9928e
                };
    Luc Didry d9928e
                window.ws.send(data);
    Luc Didry d9928e
            }
    Luc Didry d9928e
        } else {
    Luc Didry d9928e
            var file  = window.fileList[i];
    Luc Didry d9928e
            var slice = file.slice(j * window.sliceLength, (j + 1) * window.sliceLength, file.type);
    Luc Didry d9928e
            var fr = new FileReader();
    Luc Didry d9928e
            fr.onloadend = function() {
    Luc Didry d9928e
                var sl        = $('#parts-'+window.fc);
    Luc Didry d9928e
    Luc Didry d9928e
                // Get the binary result, different result in IE browsers (see default.html.ep line 27:48)
    Luc Didry d9928e
                if (isIE == true){
    Luc Didry d9928e
                    var bin = fr.content;
    Luc Didry d9928e
                } else {
    Luc Didry d9928e
                    var bin = fr.result;
    Luc Didry d9928e
                }
    Luc Didry d9928e
    Luc Didry d9928e
                // Transform it in base64
    Luc Didry d9928e
                var b         = window.btoa(bin);
    Luc Didry d9928e
    Luc Didry d9928e
                // Encrypt it
    Luc Didry d9928e
                var encrypted = sjcl.encrypt(randomkey, b);
    Luc Didry d9928e
    Luc Didry d9928e
                // Prepare json
    Luc Didry d9928e
                var data = {
    Luc Didry d9928e
                    // number of parts
    Luc Didry d9928e
                    total: parts,
    Luc Didry d9928e
                    // part X of total
    Luc Didry d9928e
                    part: j,
    Luc Didry d9928e
                    size: file.size,
    Luc Didry d9928e
                    name: file.name,
    Luc Didry d9928e
                    type: file.type,
    Luc Didry d9928e
                    delay: delay,
    Luc Didry d9928e
                    del_at_first_view: del_at_first_view,
    Luc Didry d9928e
                    zipped: $('#zip-files').is(':checked'),
    Luc Didry d9928e
                    id: short,
    Luc Didry d9928e
                    // number of the sent file in the queue
    Luc Didry d9928e
                    i: i
    Luc Didry d9928e
                };
    Luc Didry d9928e
                if ($('#file_pwd').length === 1) {
    Luc Didry d9928e
                    var pwd = $('#file_pwd').val();
    Luc Didry d9928e
                    if (pwd !== undefined && pwd !== null && pwd !== '') {
    Luc Didry d9928e
                        data['file_pwd'] = $('#file_pwd').val();
    Luc Didry d9928e
                    }
    Luc Didry d9928e
                }
    Luc Didry d9928e
                data = JSON.stringify(data)+'XXMOJOXX'+JSON.stringify(encrypted);;
    Luc Didry d9928e
    Luc Didry 409704
                var percent = Math.round(1000 * j/parts)/10;
    Luc Didry 409704
                console.log('sending slice '+(j + 1)+'/'+parts+' of file '+file.name+' ('+percent+'%)');
    Luc Didry d9928e
    Luc Didry 409704
                sl.html(percent.toFixed(1)+'%');
    Luc Didry d9928e
    Luc Didry d9928e
                // Verify that we have a websocket and send json
    Luc Didry d9928e
                if (window.ws.readyState === 3) {
    Luc Didry 1119e7
                    window.ws = spawnWebsocket(0, function() {
    Luc Didry d9928e
                        window.ws.send(data);
    Luc Didry 1119e7
                    });
    Luc Didry d9928e
                } else {
    Luc Didry d9928e
                    window.ws.onclose = function() {
    Luc Didry d9928e
                        console.log('Websocket closed, waiting 10sec.');
    Luc Didry d9928e
                        window.ws = spawnWebsocket(0, function() {
    Luc Didry d9928e
                            console.log('sending again slice '+(j + 1)+'/'+parts+' of file '+file.name);
    Luc Didry d9928e
                            window.ws.send(data);
    Luc Didry d9928e
                        });
    Luc Didry d9928e
                    };
    Luc Didry d9928e
                    window.ws.onerror = function() {
    Luc Didry d9928e
                        console.log('Error on Websocket, waiting 10sec.');
    Luc Didry d9928e
                        window.ws = spawnWebsocket(0, function() {
    Luc Didry d9928e
                            console.log('sending again slice '+(j + 1)+'/'+parts+' of file '+file.name);
    Luc Didry d9928e
                            window.ws.send(data);
    Luc Didry d9928e
                        });
    Luc Didry d9928e
                    };
    Luc Didry d9928e
                    window.ws.send(data);
    Luc Didry d9928e
                }
    Luc Didry 91e078
            }
    Luc Didry d9928e
            fr.readAsBinaryString(slice);
    Luc Didry 91e078
        }
    Luc Didry 91e078
    }
    Luc Didry 91e078
    Luc Didry 91e078
    // Update the progress bar
    Luc Didry 91e078
    function updateProgressBar(data) {
    Luc Didry d9928e
        if (typeof(data.action) !== 'undefined' && data.action === 'cancel') {
    Luc Didry d9928e
            if (data.success) {
    Luc Didry d9928e
                console.log('Upload successfully cancelled');
    Luc Didry d9928e
            } else {
    Luc Didry d9928e
                console.log('Upload cancellation failed: ' + data.msg);
    Luc Didry d9928e
            }
    Luc Didry d9928e
    Luc Didry d9928e
            // Remove the cancelled index
    Luc Didry d9928e
            window.cancelled.splice(window.cancelled.indexOf(window.fc), 1);
    Luc Didry d9928e
    Luc Didry d9928e
            // Upload next file
    Luc Didry d9928e
            window.fc++;
    Luc Didry d9928e
            data.i++;
    Luc Didry d9928e
            if (data.i < window.fileList.length) {
    Luc Didry d9928e
                uploadFile(data.i, $('#delete-day').val(), $('#first-view').is(':checked'));
    Luc Didry d9928e
            } else {
    Luc Didry d9928e
                // We have finished
    Luc Didry d9928e
                window.cancelled = [];
    Luc Didry d9928e
                window.fileList = null;
    Luc Didry d9928e
                window.onbeforeunload = null;
    Luc Didry d9928e
                $('#delete-day').attr('disabled', null);
    Luc Didry d9928e
                $('#first-view').attr('disabled', null);
    Luc Didry d1627e
                if ($('#zip-files').is(':checked') && window.zip === null) {
    Luc Didry d9928e
                    $('label[for="zip-files"]').click();
    Luc Didry 0c5602
                }
    Luc Didry d9928e
            }
    Luc Didry d1627e
            if ($('#results li').length === 0 && window.fileList === null) {
    Luc Didry d1627e
                $('#results').hide();
    Luc Didry d1627e
            }
    Luc Didry d9928e
        } else {
    Luc Didry d9928e
            var i                 = data.i;
    Luc Didry d9928e
            var sent_delay        = data.sent_delay;
    Luc Didry d9928e
            var del_at_first_view = data.del_at_first_view;
    Luc Didry d9928e
            if (data.success) {
    Luc Didry d9928e
                var j          = data.j;
    Luc Didry d9928e
                var delay      = data.delay;
    Luc Didry d9928e
                var parts      = data.parts;
    Luc Didry d9928e
                var short      = data.short;
    Luc Didry d9928e
                var created_at = data.created_at;
    Luc Didry d9928e
    Luc Didry d9928e
                console.log('getting response for slice '+(j + 1)+'/'+parts+' of file '+data.name+' ('+data.duration+' sec)');
    Luc Didry d9928e
    Luc Didry d9928e
                var dp    = $('#progress-'+window.fc);
    Luc Didry d9928e
                var key   = dp.attr('data-key');
    Luc Didry d9928e
    Luc Didry d9928e
                if (j + 1 === parts) {
    Luc Didry d9928e
                    //
    Luc Didry d9928e
                    window.ws.onclose = function() {
    Luc Didry d9928e
                        console.log('Connection is closed.');
    Luc Didry d9928e
                    };
    Luc Didry d9928e
                    window.ws.onerror = function() {
    Luc Didry d9928e
                        console.log('Error on WebSocket connection but file has been fully send, so we don\'t care.');
    Luc Didry d9928e
                    }
    Luc Didry d9928e
    Nicolas Constant b2c4a7
                    notify(i18n.fileUploaded, data.name);
    Nicolas Constant 538bf7
    Luc Didry d9928e
                    $('#parts-'+window.fc).remove();
    Luc Didry d9928e
                    var n       = $('#name-'+window.fc);
    Luc Didry d9928e
                    var s       = $('#size-'+window.fc);
    Luc Didry d9928e
                    var d       = $('
    ');
    Luc Didry d9928e
                    var url     = baseURL+'r/'+short+'#'+key;
    Luc Didry d9928e
                    var del_url = actionURL+'d/'+short+'/'+data.token;
    Luc Didry d9928e
                    var links   = encodeURIComponent('["'+short+'"]');
    Luc Didry d9928e
                    var limit   = (delay === 0) ? i18n.noLimit : i18n.expiration+' '+moment.unix(delay * 86400 + created_at).locale(window.navigator.language).format('LLLL');
    Luc Didry 0b3476
                    if (!isGuest) {
    Luc Didry 41c656
                        n.html(n.html()+' '+s.html()+' 
    '+limit);
    Luc Didry 0b3476
                        d.html(['
    ',
    Luc Didry 0b3476
                                    '
    ',
    Luc Didry 0b3476
                                        '',
    Luc Didry 0b3476
                                            '',
    Luc Didry 0b3476
                                                '',
    Luc Didry 0b3476
                                            '',
    Luc Didry 0b3476
                                            '',
    Luc Didry 0b3476
                                                '',
    Luc Didry 0b3476
                                            '',
    Luc Didry 0b3476
                                        '',
    Luc Didry 0b3476
                                        '<input id="', short, '" class="form-control link-input white-background" value="', url, '" readonly="" type="text">',
    Luc Didry 0b3476
                                        '<label class="active" for="', short, '">', i18n.dlText, '</label>',
    Luc Didry 0b3476
                                    '',
    Luc Didry 0b3476
                                    '
    ',
    Luc Didry 0b3476
                                        '',
    Luc Didry 0b3476
                                            '',
    Luc Didry d9928e
                                        '',
    Luc Didry 0b3476
                                        '<input id="delete-', short, '" class="form-control white-background" value="', del_url, '" readonly="" type="text">',
    Luc Didry 0b3476
                                        '<label class="active" for="delete-', short, '">', i18n.delText, '</label>',
    Luc Didry 0b3476
                                    '',
    Luc Didry 0b3476
                                ''].join(''));
    Luc Didry 0b3476
                    } else {
    Luc Didry 0b3476
                        n.html(n.html()+' '+s.html());
    Luc Didry 0b3476
                    }
    Luc Didry d9928e
                    s.remove();
    Luc Didry d3222b
    Luc Didry d9928e
                    var p2 = dp.parent();
    Luc Didry d9928e
                    var p1 = p2.parent();
    Luc Didry 10a8dd
    Luc Didry d9928e
                    p2.remove();
    Luc Didry d9928e
                    p1.append(d);
    Luc Didry 10a8dd
    Luc Didry d9928e
                    $('#copyurl-'+window.fc).on('click', function(e) {
    Luc Didry d9928e
                        e.preventDefault();
    Luc Didry d9928e
                        copyToClipboard(url);
    Luc Didry d9928e
                    });
    Luc Didry d9928e
                    $("input[type='text']").on("click", function () {
    Luc Didry d9928e
                        $(this).select();
    Luc Didry d9928e
                    });
    Luc Didry d9928e
                    // Add copy all and mailto buttons
    Luc Didry d9928e
                    var misc = $('#misc');
    Luc Didry 41c656
                    if (misc.html() === '' && !isGuest) {
    Luc Didry d9928e
                        misc.html(''+i18n.copyAll+' '+i18n.mailTo+'');
    Luc Didry d9928e
                        $('#copyall').on('click', copyAllToClipboard);
    Luc Didry d9928e
                    } else {
    Luc Didry d9928e
                        updateMailLink();
    Luc Didry d9928e
                    }
    Luc Didry 10a8dd
    Luc Didry d9928e
                    // Add the file to localStorage
    Luc Didry 0b3476
                    if (!isGuest) {
    Luc Didry 0b3476
                        addItem(data.name, url, data.size, del_at_first_view, created_at, delay, data.short, data.token);
    Luc Didry 0b3476
                    }
    Luc Didry 10a8dd
    Luc Didry 8b68d7
                    if (isGuest && short !== null) {
    Luc Didry 8b68d7
                        window.filesURLs.push(JSON.stringify({ name: data.name, short: data.short, url: url, size: data.size, created_at: created_at, delay: delay, token: data.token }));
    Luc Didry 8b68d7
                    }
    Luc Didry 8b68d7
    Luc Didry d9928e
                    // Upload next file
    Luc Didry d9928e
                    window.fc++;
    Luc Didry d9928e
                    i++;
    Luc Didry d9928e
                    if (i < window.fileList.length) {
    Luc Didry d9928e
                        uploadFile(i, sent_delay, del_at_first_view);
    Luc Didry d9928e
                    } else {
    Luc Didry d9928e
                        // We have finished
    Luc Didry d9928e
                        window.fileList = null;
    Luc Didry d9928e
                        window.onbeforeunload = null;
    Luc Didry d9928e
                        $('#delete-day').attr('disabled', null);
    Luc Didry d9928e
                        $('#first-view').attr('disabled', null);
    Luc Didry d1627e
                        if ($('#zip-files').is(':checked') && window.zip === null) {
    Luc Didry d9928e
                            $('label[for="zip-files"]').click();
    Luc Didry d9928e
                        }
    Luc Didry 8b68d7
                        if (isGuest) {
    Luc Didry 8b68d7
                            sendFilesURLs();
    Luc Didry 8b68d7
                        }
    Luc Didry d9928e
                    }
    Luc Didry d1627e
                    if ($('#results li').length === 0 && window.fileList === null) {
    Luc Didry d1627e
                        $('#results').hide();
    Luc Didry d1627e
                    }
    Luc Didry 10a8dd
                } else {
    Luc Didry d9928e
                    j++;
    Luc Didry d9928e
                    // Update progress bar
    Luc Didry 409704
                    var percent = Math.round(1000 * j/parts)/10;
    Luc Didry 409704
                    var wClass  = percent.toString().replace('.', '-');
    Luc Didry d9928e
                    dp.removeClass();
    Luc Didry d9928e
                    dp.addClass('determinate');
    Luc Didry 409704
                    dp.addClass('width-'+wClass);
    Luc Didry d9928e
                    dp.attr('aria-valuenow', percent);
    Luc Didry d9928e
    Luc Didry d9928e
                    // Encrypt and upload next slice
    Luc Didry d9928e
                    sliceAndUpload(key, i, parts, j, delay, del_at_first_view, short, data.token);
    Luc Didry 10a8dd
                }
    Luc Didry ae8934
            } else {
    Luc Didry d9928e
                addAlertOnFile(data.msg, i, delay, del_at_first_view);
    Luc Didry 8b68d7
                if (isGuest) {
    Luc Didry 8b68d7
                    sendFilesURLs();
    Luc Didry 8b68d7
                }
    Luc Didry ae8934
            }
    Luc Didry a594b9
        }
    Luc Didry a594b9
    }
    Luc Didry a594b9
    Luc Didry a594b9
    Luc Didry a594b9
    Luc Didry a594b9
    // Write message instead in a file block
    Luc Didry a594b9
    function addAlertOnFile(msg, i, sent_delay, del_at_first_view) {
    Luc Didry d3222b
        var n       = $('#name-'+window.fc);
    Luc Didry d3222b
        var p       = $('#progress-'+window.fc);
    Luc Didry d3222b
        var d       = $('
    ');
    Luc Didry a594b9
    Luc Didry d3222b
        p.parent().remove();
    Luc Didry d3222b
        d.addClass('card pink');
    Luc Didry d3222b
        d.html(['
    ',
    Luc Didry d3222b
                    '', msg, '',
    Luc Didry d3222b
                ''].join(''));
    Luc Didry d3222b
        n.parent().append(d);
    Luc Didry a594b9
    Luc Didry a594b9
        // Upload next file
    Luc Didry a594b9
        window.fc++;
    Luc Didry a594b9
        i++;
    Luc Didry 8ebaf9
        if (i < window.fileList.length) {
    Luc Didry a594b9
            uploadFile(i, sent_delay, del_at_first_view);
    Luc Didry a594b9
        } else {
    Luc Didry a594b9
            // We have finished
    Luc Didry a594b9
            window.onbeforeunload = null;
    Luc Didry cf7993
            $('#zip-files').attr('disabled', null);
    Luc Didry d3222b
            $('#delete-day').attr('disabled', null);
    Luc Didry d3222b
            $('#first-view').attr('disabled', null);
    Luc Didry 91e078
        }
    Luc Didry 91e078
    }
    Luc Didry 91e078
    Luc Didry 91e078
    // Dropzone events functions
    Luc Didry 91e078
    function handleDrop(evt) {
    Luc Didry 91e078
        evt.stopPropagation();
    Luc Didry 91e078
        evt.preventDefault();
    Luc Didry 91e078
    Luc Didry 91e078
        var f = evt.dataTransfer.files; // FileList object
    Luc Didry 91e078
        handleFiles(f);
    Luc Didry 91e078
    }
    Luc Didry 91e078
    function handleDragOver(evt) {
    Luc Didry 91e078
        evt.stopPropagation();
    Luc Didry 91e078
        evt.preventDefault();
    Luc Didry 91e078
        evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
    Luc Didry 91e078
    }
    Luc Didry 91e078
    Luc Didry 91e078
    // Spawn websocket
    Luc Didry a594b9
    function spawnWebsocket(i, callback) {
    Luc Didry a594b9
        if (i === undefined || i === null) {
    Luc Didry a594b9
            i = 0;
    Luc Didry a594b9
        }
    Luc Didry 91e078
        var ws       = new WebSocket(ws_url);
    Luc Didry 91e078
        ws.onopen    = function() {
    Luc Didry 91e078
            console.log('Connection is established!');
    Luc Didry 91e078
            if (callback !== undefined) {
    Luc Didry 91e078
                callback();
    Luc Didry 91e078
            }
    Luc Didry 91e078
        };
    Luc Didry 91e078
        ws.onclose   = function() {
    Luc Didry 91e078
            console.log('Connection is closed.');
    Luc Didry 91e078
        }
    Luc Didry 91e078
        ws.onmessage = function(e) {
    Luc Didry 10a8dd
            updateProgressBar(JSON.parse(e.data));
    Luc Didry 91e078
        }
    Luc Didry 91e078
        ws.onerror = function() {
    Luc Didry 1119e7
            console.log('error');
    Luc Didry a594b9
            if (i < 5 && callback !== undefined) {
    Luc Didry cc5f4d
                console.log('Retrying to send file (try '+i+' of 5)');
    Luc Didry 1119e7
                window.ws = spawnWebsocket(i + 1, callback);
    Luc Didry a594b9
            }
    Luc Didry 91e078
        }
    Luc Didry 91e078
        return ws;
    Luc Didry 91e078
    }
    Luc Didry 91e078
    Luc Didry cc012f
    // Dropzone events binding
    Luc Didry cc012f
    function bindDropZone() {
    Luc Didry 2d835b
        var dropZone = document.getElementById('files');
    Luc Didry 2d835b
        dropZone.addEventListener('dragover', handleDragOver, false);
    Luc Didry 2d835b
        dropZone.addEventListener('drop', handleDrop, false);
    Luc Didry cc012f
        $('#file-browser-span').removeClass('disabled');
    Luc Didry cc012f
        $('#file-browser-span').addClass('cyan');
    Luc Didry cc012f
        $('#file-browser-button').attr('disabled', null);
    Luc Didry cc012f
        $('#file-browser-button').on('change', function(e) {
    Luc Didry cc012f
            handleFiles(this.files);
    Luc Didry cc012f
        });
    Luc Didry cc012f
    }
    Luc Didry cc012f
    Luc Didry cc012f
    // When it's ready
    Luc Didry d9928e
    $(document).ready(function() {
    Luc Didry d9928e
        $('#zip-files').prop('checked', false);
    Luc Didry d9928e
        $('#first-view').prop('checked', false);
    Luc Didry d9928e
        $('#zipname').val('documents.zip');
    Luc Didry cc012f
        if (!sjcl.random.isReady(10)) {
    Luc Didry cc012f
            var loop = setInterval(function() {
    Luc Didry cc012f
                if (!sjcl.random.isReady(10)) {
    Luc Didry cc012f
                    $('#not-enough-entropy').removeClass('hiddendiv');
    Luc Didry cc012f
                } else {
    Luc Didry cc012f
                    $('#not-enough-entropy').addClass('hiddendiv');
    Luc Didry cc012f
                    bindDropZone();
    Luc Didry cc012f
                    clearInterval(loop);
    Luc Didry cc012f
                }
    Luc Didry cc012f
            }, 1000);
    Luc Didry cc012f
        } else {
    Luc Didry cc012f
            bindDropZone();
    Luc Didry cc012f
        }
    Luc Didry 501db9
        if (maxSize > 0) {
    Luc Didry 501db9
            $('#max-file-size').text(i18n.maxSize.replace('XXX', filesize(maxSize)));
    Luc Didry 501db9
        }
    Luc Didry d9928e
        $('label[for="first-view"]').on('click', firstViewClicking);
    Luc Didry d9928e
        $('label[for="zip-files"]').on('click', zipClicking);
    Luc Didry d9928e
        $('#zipname').on('input', updateZipname);
    Luc Didry d9928e
        $('#uploadZip').on('click', uploadZip);
    Luc Didry d9928e
        $('#reset-zipping').on('click', function() {
    Luc Didry d9928e
            window.zip = null;
    Luc Didry d9928e
            $('label[for="zip-files"]').click();
    Luc Didry d1627e
            $('#zip-files').attr('disabled', null);
    Luc Didry d1627e
            $('#zip-compressing').addClass('hide');
    Luc Didry d1627e
            $('#file-browser-button').attr('disabled', null);
    Luc Didry d1627e
            $('#file-browser-span').removeClass('disabled');
    Luc Didry 76ce6f
            $('#files').removeClass('m6').addClass('m12');
    Luc Didry cf7993
        });
    Luc Didry 91e078
    });