Fix flat PR (#446)

* Clean up PR #349 

- Rely on os.makedirs(..., exist_ok=True) for "thread"-safety

- Remove the previous info_dict when we know the transaction went through.

- bytes.hex() will always be lowercase (unless we go off CPython):
c3d9508ff2/Python/pystrhex.c (L5-L49)
c3d9508ff2/Python/codecs.c (L16)

- Reintroduce comments and meaningful creation dates in generated torrents:
Also make create_default_metadata_base set the correct metadata now
This commit is contained in:
Anna-Maria Meriniemi 2018-02-04 14:56:29 +02:00 committed by A nyaa developer
parent f38d7e0707
commit e5fe63156d
3 changed files with 14 additions and 14 deletions

View File

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

View File

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

View File

@ -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...
}