@@ -81,11 +81,12 @@ def create_torrent_file(file_path_list, params, torrent_filepath=None):
81
81
# filter all non-files
82
82
path_list = list (_existing_files (file_path_list ))
83
83
84
+ # ACHTUNG!
85
+ # In the case of a multi-file torrent, the torrent name plays the role of the toplevel dir name.
84
86
# 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 ()
87
88
for path in path_list :
88
- relative = path .relative_to (base_path )
89
+ relative = path .relative_to (base_dir )
89
90
fs .add_file (str (relative ), path .size ())
90
91
91
92
if params .get (b'piece length' ):
@@ -102,7 +103,6 @@ def create_torrent_file(file_path_list, params, torrent_filepath=None):
102
103
params = {k : (v .decode ('utf-8' ) if isinstance (v , bytes ) else v ) for k , v in params .items ()}
103
104
104
105
torrent = lt .create_torrent (fs , piece_size = piece_size , flags = flags )
105
- # Python2 wants binary, python3 want unicode
106
106
if params .get (b'comment' ):
107
107
torrent .set_comment (params [b'comment' ])
108
108
if params .get (b'created by' ):
@@ -133,7 +133,7 @@ def create_torrent_file(file_path_list, params, torrent_filepath=None):
133
133
torrent .add_url_seed (params [b'urllist' ])
134
134
135
135
# 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 ))
137
137
138
138
t1 = torrent .generate ()
139
139
torrent = lt .bencode (t1 )
@@ -144,8 +144,7 @@ def create_torrent_file(file_path_list, params, torrent_filepath=None):
144
144
145
145
return {
146
146
'success' : True ,
147
- 'base_path' : base_path ,
148
- 'base_dir' : base_path .parent ,
147
+ 'base_dir' : base_dir ,
149
148
'torrent_file_path' : torrent_filepath ,
150
149
'metainfo' : torrent ,
151
150
'infohash' : sha1 (lt .bencode (t1 [b'info' ])).digest ()
0 commit comments