/* * 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 Rdxport = Rdxport || {}; Rdxport.Importer = function(username, token) { this.username = username; this.token = token; this.$el = $('#uploadModal'); this.uploads = []; this.webSocket = null; this.initWebSocket(); }; Rdxport.Importer.CMD_LIST = 'list'; Rdxport.Importer.CMD_NEW = 'new'; Rdxport.Importer.CMD_RECONNECT = 'reconnect'; Rdxport.Importer.prototype.initWebSocket = function() { var importer = this; var webSocket = new WebSocket('wss://import.helsinki.at/rhimportd'); webSocket.onclose = function(code, reason) { console.log('close'); console.log(code); console.log(reason); }; webSocket.onerror = function() { console.log('error'); }; webSocket.onopen = function() { console.log('open'); console.log('send list'); var sendOptions = { COMMAND: Rdxport.Importer.CMD_NEW, LOGIN_NAME: importer.username, PASSWORD: importer.token, TIMEOUT: 200, REFERENCE_ID: 999, SHOW_ID: 10000, CLEAR_SHOW_CARTS: true, SOURCE_URI: 'archiv://2016/03/31/05/00' }; console.log(sendOptions); this.send(JSON.stringify(sendOptions)); }; webSocket.onmessage = function(event) { console.log('message'); console.log(event.data); }; /*this.webSocket = new WebSocket('wss://import.helsinki.at/rhimportd'); this.webSocket.onclose = function(code, reason) { console.log('close'); console.log(code); console.log(reason); }; this.webSocket.onerror = function() { console.log('error'); }; this.webSocket.onopen = function() { console.log('open'); console.log('send reconnect'); var reconnectOptions = { COMMAND: Rdxport.Importer.CMD_RECONNECT, LOGIN_NAME: importer.username, PASSWORD: importer.token }; console.log(reconnectOptions); this.send(JSON.stringify(reconnectOptions)); }; this.webSocket.onmessage = function(event) { console.log('message'); console.log(event.data); };*/ }; Rdxport.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'); }; Rdxport.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 Rdxport.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}); }; Rdxport.Importer.prototype.isUploading = function() { var $result = false; $.each(this.uploads, function(index, upload) { if (upload && upload.isUploading()) { $result = true; } }); return $result; }; Rdxport.Importer.prototype.cancelAllUploads = function() { $.each(this.uploads, function(index, upload){ if (upload) { upload.cancel(); } }); }; Rdxport.Importer.prototype.removeUpload = function(upload) { var self = this; $.each(this.uploads, function(index, currentUpload){ if(currentUpload === upload) { self.uploads.splice(index, 1); } }); }; Rdxport.Importer.prototype.getUploadByCartNumber = function (cartNumber) { var upload = null; $.each(this.uploads, function(index, currentUpload){ if (currentUpload.cart !== null && currentUpload.cart.number === cartNumber ) { upload = currentUpload; } }); return upload; }; Rdxport.Importer.prototype.getUploadByCartAndCutNumber = function (cartNumber, cutNumber) { var upload = null; $.each(this.uploads, function(index, currentUpload) { if (currentUpload.cart !== null && currentUpload.cart.number === cartNumber && currentUpload.cut !== null && currentUpload.cut.number === cutNumber) { upload = currentUpload; } }); return upload; }; Rdxport.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.uploadprogress = { progress: 0, total: 0, bytesSent: 0 }; this.cart = null; this.cut = null; this.import(); }; Rdxport.Upload.prototype.isUploading = function() { var files = this.dropzone.getUploadingFiles(); return files.length > 0; }; Rdxport.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); }; Rdxport.Upload.prototype.import = function() { var self = this; this.dropzone.off('uploadprogress').on('uploadprogress', function(file) { self.uploadprogress = file.upload; 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); }); }; Rdxport.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', {CART_NUMBER: this.newCartNumber}, function(cartXML) { self.cart = new Rdxport.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); } }; Rdxport.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(); }; Rdxport.Upload.prototype.importFileUploadError = function(file, msg, xhr) { var self = this; this.groupView.uploadError(this, file, msg, xhr, function() { self.cancel(); }); }; Rdxport.Upload.prototype.addCut = function(file) { var self = this; rdxport.addAndEditCut(this.cart.number, {DESCRIPTION: file.name}, function(cutXml) { self.cut = new Rdxport.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(); }); };