/* * 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 }); var self = this; dropzone.on('addedfile', function(file) { $('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.modal('hide'); }) .removeAttr('disabled').focus(); }); this.$el.modal({keyboard: true}); }; Rivendell.Importer.prototype.cancelAllUploads = function() { $.each(this.uploads, function(index, 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.cancel = function() { this.dropzone.off(Dropzone.ERROR); this.dropzone.removeAllFiles(true); this.dropzone.disable(); 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 self = this; if (this.createCart) { rdxport.addCart(this.group.groupName, 'audio', this.newCartNumber, function(cartXML) { self.cart = new Rivendell.Cart(cartXML, self.group); success(files[0]); }).fail(function() { //self.importFileUploadError(files[0], 'Failed to add Cart.'); }); } else { this.cart = this.group.carts[0]; success(files[0]); } }; Rivendell.Upload.prototype.importFileUploadSuccess = function() { this.dropzone.off(Dropzone.ERROR); this.dropzone.removeAllFiles(true); this.dropzone.disable(); this.group.fetchCarts(); importer.removeUpload(this); }; Rivendell.Upload.prototype.importFileUploadError = function(file, msg, xhr) { this.groupView.uploadError(self, file, msg); this.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(); }); };