diff --git a/src/tribler-core/tribler_core/components/libtorrent/restapi/create_torrent_endpoint.py b/src/tribler-core/tribler_core/components/libtorrent/restapi/create_torrent_endpoint.py index 078b3c95dcf..03d7ba3d601 100644 --- a/src/tribler-core/tribler_core/components/libtorrent/restapi/create_torrent_endpoint.py +++ b/src/tribler-core/tribler_core/components/libtorrent/restapi/create_torrent_endpoint.py @@ -111,7 +111,7 @@ async def create_torrent(self, request): # Download this torrent if specified if 'download' in request.query and request.query['download'] and request.query['download'] == "1": download_config = DownloadConfig() - download_config.set_dest_dir(result['base_path'] if len(file_path_list) == 1 else result['base_dir']) + download_config.set_dest_dir(result['base_dir']) download_config.set_hops(self.download_manager.download_defaults.number_hops) self.download_manager.start_download(tdef=TorrentDef(metainfo_dict), config=download_config) diff --git a/src/tribler-core/tribler_core/components/libtorrent/utils/torrent_utils.py b/src/tribler-core/tribler_core/components/libtorrent/utils/torrent_utils.py index 1848726aed1..6f7efca8e02 100644 --- a/src/tribler-core/tribler_core/components/libtorrent/utils/torrent_utils.py +++ b/src/tribler-core/tribler_core/components/libtorrent/utils/torrent_utils.py @@ -81,11 +81,12 @@ def create_torrent_file(file_path_list, params, torrent_filepath=None): # filter all non-files path_list = list(_existing_files(file_path_list)) + # ACHTUNG! + # In the case of a multi-file torrent, the torrent name plays the role of the toplevel dir name. # get the directory where these files are in. If there are multiple files, take the common directory they are in - base_path = commonprefix(path_list).parent if len(path_list) > 1 else path_list[0].parent - base_path = base_path.absolute() + base_dir = (commonprefix(path_list).parent if len(path_list) > 1 else path_list[0].parent).absolute() for path in path_list: - relative = path.relative_to(base_path) + relative = path.relative_to(base_dir) fs.add_file(str(relative), path.size()) if params.get(b'piece length'): @@ -102,7 +103,6 @@ def create_torrent_file(file_path_list, params, torrent_filepath=None): params = {k: (v.decode('utf-8') if isinstance(v, bytes) else v) for k, v in params.items()} torrent = lt.create_torrent(fs, piece_size=piece_size, flags=flags) - # Python2 wants binary, python3 want unicode if params.get(b'comment'): torrent.set_comment(params[b'comment']) if params.get(b'created by'): @@ -133,7 +133,7 @@ def create_torrent_file(file_path_list, params, torrent_filepath=None): torrent.add_url_seed(params[b'urllist']) # read the files and calculate the hashes - lt.set_piece_hashes(torrent, str(base_path)) + lt.set_piece_hashes(torrent, str(base_dir)) t1 = torrent.generate() torrent = lt.bencode(t1) @@ -144,8 +144,7 @@ def create_torrent_file(file_path_list, params, torrent_filepath=None): return { 'success': True, - 'base_path': base_path, - 'base_dir': base_path.parent, + 'base_dir': base_dir, 'torrent_file_path': torrent_filepath, 'metainfo': torrent, 'infohash': sha1(lt.bencode(t1[b'info'])).digest() diff --git a/src/tribler-core/tribler_core/utilities/tests/test_torrent_utils.py b/src/tribler-core/tribler_core/utilities/tests/test_torrent_utils.py index 01734ccbb27..4bafeb5204d 100644 --- a/src/tribler-core/tribler_core/utilities/tests/test_torrent_utils.py +++ b/src/tribler-core/tribler_core/utilities/tests/test_torrent_utils.py @@ -2,10 +2,13 @@ import pytest +from tribler_core.components.libtorrent.utils.torrent_utils import ( + commonprefix, + create_torrent_file, + get_info_from_handle, +) from tribler_core.tests.tools.common import TESTS_DATA_DIR from tribler_core.utilities.path_util import Path -from tribler_core.components.libtorrent.utils.torrent_utils import commonprefix, create_torrent_file, \ - get_info_from_handle TORRENT_DATA_DIR = TESTS_DATA_DIR / "torrent_creation_files" FILE1_NAME = "file1.txt" @@ -21,7 +24,7 @@ def get_params(): def verify_created_torrent(result): assert isinstance(result, dict) - assert result["base_path"] == TORRENT_DATA_DIR + assert result["base_dir"] == TORRENT_DATA_DIR assert result["success"] @@ -46,6 +49,7 @@ def test_create_torrent_two_files(): file_path_list = [TORRENT_DATA_DIR / FILE1_NAME, TORRENT_DATA_DIR / FILE2_NAME] result = create_torrent_file(file_path_list, get_params()) + assert result['base_dir'] == TORRENT_DATA_DIR.parent assert result["success"]