Skip to content

Commit

Permalink
Merge pull request #6689 from ichorid/fix/create_torrent_from_dir
Browse files Browse the repository at this point in the history
Fix creating torrent from dir
  • Loading branch information
ichorid authored Dec 27, 2021
2 parents b9d177a + 9a11564 commit 480b592
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand All @@ -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'):
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"]


Expand All @@ -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"]


Expand Down

0 comments on commit 480b592

Please sign in to comment.