Name fixes, DRY

This commit is contained in:
TheAMM 2017-08-30 19:20:02 +03:00 committed by nyaadev
parent fd0a02b95c
commit a92d886b5c
3 changed files with 26 additions and 25 deletions

View File

@ -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)

View File

@ -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')

View File

@ -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)