/* * rhwebimport * * Copyright (C) 2014-2016 Christian Pointner * Copyright (C) 2015-2016 Peter Grassberger * * This file is part of rhwebimport. * * rhwebimport is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * rhwebimport is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with rhwebimport. If not, see . */ 'use strict'; var Rivendell = Rivendell || {}; Rivendell.Importer = function() { this.$el = $('#uploadModal'); this.uploads = []; }; Rivendell.Importer.prototype.resetModal = function() { $('div.modal-header h4', this.$el).text("Datei auswählen..."); $('.modal-body', this.$el).empty().css("background-image", "url('/img/dz-backdrop.png')"); $('#uploadModal-confirm', this.$el).attr('disabled','disabled').off('click'); }; Rivendell.Importer.prototype.openModal = function(group, groupView, createCart, newCartNumber, useMetadata) { createCart = typeof createCart === 'undefined' ? false : createCart; useMetadata = typeof useMetadata === 'undefined' ? false : useMetadata; this.resetModal(); $('.modal-body', this.$el).append($('
')); var dropzone = new Dropzone('#uploadModal form', { url: '/rd-bin/rdxport.cgi', parallelUploads: 1, maxFilesize: 2048, paramName: 'FILENAME', uploadMultiple: false, // todo: maybe enable this? clickable: true, createImageThumbnails: false, acceptedFiles: '.flac,.wav,.ogg,.mp3', autoProcessQueue: false }); this.$el.off('hide.bs.modal').on('hide.bs.modal', function() { dropzone.removeAllFiles(true); dropzone.disable(); dropzone.destroy(); }); var self = this; dropzone.on('addedfile', function(file) { if (!Dropzone.isValidFile(file, dropzone.options.acceptedFiles)) { $('div.modal-header h4', self.$el).text('Nicht unterstützter Dateityp.'); $('.modal-body', this.$el).css("background-image", "url('/img/dz-backdrop.png')"); $('#uploadModal-confirm', this.$el).attr('disabled','disabled').off('click'); return; } $('div.modal-header h4', self.$el).text(file.name); $('div.modal-body', self.$el).css("background-image", "url('/img/audio_file.png')"); $('#uploadModal-confirm', self.$el).off('click').on('click', function() { var upload = new Rivendell.Upload(group, groupView, createCart, newCartNumber, useMetadata, dropzone); self.uploads.push(upload); self.$el.off('hide.bs.modal'); self.$el.modal('hide'); }) .removeAttr('disabled').focus(); }); this.$el.modal({keyboard: true}); }; Rivendell.Importer.prototype.isUploading = function() { var $result = false; $.each(this.uploads, function(index, upload) { if (upload && upload.isUploading()) { $result = true; } }); return $result; }; Rivendell.Importer.prototype.cancelAllUploads = function() { $.each(this.uploads, function(index, upload){ if (upload) { upload.cancel(); } }); }; Rivendell.Importer.prototype.removeUpload = function(upload) { var self = this; $.each(this.uploads, function(index, currentUpload){ if(currentUpload === upload) { self.uploads.splice(index, 1); } }); }; Rivendell.Upload = function(group, groupView, createCart, newCartNumber, useMetadata, dropzone) { this.group = group; this.groupView = groupView; this.createCart = createCart; this.newCartNumber = newCartNumber; this.useMetadata = useMetadata; this.dropzone = dropzone; this.cart = null; this.cut = null; this.import(); }; Rivendell.Upload.prototype.isUploading = function() { var files = this.dropzone.getUploadingFiles(); return files.length > 0; }; Rivendell.Upload.prototype.cancel = function() { this.dropzone.off(Dropzone.ERROR); this.dropzone.removeAllFiles(true); this.dropzone.disable(); this.dropzone.destroy(); var self = this; if (this.createCart && this.cart !== null) { rdxport.removeCart(this.cart.number, function() { self.group.fetchCarts(); }); this.cart = null; } else if (this.cart !== null && this.cut !== null) { rdxport.removeCut(this.cart.number, this.cut.number, function() { self.group.fetchCarts(); }); this.cut = null; } importer.removeUpload(this); }; Rivendell.Upload.prototype.import = function() { var self = this; this.dropzone.off('uploadprogress').on('uploadprogress', function(file) { self.groupView.uploadProgress(self, file); }); this.dropzone.off(Dropzone.SUCCESS).on(Dropzone.SUCCESS, function(file) { self.importFileUploadSuccess(); }); this.dropzone.off(Dropzone.ERROR).on(Dropzone.ERROR, function(file, msg, xhr) { self.importFileUploadError(file, msg, xhr); }); // debug //self.importFileUploadError({cartNumber: 100000}, '123Fake error', null); this.addCart(function(file) { self.addCut(file); }); }; Rivendell.Upload.prototype.addCart = function(success) { var files = this.dropzone.getAcceptedFiles(); var file = files[files.length - 1]; var self = this; if (this.createCart) { rdxport.addCart(this.group.groupName, 'audio', this.newCartNumber, function(cartXML) { self.cart = new Rivendell.Cart(cartXML, self.group); success(file); }).fail(function() { //self.importFileUploadError(files[0], 'Failed to add Cart.'); }); } else { this.cart = this.group.carts[0]; success(file); } } Rivendell.Upload.prototype.importFileUploadSuccess = function() { this.dropzone.off(Dropzone.ERROR); this.dropzone.removeAllFiles(true); this.dropzone.disable(); this.dropzone.destroy(); importer.removeUpload(this); this.group.fetchCarts(); }; Rivendell.Upload.prototype.importFileUploadError = function(file, msg, xhr) { var self = this; this.groupView.uploadError(this, file, msg, xhr, function() { self.cancel(); }); }; Rivendell.Upload.prototype.addCut = function(file) { var self = this; rdxport.addAndEditCut(this.cart.number, {DESCRIPTION: file.name}, function(cutXml) { self.cut = new Rivendell.Cut(cutXml, self.cart); self.dropzone.on('sending', function(file, xhr, formData) { var cutNumber = $('cutNumber', cutXml).text(); var cutNumberLeading = cutNumber; switch (cutNumber.toString().length) { case 0: cutNumberLeading = '000' + cutNumber; break; case 1: cutNumberLeading = '00' + cutNumber; break; case 2: cutNumberLeading = '0' + cutNumber; break; case 3: default: cutNumberLeading = cutNumber; break; } file.cartNumber = self.cart.number; file.cutNumber = cutNumberLeading; formData.append('COMMAND', 2); formData.append('LOGIN_NAME', auth_username); formData.append('PASSWORD', auth_token); formData.append('CART_NUMBER', self.cart.number); formData.append('CUT_NUMBER', cutNumber); formData.append('CHANNELS', 2); formData.append('NORMALIZATION_LEVEL', self.cart.normlevel); formData.append('AUTOTRIM_LEVEL', self.cart.trimlevel); formData.append('USE_METADATA', (self.useMetadata ? 1 : 0)); }); self.dropzone.processQueue(); self.group.fetchCarts(); }); };