summaryrefslogtreecommitdiff
path: root/www/js/importer.js
blob: c17c9537858c8fcb2bdf333c5010d213c5d8f7a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 *  rhwebimport
 *
 *  Copyright (C) 2014-2016 Christian Pointner <equinox@helsinki.at>
 *  Copyright (C) 2015-2016 Peter Grassberger <petertheone@gmail.com>
 *
 *  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 <http://www.gnu.org/licenses/>.
 */

'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).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, useMetadata) {
  this.resetModal();

  if (createCart === undefined) {
    createCart = false;
  }

  if (useMetadata === undefined) {
    useMetadata = false;
  }

  var $form = $('<form>');
  var self = this;
  var dropzone = $form.dropzone({
    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,
    init: function() {
      this.on('addedfile', function(file) {
        self.importFileAdded(this, file, group, groupView, createCart, useMetadata);
      });
    }
  });

  this.uploads.push(dropzone);

  $('.modal-body', this.$el).empty().append($form);
  this.$el.modal({keyboard: true});
};

Rivendell.Importer.prototype.importFileAdded = function(dropzone, file, group, groupView, createCart, useMetadata) {
  // wtf? remove accepted files?
  /*$(dropzone.getAcceptedFiles()).each(function(index, elem) {
    dropzone.removeFile(elem);
  });*/

  $('div.modal-body', this.$el).css("background-image", "url('/img/audio_file.png')");
  $('div.modal-header h4', this.$el).text(file.name);

  var self = this;
  $('#uploadModal-confirm', this.$el).off('click').on('click', function() {
    self.importCartConfirm(dropzone, group, groupView, createCart, useMetadata);
  }).removeAttr('disabled');
};

Rivendell.Importer.prototype.importCartConfirm = function(dropzone, group, groupView, createCart, useMetadata) {
  this.$el.modal('hide');

  //var cart_row = $('#show-cart-' + cart);
  //cart_row.find('.btn').attr('disabled','disabled');
  //var importing_row = jingles_newImportingEntry(cart);
  //importing_row.find('button').unbind('click').click(function() { jingles_importCartCancel(cart, dropzone); });
  //cart_row.replaceWith(importing_row);

  var files = dropzone.getAcceptedFiles();
  //importing_row.find('.file-name').text(files[0].name);
  //jingles_importUpdateProgress(files[0], importing_row);
  var self = this;
  dropzone.on('uploadprogress', function(file) {
    groupView.uploadProgress(file);
  });
  dropzone.on('success', function(file) {
    self.importFileUploadSuccess(dropzone, file, group);
  });
  dropzone.off('error').on('error', function(file, msg, xhr) {
    self.importFileUploadError(this, file, msg, xhr);
  });
  /*$(files).each(function(index, file) {
   self.importAddCut(dropzone, group, file);
   });*/

  if (createCart) {
    rivendell.addCart(group.groupName, 'audio', null, function(cartXML) {
      var cart = new Rivendell.Cart(cartXML, group);
      self.importAddCut(dropzone, group, files[0], createCart, cart, useMetadata);
    });
  } else {
    var cart = group.mainCart;
    self.importAddCut(dropzone, group, files[0], createCart, cart, useMetadata);
  }
};

Rivendell.Importer.prototype.importFileUploadSuccess = function(dropzone, file, group) {
  dropzone.disable();
  this.uploads = $.grep(this.uploads, function(element) {
    return element !== dropzone;
  });
  group.fetchCarts();
};

Rivendell.Importer.prototype.importFileUploadError = function(dropzone, file, msg, xhr) {
  //var error_row = jingles_newImportErrorEntry(cart, msg);
  //error_row.find('button').unbind('click').click(function() {
  //jingles_deleteCart(cart);
  //});
  //$('#show-cart-' + cart).replaceWith(error_row);
  //error_row.find('.file-name').text(file.name);
  dropzone.removeFile(file);
  dropzone.disable();
  this.uploads = $.grep(this.uploads, function(element) {
    return element !== dropzone;
  });
};

Rivendell.Importer.prototype.importAddCut = function(dropzone, group, file, createCart, cart, useMetadata) {
  rivendell.addAndEditCut(cart.number, {DESCRIPTION: file.name}, function(cutXml) {
    group.fetchCarts();
    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 = cart.number;
      file.cutNumber = cutNumberLeading;

      formData.append('COMMAND', 2);
      formData.append('LOGIN_NAME', auth_username);
      formData.append('PASSWORD', auth_token);
      formData.append('CART_NUMBER', cart.number);
      formData.append('CUT_NUMBER', cutNumber);
      formData.append('CHANNELS', 2);
      formData.append('NORMALIZATION_LEVEL', cart.normlevel);
      formData.append('AUTOTRIM_LEVEL', cart.trimlevel);
      if (useMetadata) {
        formData.append('USE_METADATA', 1);
      } else {
        formData.append('USE_METADATA', 0);
      }
    });
    dropzone.processQueue();
  }).fail(function(xhr, status, err) {
    self.importFileUploadError(dropzone, file, err, xhr);
    if (createCart) {
      rivendell.removeCart(cart.number);
    }
  });
};