diff --git a/nyaa/backend.py b/nyaa/backend.py index 95ecef8..4d79203 100644 --- a/nyaa/backend.py +++ b/nyaa/backend.py @@ -162,9 +162,9 @@ def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False): # Delete exisiting torrent which is marked as deleted if torrent_data.db_id is not None: - oldtorrent = models.Torrent.by_id(torrent_data.db_id) - _delete_torrent_file(oldtorrent) - db.session.delete(oldtorrent) + old_torrent = models.Torrent.by_id(torrent_data.db_id) + _delete_torrent_file(old_torrent) + db.session.delete(old_torrent) db.session.commit() # The torrent has been validated and is safe to access with ['foo'] etc - all relevant @@ -196,14 +196,14 @@ def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False): uploader_ip=ip_address(flask.request.remote_addr).packed) # Store bencoded info_dict - info_hash = torrent_data.info_hash.hex().lower() - path = os.path.join(app.config['BASE_DIR'], 'info_dicts', - info_hash[0:2], info_hash[2:4]) - if not os.path.exists(path): - os.makedirs(path) - path = os.path.join(path, info_hash) - with open(path, 'wb') as fp: - fp.write(torrent_data.bencoded_info_dict) + info_dict_path = torrent.info_dict_path + + info_dict_dir = os.path.dirname(info_dict_path) + if not os.path.exists(info_dict_dir): + os.makedirs(info_dict_dir) + + with open(info_dict_path, 'wb') as out_file: + out_file.write(torrent_data.bencoded_info_dict) torrent.stats = models.Statistic() torrent.has_torrent = True @@ -371,9 +371,6 @@ def tracker_api(info_hashes, method): def _delete_torrent_file(torrent): - info_hash = torrent.info_hash_as_hex - path = os.path.join(app.config['BASE_DIR'], 'info_dicts', - info_hash[0:2], info_hash[2:4], info_hash) - - if os.path.exists(path): - os.remove(path) + info_dict_path = torrent.info_dict_path + if os.path.exists(info_dict_path): + os.remove(info_dict_path) diff --git a/nyaa/models.py b/nyaa/models.py index b2fc216..8d44597 100644 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -224,6 +224,14 @@ class TorrentBase(DeclarativeHelperBase): # Escaped return escape_markup(self.information) + @property + def info_dict_path(self): + ''' Returns a path to the info_dict file in form of 'info_dicts/aa/bb/aabbccddee...' ''' + info_hash = self.info_hash_as_hex + info_dict_dir = os.path.join(app.config['BASE_DIR'], 'info_dicts', + info_hash[0:2], info_hash[2:4]) + return os.path.join(info_dict_dir, info_hash) + @property def info_hash_as_b32(self): return base64.b32encode(self.info_hash).decode('utf-8') diff --git a/nyaa/views/torrents.py b/nyaa/views/torrents.py index ac377f0..4d9e803 100644 --- a/nyaa/views/torrents.py +++ b/nyaa/views/torrents.py @@ -474,13 +474,9 @@ def _create_upload_category_choices(): def _make_torrent_file(torrent): - info_hash = torrent.info_hash_as_hex - path = os.path.join(app.config['BASE_DIR'], 'info_dicts', - info_hash[0:2], info_hash[2:4], info_hash) + with open(torrent.info_dict_path, 'rb') as in_file: + bencoded_info = in_file.read() - with open(path, 'rb') as fp: - bencoded_info = fp.read() + bencoded_torrent_data = torrents.create_bencoded_torrent(torrent, bencoded_info) - data = torrents.create_bencoded_torrent(torrent, bencoded_info) - - return data, len(data) + return bencoded_torrent_data, len(bencoded_torrent_data)