file_upload.js 4.10 KiB
/*
 * Copyright (C) 2015 IRSTEA
 * All rights reserved.
 */
(function($) {
    /** Plugin irsteaFileUpload.
     */
    $.fn.irsteaFileUpload = function(options) {
        var $this             = $(this),
            prototype         = options.prototype,
            createUrl         = options.createUrl,
            uploadPrototype   = options.uploadPrototype,
            downloadPrototype = options.downloadPrototype;
        delete options.prototype;
        delete options.createUrl;
        delete options.uploadPrototype;
        delete options.downloadPrototype;
        var formatSize = function(size) {
            if(size > 1000000000) {
                return (size/1000000000).toFixed(2) + ' Gio';
            if(size > 1000000) {
                return (size/1000000).toFixed(2) + ' Mio';
            if(size > 1000) {
                return (size/1000).toFixed(2)+ ' Kio';
            return size + ' o';
        var formatBitrate = function(rate) { return formatSize(rate) + '/s'; };
        // Activation
        $this.fileupload({
            type: 'PUT',
            autoUpload: true,
            formData: {},
            multipart: false,
            uploadTemplateId: null,
            downloadTemplateId: null,
            filesContainer: $this.find('.fileinput-entries'),
            maxChunkSize: 1000000,
            uploadTemplate: function(data) {
                var rows = $();
                $.each(data.files, function (index, file) {
                    var row = $(uploadPrototype);
                    row.find('.name').text(file.name);
                    row.find('.size').text(formatSize(file.size));
                    if (file.error) {
                        row.find('.error').text(file.error);
                    rows = rows.add(row);
                });
                return rows;
            downloadTemplate: function(data) {
                var rows = $();
                $.each(data.files, function (index, file) {
                    var row = $(downloadPrototype);
                    row.find('.size').html(formatSize(file.size));
                    if (file.error) {
                        row.find('.name').text(file.name);
                        row.find('.error').text(file.error);
                    } else {
                        row.find('.name a')
                            .text(file.name)
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
.attr('data-gallery', '') .prop('href', file.url); row.find('button.delete') .attr('data-type', file.delete_type) .attr('data-url', file.delete_url); } rows = rows.add(row); }); return rows; }, submit: function (e, data) { var $this = $(this), file = data.files[0]; $.post( createUrl, { file: { name: file.name, size: file.size, type: file.type, lastModified: file.lastModified } }, function(response) { data.url = response.url; data.jqXHR = $this.fileupload('send', data); } ); return false; }, progress: function (e, data) { if(!data.context || e.isDefaultPrevented()) { return; } var progress = Math.floor(data.loaded / data.total * 100); data.context.each(function () { $(this).find('.progress') .attr('aria-valuenow', progress) .children().first().css('width',progress + '%'); $(this).find('.progress-text').html(progress + '% ('+formatBitrate(data.bitrate)+')'); }); } }).bind({ fileuploadfailed: function (e, data) { $.ajax(data.url, { type: 'DELETE' }); }, }); }; })(jQuery);