diff --git a/nyaa/backend.py b/nyaa/backend.py index 4d79203..a0d6600 100644 --- a/nyaa/backend.py +++ b/nyaa/backend.py @@ -160,12 +160,13 @@ def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False): upload_form.ratelimit.errors = ["You've gone over the upload ratelimit."] raise TorrentExtraValidationException() - # Delete exisiting torrent which is marked as deleted + # Delete existing torrent which is marked as deleted if torrent_data.db_id is not None: old_torrent = models.Torrent.by_id(torrent_data.db_id) - _delete_torrent_file(old_torrent) db.session.delete(old_torrent) db.session.commit() + # Delete physical file after transaction has been committed + _delete_info_dict(old_torrent) # The torrent has been validated and is safe to access with ['foo'] etc - all relevant # keys and values have been checked for (see UploadForm in forms.py for details) @@ -199,8 +200,7 @@ def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False): 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) + os.makedirs(info_dict_dir, exist_ok=True) with open(info_dict_path, 'wb') as out_file: out_file.write(torrent_data.bencoded_info_dict) @@ -330,8 +330,7 @@ def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False): torrent_file.seek(0, 0) torrent_dir = app.config['BACKUP_TORRENT_FOLDER'] - if not os.path.exists(torrent_dir): - os.makedirs(torrent_dir) + os.makedirs(torrent_dir, exist_ok=True) torrent_path = os.path.join(torrent_dir, '{}.{}'.format( torrent.id, secure_filename(torrent_file.filename))) @@ -370,7 +369,7 @@ def tracker_api(info_hashes, method): return True -def _delete_torrent_file(torrent): +def _delete_info_dict(torrent): 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 2f5ac86..f687996 100644 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -229,9 +229,8 @@ class TorrentBase(DeclarativeHelperBase): 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) + return os.path.join(app.config['BASE_DIR'], 'info_dicts', + info_hash[0:2], info_hash[2:4], info_hash) @property def info_hash_as_b32(self): @@ -239,7 +238,7 @@ class TorrentBase(DeclarativeHelperBase): @property def info_hash_as_hex(self): - return self.info_hash.hex().lower() + return self.info_hash.hex() @property def magnet_uri(self): diff --git a/nyaa/torrents.py b/nyaa/torrents.py index 1a8e277..0b91489 100644 --- a/nyaa/torrents.py +++ b/nyaa/torrents.py @@ -1,8 +1,8 @@ import base64 import os -import time from urllib.parse import urlencode +import flask from flask import current_app as app from orderedset import OrderedSet @@ -100,8 +100,10 @@ def create_default_metadata_base(torrent, trackers=None, webseeds=None): metadata_base = { 'created by': 'NyaaV2', - 'creation date': int(time.time()), - 'comment': 'NyaaV2 Torrent #' + str(torrent.id), + 'creation date': int(torrent.created_utc_timestamp), + 'comment': flask.url_for('torrents.view', + torrent_id=torrent.id, + _external=True) # 'encoding' : 'UTF-8' # It's almost always UTF-8 and expected, but if it isn't... }