diff options
Diffstat (limited to 'www/js/importer.js')
-rw-r--r-- | www/js/importer.js | 124 |
1 files changed, 92 insertions, 32 deletions
diff --git a/www/js/importer.js b/www/js/importer.js index 1d99433..09f5db5 100644 --- a/www/js/importer.js +++ b/www/js/importer.js @@ -125,57 +125,62 @@ Rdxport.Importer.prototype.initListWebSocket = function() { Rdxport.Importer.prototype.resetModal = function() { $('div.modal-header h4', this.$el).text("Datei auswählen..."); - $('.modal-body .drop', this.$el).empty().css("background-image", "url('/img/dz-backdrop.png')"); + $('.modal-body .drop', this.$el).css("background-image", "url('/img/dz-backdrop.png')"); + $('.modal-body .drop form input[type="file"]', this.$el).val(''); $('.modal-body #sourceUri', this.$el).val(''); $('#uploadModal-confirm', this.$el).attr('disabled','disabled').off('click'); }; Rdxport.Importer.prototype.openModal = function(group, groupView, cartNumber, newCartNumber, useMetadata) { this.resetModal(); - $('.modal-body .drop', this.$el).append($('<form>')); - - 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 $drop = $('.modal-body .drop', this.$el); + var $form = $('form', $drop); + var $fileInput = $('input[type="file"]', $form); + + $drop.on('click', function(event) { + $fileInput.get(0).click(); + }); + + $fileInput.on('change', function() { + handleFiles($(this).get(0).files); + }); + + $drop.on('dragenter dragover', function(event) { + event.stopPropagation(); + event.preventDefault(); }); - this.$el.off('hide.bs.modal').on('hide.bs.modal', function() { - dropzone.removeAllFiles(true); - dropzone.disable(); - dropzone.destroy(); + $drop.on('drop', function(event) { + event.stopPropagation(); + event.preventDefault(); + var dataTransfer = event.dataTransfer; + handleFiles(dataTransfer.files); }); var self = this; - dropzone.on('addedfile', function(file) { - if (!Dropzone.isValidFile(file, dropzone.options.acceptedFiles)) { + function handleFiles(files) { + var file = files[0]; + + /*if (!Dropzone.isValidFile(file, dropzone.options.acceptedFiles)) { $('div.modal-header h4', self.$el).text('Nicht unterstützter Dateityp.'); $('.modal-body .drop', 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 .drop', self.$el).css("background-image", "url('/img/audio_file.png')"); $('.modal-body #sourceUri', self.$el).val(''); - $('#uploadModal-confirm', self.$el).off('click').on('click', function() { - // todo - //var sourceUri = ''; - //var upload = new Rdxport.Upload(group, groupView, createCart, newCartNumber, useMetadata, dropzone); - //self.uploads.push(upload); + var upload = new Rdxport.Upload(file, group, groupView, cartNumber, newCartNumber, useMetadata); + upload.import(); + self.uploads.push(upload); self.$el.off('hide.bs.modal'); self.$el.modal('hide'); }) .removeAttr('disabled').focus(); - }); + } $('.modal-body #sourceUri', this.$el).off().on('keyup change', function(event) { if ($(this).val() === '') { @@ -183,6 +188,7 @@ Rdxport.Importer.prototype.openModal = function(group, groupView, cartNumber, ne return; } $('.modal-body .drop', self.$el).css("background-image", "url('/img/dz-backdrop.png')"); + $('.modal-body .drop form input[type="file"]', this.$el).val(''); $('#uploadModal-confirm', self.$el).off('click').on('click', function() { var sourceUri = $('.modal-body #sourceUri', self.$el).val(); var upload = new Rdxport.Upload(sourceUri, group, groupView, cartNumber, newCartNumber, useMetadata); @@ -209,6 +215,14 @@ Rdxport.Importer.prototype.isUploading = function() { return $result; }; +Rdxport.Importer.prototype.closeAllUploads = function() { + $.each(this.uploads, function(index, upload) { + if (upload) { + upload.close(); + } + }); +}; + Rdxport.Importer.prototype.cancelAllUploads = function() { $.each(this.uploads, function(index, upload) { if (upload) { @@ -275,9 +289,20 @@ Rdxport.Importer.prototype.getUploadsByGroupName = function(groupName) { }); }; -Rdxport.Upload = function(sourceUri, group, groupView, cartNumber, newCartNumber, useMetadata) { - this.sourceUri = sourceUri; - this.filename = sourceUri ? sourceUri.substr(sourceUri.lastIndexOf('/') + 1) : ''; +Rdxport.Upload = function(fileOrsourceUri, group, groupView, cartNumber, newCartNumber, useMetadata) { + this.sourceUri = ''; + this.file = null; + this.filename = ''; + + if (typeof fileOrsourceUri === "string") { + this.sourceUri = fileOrsourceUri; + this.filename = fileOrsourceUri ? fileOrsourceUri.substr(fileOrsourceUri.lastIndexOf('/') + 1) : ''; + } else { + this.file = fileOrsourceUri; + this.filename = fileOrsourceUri.name; + this.sourceUri = 'attachment://' + fileOrsourceUri.size + '/' + this.filename; + } + this.group = group; this.groupView = groupView; this.cartNumber = cartNumber; @@ -287,16 +312,17 @@ Rdxport.Upload = function(sourceUri, group, groupView, cartNumber, newCartNumber this.uploadId = null; this.webSocket = null; this.uploadprogress = { - bytesSent: 0, + current: 0, total: 0, progress: 0, progress_step: 0, progress_step_name: '' }; + this.sendingFile = false; }; Rdxport.Upload.prototype.isUploading = function() { - return this.uploadprogress.progress_step < 2 && this.uploadprogress.progress < 100; + return this.file && this.uploadprogress.progress_step < 2; }; Rdxport.Upload.prototype.onclose = function(code, reason) { @@ -307,6 +333,7 @@ Rdxport.Upload.prototype.onclose = function(code, reason) { Rdxport.Upload.prototype.onerror = function(error) { console.log('error'); + console.log(error); var file = { cartNumber: this.cartNumber || this.newCartNumber @@ -322,18 +349,45 @@ Rdxport.Upload.prototype.onmessage = function(event) { }; switch (data.TYPE.toLowerCase()) { case Rdxport.Importer.TYPE_ERROR: + console.log(data); this.importFileUploadError(file, data.ERROR_STRING); break; case Rdxport.Importer.TYPE_PROGRESS: + this.uploadprogress.current = data.CURRENT; + this.uploadprogress.total = data.TOTAL; this.uploadprogress.progress = data.PROGRESS; this.uploadprogress.progress_step = data.PROGRESS_STEP; this.uploadprogress.progress_step_name = data.PROGRESS_STEP_NAME; this.groupView.uploadProgress(this, file); break; case Rdxport.Importer.TYPE_DONE: + console.log(data); this.importFileUploadSuccess(); break; } + + if (this.file && !this.sendingFile) { + this.sendingFile = true; + + var chunkSize = 60 * 1024; + var start = 0; + var end = chunkSize; + var self = this; + var interval = setInterval(function() { + if (self.webSocket.bufferedAmount > 4 * chunkSize) { + return true; + } + var blob = self.file.slice(start, end, {type: "application/octet-stream"}); + self.webSocket.send(blob); + + if (self.file.size === end) { + clearInterval(interval); + } + start += chunkSize; + end += chunkSize; + end = end > self.file.size ? self.file.size : end; + }, 10); + } }; Rdxport.Upload.prototype.import = function() { @@ -349,7 +403,7 @@ Rdxport.Upload.prototype.import = function() { COMMAND: Rdxport.Importer.CMD_NEW, LOGIN_NAME: importer.username, PASSWORD: importer.token, - TIMEOUT: 200, + TIMEOUT: 7200, // 2 hours SOURCE_URI: self.sourceUri, CHANNELS: 2, // todo: is this needed? @@ -397,6 +451,12 @@ Rdxport.Upload.prototype.reconnect = function(id, reference) { }; }; +Rdxport.Upload.prototype.close = function() { + this.webSocket.close(); + this.group.fetchCarts(); + importer.removeUpload(this); +}; + Rdxport.Upload.prototype.cancel = function() { this.webSocket.send(JSON.stringify({ COMMAND: 'cancel' })); this.webSocket.close(); |