From 313a195831b0a800e3092cb0e003c65125dc0ed4 Mon Sep 17 00:00:00 2001
From: Fabio Manganiello <blacklight86@gmail.com>
Date: Sun, 29 Sep 2019 17:48:30 +0200
Subject: [PATCH] Support for torrent direct download through magnet link in
 web panel

---
 .../css/source/webpanel/plugins/media/torrents.scss   | 11 +++++++++++
 .../backend/http/static/js/plugins/media/torrents.js  | 10 ++++++++++
 .../backend/http/templates/plugins/media/index.html   |  1 -
 .../http/templates/plugins/media/torrents.html        |  9 +++++++++
 4 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/platypush/backend/http/static/css/source/webpanel/plugins/media/torrents.scss b/platypush/backend/http/static/css/source/webpanel/plugins/media/torrents.scss
index 70387bfb7..b813c4a8e 100644
--- a/platypush/backend/http/static/css/source/webpanel/plugins/media/torrents.scss
+++ b/platypush/backend/http/static/css/source/webpanel/plugins/media/torrents.scss
@@ -7,6 +7,17 @@
                 padding: 0;
                 text-align: center;
 
+                .search {
+                    input[type=text] {
+                        width: 88%;
+                        max-width: none;
+                    }
+
+                    input[type=submit] {
+                        width: 8%;
+                    }
+                }
+
                 .head {
                     font-weight: bold;
                     padding: .5rem 0 2.5rem 0;
diff --git a/platypush/backend/http/static/js/plugins/media/torrents.js b/platypush/backend/http/static/js/plugins/media/torrents.js
index cdeebef00..8c7343581 100644
--- a/platypush/backend/http/static/js/plugins/media/torrents.js
+++ b/platypush/backend/http/static/js/plugins/media/torrents.js
@@ -63,6 +63,16 @@ Vue.component('media-torrents', {
             this.selectedItem = item;
             openDropdown(this.$refs.menu);
         },
+
+        onMagnetDownload: async function() {
+            const magnet = this.$refs.magnetLink.value.trim();
+            if (!magnet.length)
+                return;
+
+            const ret = await request('torrent.download', {
+                torrent: magnet,
+            });
+        }
     },
 });
 
diff --git a/platypush/backend/http/templates/plugins/media/index.html b/platypush/backend/http/templates/plugins/media/index.html
index 5c22e0097..40760f376 100644
--- a/platypush/backend/http/templates/plugins/media/index.html
+++ b/platypush/backend/http/templates/plugins/media/index.html
@@ -27,7 +27,6 @@
 
             <div class="col-2 top-buttons">
                 <button type="button"
-                        v-if="torrentsDownloading"
                         @click="torrentModal.visible = !torrentModal.visible"
                         title="Show torrents progress">
                     <i class="fa fa-download"></i>
diff --git a/platypush/backend/http/templates/plugins/media/torrents.html b/platypush/backend/http/templates/plugins/media/torrents.html
index 90b1e6694..703f6d490 100644
--- a/platypush/backend/http/templates/plugins/media/torrents.html
+++ b/platypush/backend/http/templates/plugins/media/torrents.html
@@ -2,6 +2,15 @@
 
 <script type="text/x-template" id="tmpl-media-torrents">
     <div class="transfers-container">
+        <div class="search">
+            <form @submit.prevent="onMagnetDownload">
+                <input type="text" ref="magnetLink" placeholder="Magnet link">
+                <button type="submit">
+                    <i class="fas fa-download"></i>
+                </button>
+            </form>
+        </div>
+
         <div class="head">
             <div class="col-4">Name</div>
             <div class="col-2">State</div>