Skip to content

Commit 480b592

Browse files
authored
Merge pull request #6689 from ichorid/fix/create_torrent_from_dir
Fix creating torrent from dir
2 parents b9d177a + 9a11564 commit 480b592

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

src/tribler-core/tribler_core/components/libtorrent/restapi/create_torrent_endpoint.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async def create_torrent(self, request):
111111
# Download this torrent if specified
112112
if 'download' in request.query and request.query['download'] and request.query['download'] == "1":
113113
download_config = DownloadConfig()
114-
download_config.set_dest_dir(result['base_path'] if len(file_path_list) == 1 else result['base_dir'])
114+
download_config.set_dest_dir(result['base_dir'])
115115
download_config.set_hops(self.download_manager.download_defaults.number_hops)
116116
self.download_manager.start_download(tdef=TorrentDef(metainfo_dict), config=download_config)
117117

src/tribler-core/tribler_core/components/libtorrent/utils/torrent_utils.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ def create_torrent_file(file_path_list, params, torrent_filepath=None):
8181
# filter all non-files
8282
path_list = list(_existing_files(file_path_list))
8383

84+
# ACHTUNG!
85+
# In the case of a multi-file torrent, the torrent name plays the role of the toplevel dir name.
8486
# get the directory where these files are in. If there are multiple files, take the common directory they are in
85-
base_path = commonprefix(path_list).parent if len(path_list) > 1 else path_list[0].parent
86-
base_path = base_path.absolute()
87+
base_dir = (commonprefix(path_list).parent if len(path_list) > 1 else path_list[0].parent).absolute()
8788
for path in path_list:
88-
relative = path.relative_to(base_path)
89+
relative = path.relative_to(base_dir)
8990
fs.add_file(str(relative), path.size())
9091

9192
if params.get(b'piece length'):
@@ -102,7 +103,6 @@ def create_torrent_file(file_path_list, params, torrent_filepath=None):
102103
params = {k: (v.decode('utf-8') if isinstance(v, bytes) else v) for k, v in params.items()}
103104

104105
torrent = lt.create_torrent(fs, piece_size=piece_size, flags=flags)
105-
# Python2 wants binary, python3 want unicode
106106
if params.get(b'comment'):
107107
torrent.set_comment(params[b'comment'])
108108
if params.get(b'created by'):
@@ -133,7 +133,7 @@ def create_torrent_file(file_path_list, params, torrent_filepath=None):
133133
torrent.add_url_seed(params[b'urllist'])
134134

135135
# read the files and calculate the hashes
136-
lt.set_piece_hashes(torrent, str(base_path))
136+
lt.set_piece_hashes(torrent, str(base_dir))
137137

138138
t1 = torrent.generate()
139139
torrent = lt.bencode(t1)
@@ -144,8 +144,7 @@ def create_torrent_file(file_path_list, params, torrent_filepath=None):
144144

145145
return {
146146
'success': True,
147-
'base_path': base_path,
148-
'base_dir': base_path.parent,
147+
'base_dir': base_dir,
149148
'torrent_file_path': torrent_filepath,
150149
'metainfo': torrent,
151150
'infohash': sha1(lt.bencode(t1[b'info'])).digest()

src/tribler-core/tribler_core/utilities/tests/test_torrent_utils.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import pytest
44

5+
from tribler_core.components.libtorrent.utils.torrent_utils import (
6+
commonprefix,
7+
create_torrent_file,
8+
get_info_from_handle,
9+
)
510
from tribler_core.tests.tools.common import TESTS_DATA_DIR
611
from tribler_core.utilities.path_util import Path
7-
from tribler_core.components.libtorrent.utils.torrent_utils import commonprefix, create_torrent_file, \
8-
get_info_from_handle
912

1013
TORRENT_DATA_DIR = TESTS_DATA_DIR / "torrent_creation_files"
1114
FILE1_NAME = "file1.txt"
@@ -21,7 +24,7 @@ def get_params():
2124

2225
def verify_created_torrent(result):
2326
assert isinstance(result, dict)
24-
assert result["base_path"] == TORRENT_DATA_DIR
27+
assert result["base_dir"] == TORRENT_DATA_DIR
2528
assert result["success"]
2629

2730

@@ -46,6 +49,7 @@ def test_create_torrent_two_files():
4649
file_path_list = [TORRENT_DATA_DIR / FILE1_NAME,
4750
TORRENT_DATA_DIR / FILE2_NAME]
4851
result = create_torrent_file(file_path_list, get_params())
52+
assert result['base_dir'] == TORRENT_DATA_DIR.parent
4953
assert result["success"]
5054

5155

0 commit comments

Comments
 (0)