]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/videos/+video-watch/modal/video-download.component.ts
Fix #639 providing magnet URI in player and download modal
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / modal / video-download.component.ts
index 1a73ea6df0de8796e62ad23f4935db614c1bee1c..2de706e47a707bd0b253f22aa4ca28b60e088fac 100644 (file)
@@ -12,15 +12,15 @@ export class VideoDownloadComponent implements OnInit {
 
   @ViewChild('modal') modal: ModalDirective
 
-  downloadType: 'direct' | 'torrent' = 'torrent'
-  resolution = -1
+  downloadType: 'direct' | 'torrent' | 'magnet' = 'torrent'
+  resolutionId: number | string = -1
 
   constructor () {
     // empty
   }
 
   ngOnInit () {
-    this.resolution = this.video.files[0].resolution
+    this.resolutionId = this.video.files[0].resolution.id
   }
 
   show () {
@@ -32,13 +32,28 @@ export class VideoDownloadComponent implements OnInit {
   }
 
   download () {
-    const file = this.video.files.find(f => f.resolution === this.resolution)
+    // HTML select send us a string, so convert it to a number
+    this.resolutionId = parseInt(this.resolutionId.toString(), 10)
+
+    const file = this.video.files.find(f => f.resolution.id === this.resolutionId)
     if (!file) {
-      console.error('Could not find file with resolution %d.', this.resolution)
+      console.error('Could not find file with resolution %d.', this.resolutionId)
       return
     }
 
-    const link = this.downloadType === 'direct' ? file.fileUrl : file.torrentUrl
-    window.open(link)
+    const link = (() => {
+      switch (this.downloadType) {
+        case 'direct': {
+          return file.fileDownloadUrl
+        }
+        case 'torrent': {
+          return file.torrentDownloadUrl
+        }
+        case 'magnet': {
+          return file.magnetUri
+        }
+      }
+    })()
+    window.location.assign(link)
   }
 }