diff --git a/deluge/common.py b/deluge/common.py index 73c89daa5..cde0d3c2e 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -448,7 +448,9 @@ def is_magnet(uri): True """ - if uri[:20] == "magnet:?xt=urn:btih:": + magnet_scheme = 'magnet:?' + xt_param = 'xt=urn:btih:' + if uri.startswith(magnet_scheme) and xt_param in uri: return True return False diff --git a/deluge/ui/web/js/deluge-all/add/UrlWindow.js b/deluge/ui/web/js/deluge-all/add/UrlWindow.js index 3aba36c1f..8a8a1c1de 100644 --- a/deluge/ui/web/js/deluge-all/add/UrlWindow.js +++ b/deluge/ui/web/js/deluge-all/add/UrlWindow.js @@ -81,7 +81,7 @@ Deluge.add.UrlWindow = Ext.extend(Deluge.add.Window, { var cookies = this.cookieField.getValue(); var torrentId = this.createTorrentId(); - if (url.substring(0,20) == 'magnet:?xt=urn:btih:') { + if (url.indexOf('magnet:?') == 0 && url.indexOf('xt=urn:btih') > -1) { deluge.client.web.get_magnet_info(url, { success: this.onGotInfo, scope: this, diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py index 8b0fc9473..a4b8cc73b 100644 --- a/deluge/ui/web/json_api.py +++ b/deluge/ui/web/json_api.py @@ -722,24 +722,29 @@ class WebApi(JSONComponent): :rtype: dictionary """ - try: - s = uri.split("&")[0][20:] - if len(s) == 32: - info_hash = base64.b32decode(s).encode("hex") - elif len(s) == 40: - info_hash = s - else: - return False + magnet_scheme = 'magnet:?' + xt_param = 'xt=urn:btih:' + dn_param = 'dn=' + if uri.startswith(magnet_scheme): name = None - for i in uri.split("&")[1:]: - if i[:3] == "dn=": - name = unquote_plus(i.split("=")[1]) - if not name: - name = info_hash - return {"name":name, "info_hash":info_hash, "files_tree":''} - except Exception, e: - log.exception(e) - return False + info_hash = None + for param in uri[len(magnet_scheme):].split('&'): + if param.startswith(xt_param): + xt_hash = param[len(xt_param):] + if len(xt_hash) == 32: + info_hash = base64.b32decode(xt_hash).encode("hex") + elif len(xt_hash) == 40: + info_hash = xt_hash + else: + break + elif param.startswith(dn_param): + name = unquote_plus(param[len(dn_param):]) + + if info_hash: + if not name: + name = info_hash + return {"name":name, "info_hash":info_hash, "files_tree":''} + return False @export def add_torrents(self, torrents):