mirror of
https://gitlab.com/SIGBUS/nyaa.git
synced 2025-01-25 00:25:12 +00:00
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:
parent
f38d7e0707
commit
e5fe63156d
|
@ -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."]
|
upload_form.ratelimit.errors = ["You've gone over the upload ratelimit."]
|
||||||
raise TorrentExtraValidationException()
|
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:
|
if torrent_data.db_id is not None:
|
||||||
old_torrent = models.Torrent.by_id(torrent_data.db_id)
|
old_torrent = models.Torrent.by_id(torrent_data.db_id)
|
||||||
_delete_torrent_file(old_torrent)
|
|
||||||
db.session.delete(old_torrent)
|
db.session.delete(old_torrent)
|
||||||
db.session.commit()
|
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
|
# 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)
|
# 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_path = torrent.info_dict_path
|
||||||
|
|
||||||
info_dict_dir = os.path.dirname(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, exist_ok=True)
|
||||||
os.makedirs(info_dict_dir)
|
|
||||||
|
|
||||||
with open(info_dict_path, 'wb') as out_file:
|
with open(info_dict_path, 'wb') as out_file:
|
||||||
out_file.write(torrent_data.bencoded_info_dict)
|
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_file.seek(0, 0)
|
||||||
|
|
||||||
torrent_dir = app.config['BACKUP_TORRENT_FOLDER']
|
torrent_dir = app.config['BACKUP_TORRENT_FOLDER']
|
||||||
if not os.path.exists(torrent_dir):
|
os.makedirs(torrent_dir, exist_ok=True)
|
||||||
os.makedirs(torrent_dir)
|
|
||||||
|
|
||||||
torrent_path = os.path.join(torrent_dir, '{}.{}'.format(
|
torrent_path = os.path.join(torrent_dir, '{}.{}'.format(
|
||||||
torrent.id, secure_filename(torrent_file.filename)))
|
torrent.id, secure_filename(torrent_file.filename)))
|
||||||
|
@ -370,7 +369,7 @@ def tracker_api(info_hashes, method):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def _delete_torrent_file(torrent):
|
def _delete_info_dict(torrent):
|
||||||
info_dict_path = torrent.info_dict_path
|
info_dict_path = torrent.info_dict_path
|
||||||
if os.path.exists(info_dict_path):
|
if os.path.exists(info_dict_path):
|
||||||
os.remove(info_dict_path)
|
os.remove(info_dict_path)
|
||||||
|
|
|
@ -229,9 +229,8 @@ class TorrentBase(DeclarativeHelperBase):
|
||||||
def info_dict_path(self):
|
def info_dict_path(self):
|
||||||
''' Returns a path to the info_dict file in form of 'info_dicts/aa/bb/aabbccddee...' '''
|
''' Returns a path to the info_dict file in form of 'info_dicts/aa/bb/aabbccddee...' '''
|
||||||
info_hash = self.info_hash_as_hex
|
info_hash = self.info_hash_as_hex
|
||||||
info_dict_dir = os.path.join(app.config['BASE_DIR'], 'info_dicts',
|
return os.path.join(app.config['BASE_DIR'], 'info_dicts',
|
||||||
info_hash[0:2], info_hash[2:4])
|
info_hash[0:2], info_hash[2:4], info_hash)
|
||||||
return os.path.join(info_dict_dir, info_hash)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def info_hash_as_b32(self):
|
def info_hash_as_b32(self):
|
||||||
|
@ -239,7 +238,7 @@ class TorrentBase(DeclarativeHelperBase):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def info_hash_as_hex(self):
|
def info_hash_as_hex(self):
|
||||||
return self.info_hash.hex().lower()
|
return self.info_hash.hex()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def magnet_uri(self):
|
def magnet_uri(self):
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import base64
|
import base64
|
||||||
import os
|
import os
|
||||||
import time
|
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
|
import flask
|
||||||
from flask import current_app as app
|
from flask import current_app as app
|
||||||
|
|
||||||
from orderedset import OrderedSet
|
from orderedset import OrderedSet
|
||||||
|
@ -100,8 +100,10 @@ def create_default_metadata_base(torrent, trackers=None, webseeds=None):
|
||||||
|
|
||||||
metadata_base = {
|
metadata_base = {
|
||||||
'created by': 'NyaaV2',
|
'created by': 'NyaaV2',
|
||||||
'creation date': int(time.time()),
|
'creation date': int(torrent.created_utc_timestamp),
|
||||||
'comment': 'NyaaV2 Torrent #' + str(torrent.id),
|
'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...
|
# 'encoding' : 'UTF-8' # It's almost always UTF-8 and expected, but if it isn't...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue