FileManager.vue 21 KB
Newer Older
1
<template>
2
3
4
5
6
7
  <b-container>
    <b-row>
      <b-col>
        <h3>Dateien und Playlists</h3>
      </b-col>
      <b-col align="right">
8
9
10
11
12
13
14
15
16
17
18
19
        <b-dropdown
          id="ddshows"
          text="Sendereihe auswählen"
          variant="info"
        >
          <b-dropdown-item
            v-for="(show, index) in this.shows"
            :key="show.id"
            @click="switchShow(index)"
          >
            {{ show.name }}
          </b-dropdown-item>
20
21
22
        </b-dropdown>
      </b-col>
    </b-row>
23
    <hr>
24
25
26
27
28
29
30
31
32
33
    <b-jumbotron>
      <template slot="header">
        <span v-if="loaded.shows">
          {{ shows[currentShow].name }}
        </span>
        <span v-else>Shows are being loaded</span>
      </template>
      <template slot="lead">
        <span v-if="loaded.shows">{{ shows[currentShow].short_description }}</span>
      </template>
34
      <hr>
35
36
      <div align="center">
        <b-button-group>
37
38
39
40
41
42
43
44
45
46
47
48
49
50
          <b-button
            size="lg"
            :variant="button.files"
            @click="switchMode('files')"
          >
            Files
          </b-button>
          <b-button
            size="lg"
            :variant="button.playlists"
            @click="switchMode('playlists')"
          >
            Playlists
          </b-button>
51
52
53
54
55
56
57
58
        </b-button-group>
      </div>
    </b-jumbotron>

    <div v-if="mode === 'files'">
      <div v-if="!loaded.files">
        <b-row>
          <b-col align="center">
59
60
61
62
            <img
              src="../assets/radio.gif"
              alt="loading data"
            >
63
64
65
66
          </b-col>
        </b-row>
      </div>
      <div v-else>
67
68
69
70
71
        <b-modal
          id="modal-add-file"
          title="Add new file"
          @ok="addFile"
        >
72
73
74
75
76
77
          <div v-if="addNewFileURI">
            <b-row>
              <b-col md="2">
                <b>Link:</b>
              </b-col>
              <b-col>
78
79
80
81
82
                <b-form-input
                  v-model="uploadSourceURI"
                  type="url"
                  placeholder="Insert a HTTP(S) link here"
                />
83
84
85
86
              </b-col>
            </b-row>
          </div>
          <div v-else>
87
88
89
90
91
92
            <b-form-file
              v-model="uploadSourceFile"
              accept="audio/*"
              placeholder="Choose a file..."
              drop-placeholder="Drop file here..."
            />
93
94
95
          </div>
          <hr>
          <div align="center">
96
97
98
99
100
            <b-form-checkbox
              v-model="addNewFileURI"
              value="true"
              unchecked_value="false"
            >
101
102
103
104
              Download from remote source instead of uploading a file
            </b-form-checkbox>
          </div>
        </b-modal>
105
106
107
108
109
110
        <b-modal
          id="modal-edit-file"
          title="Edit meta information"
          size="lg"
          @ok="saveFile"
        >
111
          <b-row>
112
113
114
115
116
117
118
119
120
            <b-col md="2">
              Artist:
            </b-col>
            <b-col>
              <b-form-input
                v-model="temp.artist"
                type="text"
              />
            </b-col>
121
122
          </b-row>
          <b-row>
123
124
125
126
127
128
129
130
131
            <b-col md="2">
              Album:
            </b-col>
            <b-col>
              <b-form-input
                v-model="temp.album"
                type="text"
              />
            </b-col>
132
133
          </b-row>
          <b-row>
134
135
136
137
138
139
140
141
142
            <b-col md="2">
              Title:
            </b-col>
            <b-col>
              <b-form-input
                v-model="temp.title"
                type="text"
              />
            </b-col>
143
          </b-row>
144
          <hr>
145
          <b-row>
146
147
148
            <b-col md="2">
              Sourced from:
            </b-col>
149
150
151
            <b-col>{{ temp.uri }}</b-col>
          </b-row>
        </b-modal>
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
        <div
          v-if="files.length === 0"
          align="center"
        >
          <b-alert
            show
            variant="warning"
          >
            There are no files for this show yet.
          </b-alert>
          <b-button
            v-b-modal.modal-add-file
            variant="success"
          >
            Upload or add a file
          </b-button>
168
169
        </div>
        <div v-else>
170
171
172
173
174
175
176
177
178
179
          <div
            align="center"
            style="padding-bottom: 1.5em;"
          >
            <b-button
              v-b-modal.modal-add-file
              variant="success"
            >
              Upload or add a file
            </b-button>
180
          </div>
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
          <b-table
            ref="filesTable"
            striped
            :fields="filesTableFields"
            :items="files"
          >
            <template
              slot="metadata.album"
              slot-scope="data"
            >
              <span v-if="data.item.source.import.state !== 'done'"><img
                src="../assets/radio.gif"
                width="24"
                alt="loading data"
              ></span>
196
197
              <span v-else>{{ data.value }}</span>
            </template>
198
199
200
201
            <template
              slot="metadata.title"
              slot-scope="data"
            >
202
203
              <span v-if="data.item.source.import.state === 'done'">{{ data.value }}</span>
              <span v-else-if="data.item.source.import.progress !== undefined">{{ data.item.source.import.progress.step }} :</span>
204
            </template>
205
206
207
208
209
210
211
            <template
              slot="duration"
              slot-scope="data"
            >
              <div v-if="data.item.source.import.state === 'done'">
                {{ prettyNanoseconds(data.value) }}
              </div>
212
213
              <div v-else-if="data.item.source.import.progress !== undefined">
                <div v-if="data.item.source.import.progress.step === 'fetching'">
214
215
216
217
218
219
220
                  <b-progress
                    :value="data.item.source.import.progress.progress"
                    :max="1"
                    show-progress
                    variant="info"
                    animated
                  />
221
222
                </div>
                <div v-else>
223
224
225
226
227
228
229
                  <b-progress
                    :value="data.item.source.import.progress.progress"
                    :max="1"
                    show-progress
                    variant="success"
                    animated
                  />
230
231
                </div>
              </div>
232
            </template>
233
234
235
236
            <template
              slot="size"
              slot-scope="data"
            >
237
              <span v-if="data.item.source.import.state === 'done'">{{ prettyFileSize(data.value) }}</span>
238
            </template>
239
240
241
242
            <template
              slot="actions"
              slot-scope="data"
            >
243
              <b-button-group size="sm">
244
245
246
247
248
249
250
251
252
                <b-button @click="editFile(data.item.id)">
                  Edit
                </b-button>
                <b-button
                  variant="danger"
                  @click="deleteFile(data.item.id)"
                >
                  Delete
                </b-button>
253
254
255
              </b-button-group>
            </template>
          </b-table>
256
        </div>
257
258
259
260
261
262
263
      </div>
    </div>

    <div v-if="mode === 'playlists'">
      <div v-if="!loaded.playlists">
        <b-row>
          <b-col align="center">
264
265
266
267
            <img
              src="../assets/radio.gif"
              alt="loading data"
            >
268
269
270
271
          </b-col>
        </b-row>
      </div>
      <div v-else>
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
        <div
          v-if="playlists.length === 0"
          align="center"
        >
          <b-alert
            show
            variant="warning"
          >
            There are no playlists for this show yet.
          </b-alert>
          <b-button
            variant="success"
            @click="notYetImplemented"
          >
            Create a playlist
          </b-button>
288
289
        </div>
        <div v-else>
290
291
292
293
          <b-table
            striped
            :items="playlistsTable"
          />
294
        </div>
295
      </div>
296
    </div>
297
  </b-container>
298
299
300
</template>

<script>
301
import axios from 'axios'
302
303
import filesize from 'filesize'
import prettyDate from '../mixins/prettyDate'
304
305

export default {
306
  mixins: [ prettyDate ],
307
308
309
310
311
  data () {
    return {
      shows: [],        // an array of objects describing our shows (empty at load, will be populated on created())
      currentShow: 0,   // index of the currently selected show in our shows array
      currentShowID: 0, // actual id of the currently selected show
312
313
      files: [],
      playlists: [],
314
      mode: 'files',
315
316
317
      addNewFileURI: false,
      uploadSourceURI: '',
      uploadSourceFile: null,
318
      uploadInterval: null,
319
320
321
322
323
      loaded: {
        shows: false,
        files: false,
        playlists: false
      },
324
325
326
327
328
329
      temp: {
        id: null,
        artist: '',
        album: '',
        title: ''
      },
330
331
332
      button: {
        files: 'info',
        playlists: 'outline-info'
333
334
335
336
337
338
339
340
341
342
      },
      filesTableFields: [
        { key: 'id', label: 'Index' },
        { key: 'metadata.artist', label: 'Artist' },
        { key: 'metadata.album', label: 'Album' },
        { key: 'metadata.title', label: 'Title' },
        { key: 'duration', label: 'Duration' },
        { key: 'size', label: 'Size' },
        { key: 'actions', label: 'Actions' },
      ]
343
344
    }
  },
345
346
347
348
349
350
351
352
353
354
355
356
357
358
  computed: {
    playlistsTable: function (){
      var arr = []
      for (var i in this.files) {
        arr.push({
          id: this.files[i].id,
          other_fields: 'not yet implemented',
          updated: this.files[i].updated,
          actions: '...'
        })
      }
      return arr
    }
  },
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
  created () {
    // when we enter this module, we want to load all shows of the current user
    // before we search for corresponding shows in the tank
    var uri = process.env.VUE_APP_API_STEERING_SHOWS
    // only the superuser should see all shows and therefore files and playlists
    // normal users should only see their own shows
    if (!this.$parent.user.steeringUser.is_superuser) {
      uri += '?owner=' + this.$parent.user.steeringUser.id
    }
    // now make the API call to fetch the shows
    axios.get(uri, {
      withCredentials: true,
      headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
    }).then(response => {
      if (response.data.length === 0) {
        alert('There are now shows associated with your account!')
        return
      }
      this.shows = response.data
      this.currentShowID = this.shows[0].id
      this.currentShow = 0
      this.loaded.shows = true
      this.switchShow(this.currentShow)
    }).catch(error => {
      alert('There was an error fetching shows from steering: ' + error)
    })
  },
386
  methods: {
387
388
389
    notYetImplemented: function () {
      alert('By the mighty witchcraftry of the mother of time!\n\nThis feature is not implemented yet.')
    },
390
391
392
    prettyFileSize: function (s) {
      return filesize(s)
    },
393
394
395
396
397
398
399
400
    getFileById: function (id) {
      for (var i in this.files) {
        if (this.files[i].id === id) {
          return this.files[i]
        }
      }
      return null
    },
401
    editFile: function (id) {
402
403
404
405
406
407
408
409
410
411
      var file = this.getFileById(id)
      this.temp.id = file.id
      this.temp.artist = file.metadata.artist
      this.temp.album = file.metadata.album
      this.temp.title = file.metadata.title
      this.temp.uri = file.source.uri
      this.$bvModal.show('modal-edit-file')
    },
    saveFile: function (){
      var file = this.getFileById(this.temp.id)
412
      // we only want to send a PATCH request if some metadata actually changed
413
      if (this.temp.artist !== file.metadata.artist || this.temp.album !== file.metadata.album || this.temp.title !== file.metadata.title ) {
414
415
416
        // if a metadata property was in use before and now shall be emptied
        // we cannot just omit the property, but have to explicitly send null
        var metadata = {
417
          artist: this.temp.artist ? this.temp.artist : null,
418
419
420
          album: this.temp.album ? this.temp.album : null,
          title: this.temp.title ? this.temp.title : null
        }
421
        var uri = process.env.VUE_APP_API_TANK + 'shows/' + this.shows[this.currentShow].slug + '/files/' + file.id
422
        // TODO: add mechanism to indicate the running patch request in the files table
423
424
425
426
427
428
429
430
431
432
433
        axios.patch(uri, metadata, {
          withCredentials: true,
          headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
        }).then(response => {
          this.getFileById(this.temp.id).metadata = response.data.metadata
        }).catch(error => {
          console.log('Error:')
          console.log(error)
          alert('Error: could not save metadata info to file. See console log for details.')
        })
      }
434
435
436
    },
    deleteFile: function (id) {
      var uri = process.env.VUE_APP_API_TANK + 'shows/' + this.shows[this.currentShow].slug + '/files/' + id
437
      // TODO: add mechanism to indicate the running delete request in the files table
438
439
440
441
      axios.delete(uri, {
        withCredentials: true,
        headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token },
      }).then(
442
        this.fetchFiles(this.shows[this.currentShow].slug)
443
444
445
446
447
448
      ).catch(error => {
        console.log('Error:')
        console.log(error)
        alert('Error: could not delete file. See console log for details.')
      })
    },
449
    addFile: function () {
450
451
      var uri = process.env.VUE_APP_API_TANK + 'shows/' + this.shows[this.currentShow].slug + '/files'
      if (this.addNewFileURI) {
452
453
454
455
        // TODO: add mechanism to indicate the running post request in the files table
        axios.post(uri, { 'source-uri': this.uploadSourceURI }, {
          withCredentials: true,
          headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
456
        }).then(() => {
457
          this.fetchFiles(this.shows[this.currentShow].slug)
458
459
460
461
          if (this.uploadInterval === null) {
            this.uploadInterval = setInterval(() => { this.fetchImports(this.shows[this.currentShow].slug) }, 250)
          }
        }).catch(error => {
462
463
464
465
          console.log('Error:')
          console.log(error)
          alert('Error: could not add the new remote import. See console log for details.')
        })
466
      } else if (this.uploadSourceFile) {
467
        // TODO: add mechanism to indicate the running post request in the files table
468
469
470
471
472
        axios.post(uri, { 'source-uri': encodeURI('upload://' + this.uploadSourceFile.name) }, {
          withCredentials: true,
          headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
        }).then(response => {
          this.startUpload(response.data.id)
473
          this.fetchFiles(this.shows[this.currentShow].slug)
474
475
476
          if (this.uploadInterval === null) {
            this.uploadInterval = setInterval(() => { this.fetchImports(this.shows[this.currentShow].slug) }, 250)
          }
477
478
479
480
481
482
        }).catch(error => {
          console.log('Error:')
          console.log(error)
          alert('Error: could not add the new file upload. See console log for details.')
        })
      } else {
483
        alert('Something is wrong. You have choosen to upload a file, but the corresponding file object does not exist.')
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
      }
    },
    startUpload: function (id) {
      var uri = process.env.VUE_APP_API_TANK + 'shows/' + this.shows[this.currentShow].slug + '/files/' + id + '/import'
      axios.get(uri, {
        withCredentials: true,
        headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token },
        params: {'wait-for': 'running'}
      }).then(
        this.upload(id)
      ).catch(error => {
        console.log('Error:')
        console.log(error)
        alert('Error: could not start the file upload. See console log for details.')
      })
    },
    upload: function (id) {
      /*
       * NOTE: there is no npm package for flow.js and importing it manually did not
       *       work so far. therefore this is commented out and we are using the simple
       *       upload method, until there is a nice npm package for flow.js or somone
       *       resolves this issue otherwise
      var flow = new Flow({
        target: process.env.VUE_APP_API_TANK + 'shows/' + this.shows[this.currentShow].slug + '/files/' + id + '/upload',
        chunkSize: 100 * 1024,
        prioritizeFirstAndLastChunk: true
      })
      flow.on('fileSuccess', function(file, message) {
        console.log(file, message)
      })
      flow.on('fileError', function(file, message) {
        console.log(file, message)
        alert('Error: could not upload your file. See console log for details.')
      })
      flow.addFile(this.uploadSourceFile)
      flow.upload()
      */
      var uri = process.env.VUE_APP_API_TANK + 'shows/' + this.shows[this.currentShow].slug + '/files/' + id + '/upload'
      axios.put(uri, this.uploadSourceFile, {
        withCredentials: true,
        headers: {
          'Authorization': 'Bearer ' + this.$parent.user.access_token,
          'Content-Type': 'application/octet-stream'
        }
528
529
530
531
532
533
534
      }).then(() => {
        console.log('Sucessfully uploaded file.')
        // now we start polling for the import progress
        // the fetchImports function has to make sure to deactivate the interval
        // again, when all running imports are done (in this first raw version;
        // ideally we should refine this so that every single file gets updated independently)
        //this.uploadInterval = setInterval(() => { this.fetchImports(this.shows[this.currentShow].slug) }, 100)
535
536
537
538
539
      }).catch(error => {
        console.log('Error:')
        console.log(error)
        alert('Error: could not start the file upload. See console log for details.')
      })
540
    },
541
542
543
544
    switchShow: function (index) {
      // set the current show and its ID to whatever we want to switch to now
      this.currentShow = index
      this.currentShowID = this.shows[this.currentShow].id
545
      this.fetchShow(this.shows[this.currentShow].slug)
546
547
548
549
550
    },
    switchMode: function (mode) {
      if (this.mode !== mode) {
        this.mode = mode
        for (var b in this.button) {
551
552
          if (b === mode) { this.button[b] = 'info' }
          else { this.button[b] = 'outline-info' }
553
554
555
        }
      }
    },
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
    fetchImports: function (slug){
      var uri = process.env.VUE_APP_API_TANK + 'shows/' + slug + '/imports'
      axios.get(uri, {
        withCredentials: true,
        headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
      }).then(response => {
        // if all imports are done, we will receive an empty result set and stop
        // polling the server again. now we can also refetch all file statuses.
        if (response.data.results === null) {
          clearInterval(this.uploadInterval)
          this.uploadInterval = null
          this.fetchShow(slug)
        } else {
          for (var i in response.data.results) {
            var f = this.getFileById(response.data.results[i].id)
            if (f) {
              f.source.import.progress = {
                progress: response.data.results[i].progress.progress,
                step: response.data.results[i].progress.step
              }
            }
            var p = '[import]'
            p += ' id: ' + response.data.results[i].id
            p += ', progress: ' + response.data.results[i].progress.progress
            p += ', step: ' + response.data.results[i].progress.step
            console.log(p)
            this.$refs.filesTable.refresh()
          }
        }
      }).catch(error => {
        console.log(error)
        alert('There was an error fetching current imports. See console for details.')
      })
    },
590
591
592
593
594
    fetchShow: function (slug) {
      this.fetchFiles(slug)
      this.fetchPlaylists(slug)
    },
    fetchFiles: function (slug) {
595
596
597
598
599
600
601
602
603
604
605
606
607
      this.loaded.files = false
      var uri = process.env.VUE_APP_API_TANK + 'shows/' + slug + '/files'
      axios.get(uri, {
        withCredentials: true,
        headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
      }).then(response => {
        // we don't have to check separately, if there are files, because tank
        // always provides an empty array if there are no files (or even if there is no corresponding show)
        this.files = response.data.results
        this.loaded.files = true
      }).catch(error => {
        alert('There was an error fetching files from tank: ' + error)
      })
608
609
610
611
    },
    fetchPlaylists: function (slug) {
      this.loaded.playlists = false
      var uri = process.env.VUE_APP_API_TANK + 'shows/' + slug + '/playlists'
612
613
614
615
616
617
618
619
620
621
622
      axios.get(uri, {
        withCredentials: true,
        headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
      }).then(response => {
        // we don't have to check separately, if there are playlists, because tank
        // always provides an empty array if there are no playlists (or even if there is no corresponding show)
        this.playlists = response.data.results
        this.loaded.playlists = true
      }).catch(error => {
        alert('There was an error fetching playlists from tank: ' + error)
      })
623
624
625
    }
  }
}
626
627
628
</script>

<style>
629
630
631
632
633
div.filelistbox {
  border: 1px solid #e9ecef;
  border-radius: 0.3rem;
  padding: 1rem 2rem;
}
634
635
636
637
638
639
640
641
642
643
644
.stateNew {
  color: red;
  font-weight: bold;
}
.stateRunning {
  color: darkgreen;
}
.stateUndefined {
  color: orange;
  font-weight: bold;
}
645
</style>