mirror of
https://github.com/sb745/NyaaV3.git
synced 2025-03-12 22:06:55 +02:00
Name fixes, DRY
This commit is contained in:
parent
fd0a02b95c
commit
a92d886b5c
3 changed files with 26 additions and 25 deletions
|
@ -162,9 +162,9 @@ def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False):
|
||||||
|
|
||||||
# Delete exisiting torrent which is marked as deleted
|
# Delete exisiting torrent which is marked as deleted
|
||||||
if torrent_data.db_id is not None:
|
if torrent_data.db_id is not None:
|
||||||
oldtorrent = models.Torrent.by_id(torrent_data.db_id)
|
old_torrent = models.Torrent.by_id(torrent_data.db_id)
|
||||||
_delete_torrent_file(oldtorrent)
|
_delete_torrent_file(old_torrent)
|
||||||
db.session.delete(oldtorrent)
|
db.session.delete(old_torrent)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
# The torrent has been validated and is safe to access with ['foo'] etc - all relevant
|
# 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)
|
uploader_ip=ip_address(flask.request.remote_addr).packed)
|
||||||
|
|
||||||
# Store bencoded info_dict
|
# Store bencoded info_dict
|
||||||
info_hash = torrent_data.info_hash.hex().lower()
|
info_dict_path = torrent.info_dict_path
|
||||||
path = os.path.join(app.config['BASE_DIR'], 'info_dicts',
|
|
||||||
info_hash[0:2], info_hash[2:4])
|
info_dict_dir = os.path.dirname(info_dict_path)
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(info_dict_dir):
|
||||||
os.makedirs(path)
|
os.makedirs(info_dict_dir)
|
||||||
path = os.path.join(path, info_hash)
|
|
||||||
with open(path, 'wb') as fp:
|
with open(info_dict_path, 'wb') as out_file:
|
||||||
fp.write(torrent_data.bencoded_info_dict)
|
out_file.write(torrent_data.bencoded_info_dict)
|
||||||
|
|
||||||
torrent.stats = models.Statistic()
|
torrent.stats = models.Statistic()
|
||||||
torrent.has_torrent = True
|
torrent.has_torrent = True
|
||||||
|
@ -371,9 +371,6 @@ def tracker_api(info_hashes, method):
|
||||||
|
|
||||||
|
|
||||||
def _delete_torrent_file(torrent):
|
def _delete_torrent_file(torrent):
|
||||||
info_hash = torrent.info_hash_as_hex
|
info_dict_path = torrent.info_dict_path
|
||||||
path = os.path.join(app.config['BASE_DIR'], 'info_dicts',
|
if os.path.exists(info_dict_path):
|
||||||
info_hash[0:2], info_hash[2:4], info_hash)
|
os.remove(info_dict_path)
|
||||||
|
|
||||||
if os.path.exists(path):
|
|
||||||
os.remove(path)
|
|
||||||
|
|
|
@ -224,6 +224,14 @@ class TorrentBase(DeclarativeHelperBase):
|
||||||
# Escaped
|
# Escaped
|
||||||
return escape_markup(self.information)
|
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
|
@property
|
||||||
def info_hash_as_b32(self):
|
def info_hash_as_b32(self):
|
||||||
return base64.b32encode(self.info_hash).decode('utf-8')
|
return base64.b32encode(self.info_hash).decode('utf-8')
|
||||||
|
|
|
@ -474,13 +474,9 @@ def _create_upload_category_choices():
|
||||||
|
|
||||||
|
|
||||||
def _make_torrent_file(torrent):
|
def _make_torrent_file(torrent):
|
||||||
info_hash = torrent.info_hash_as_hex
|
with open(torrent.info_dict_path, 'rb') as in_file:
|
||||||
path = os.path.join(app.config['BASE_DIR'], 'info_dicts',
|
bencoded_info = in_file.read()
|
||||||
info_hash[0:2], info_hash[2:4], info_hash)
|
|
||||||
|
|
||||||
with open(path, 'rb') as fp:
|
bencoded_torrent_data = torrents.create_bencoded_torrent(torrent, bencoded_info)
|
||||||
bencoded_info = fp.read()
|
|
||||||
|
|
||||||
data = torrents.create_bencoded_torrent(torrent, bencoded_info)
|
return bencoded_torrent_data, len(bencoded_torrent_data)
|
||||||
|
|
||||||
return data, len(data)
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue