[Core] Assign magnet arg if magnet URI in filename

A magnet that prefetched the metadata is added as a normal torrent but
with the filename set to the magnet URI. So we need to assign the URI to
magnet arg and set filename to None before creating Torrent object.

Updated the is_magnet function to prevent AttributeError with None type.
This commit is contained in:
Calum Lind 2018-09-24 15:20:33 +01:00
parent 57ea5ef5da
commit 23f1cfc926
3 changed files with 10 additions and 4 deletions

View File

@ -672,10 +672,10 @@ def is_magnet(uri):
True True
""" """
if not uri:
return False
if uri.startswith(MAGNET_SCHEME) and XT_BTIH_PARAM in uri: return uri.startswith(MAGNET_SCHEME) and XT_BTIH_PARAM in uri
return True
return False
def get_magnet_info(uri): def get_magnet_info(uri):

View File

@ -23,7 +23,7 @@ from twisted.internet.task import LoopingCall
import deluge.component as component import deluge.component as component
from deluge._libtorrent import lt from deluge._libtorrent import lt
from deluge.common import archive_files, decode_bytes, get_magnet_info from deluge.common import archive_files, decode_bytes, get_magnet_info, is_magnet
from deluge.configmanager import ConfigManager, get_config_dir from deluge.configmanager import ConfigManager, get_config_dir
from deluge.core.authmanager import AUTH_LEVEL_ADMIN from deluge.core.authmanager import AUTH_LEVEL_ADMIN
from deluge.core.torrent import Torrent, TorrentOptions, sanitize_filepath from deluge.core.torrent import Torrent, TorrentOptions, sanitize_filepath
@ -555,6 +555,11 @@ class TorrentManager(component.Component):
return d return d
def _add_torrent_obj(self, handle, options, state, filename, magnet, resume_data, filedump, save_state): def _add_torrent_obj(self, handle, options, state, filename, magnet, resume_data, filedump, save_state):
# For magnets added with metadata, filename is used so set as magnet.
if not magnet and is_magnet(filename):
magnet = filename
filename = None
# Create a Torrent object and add to the dictionary. # Create a Torrent object and add to the dictionary.
torrent = Torrent(handle, options, state, filename, magnet) torrent = Torrent(handle, options, state, filename, magnet)
self.torrents[torrent.torrent_id] = torrent self.torrents[torrent.torrent_id] = torrent

View File

@ -74,6 +74,7 @@ class CommonTestCase(unittest.TestCase):
def test_is_magnet(self): def test_is_magnet(self):
self.assertTrue(is_magnet('magnet:?xt=urn:btih:SU5225URMTUEQLDXQWRB2EQWN6KLTYKN')) self.assertTrue(is_magnet('magnet:?xt=urn:btih:SU5225URMTUEQLDXQWRB2EQWN6KLTYKN'))
self.assertFalse(is_magnet(None))
def test_is_infohash(self): def test_is_infohash(self):
self.assertTrue(is_infohash('2dc5d0e71a66fe69649a640d39cb00a259704973')) self.assertTrue(is_infohash('2dc5d0e71a66fe69649a640d39cb00a259704973'))