mirror of
https://gitlab.com/SIGBUS/nyaa.git
synced 2024-11-01 00:05:55 +00:00
Move torrent magnet and torrent download into 'torrents' blueprint
This commit is contained in:
parent
9fef343c1b
commit
ef56e54521
|
@ -1,9 +1,6 @@
|
|||
import os.path
|
||||
from urllib.parse import quote
|
||||
|
||||
import flask
|
||||
|
||||
from nyaa import api_handler, app, db, forms, models, template_utils, torrents, views
|
||||
from nyaa import api_handler, app, db, forms, models, template_utils, views
|
||||
from nyaa.backend import get_category_id_map
|
||||
|
||||
DEBUG_API = False
|
||||
|
@ -48,35 +45,6 @@ def delete_comment(torrent_id, comment_id):
|
|||
return flask.redirect(url)
|
||||
|
||||
|
||||
@app.route('/view/<int:torrent_id>/magnet')
|
||||
def redirect_magnet(torrent_id):
|
||||
torrent = models.Torrent.by_id(torrent_id)
|
||||
|
||||
if not torrent:
|
||||
flask.abort(404)
|
||||
|
||||
return flask.redirect(torrents.create_magnet(torrent))
|
||||
|
||||
|
||||
@app.route('/view/<int:torrent_id>/torrent')
|
||||
@app.route('/download/<int:torrent_id>.torrent')
|
||||
def download_torrent(torrent_id):
|
||||
torrent = models.Torrent.by_id(torrent_id)
|
||||
|
||||
if not torrent or not torrent.has_torrent:
|
||||
flask.abort(404)
|
||||
|
||||
torrent_file, torrent_file_size = _get_cached_torrent_file(torrent)
|
||||
disposition = 'attachment; filename="{0}"; filename*=UTF-8\'\'{0}'.format(
|
||||
quote(torrent.torrent_name.encode('utf-8')))
|
||||
|
||||
resp = flask.Response(torrent_file)
|
||||
resp.headers['Content-Type'] = 'application/x-bittorrent'
|
||||
resp.headers['Content-Disposition'] = disposition
|
||||
resp.headers['Content-Length'] = torrent_file_size
|
||||
return resp
|
||||
|
||||
|
||||
@app.route('/view/<int:torrent_id>/submit_report', methods=['POST'])
|
||||
def submit_report(torrent_id):
|
||||
if not flask.g.user:
|
||||
|
@ -99,17 +67,6 @@ def submit_report(torrent_id):
|
|||
return flask.redirect(flask.url_for('torrents.view', torrent_id=torrent_id))
|
||||
|
||||
|
||||
def _get_cached_torrent_file(torrent):
|
||||
# Note: obviously temporary
|
||||
cached_torrent = os.path.join(app.config['BASE_DIR'],
|
||||
'torrent_cache', str(torrent.id) + '.torrent')
|
||||
if not os.path.exists(cached_torrent):
|
||||
with open(cached_torrent, 'wb') as out_file:
|
||||
out_file.write(torrents.create_bencoded_torrent(torrent))
|
||||
|
||||
return open(cached_torrent, 'rb'), os.path.getsize(cached_torrent)
|
||||
|
||||
|
||||
# #################################### BLUEPRINTS ####################################
|
||||
|
||||
def register_blueprints(flask_app):
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
{% if use_elastic %}
|
||||
{# ElasticSearch Torrent instances #}
|
||||
{% if torrent.has_torrent and not magnet_links %}
|
||||
<link>{{ url_for('download_torrent', torrent_id=torrent.meta.id, _external=True) }}</link>
|
||||
<link>{{ url_for('torrents.download', torrent_id=torrent.meta.id, _external=True) }}</link>
|
||||
{% else %}
|
||||
<link>{{ create_magnet_from_es_info(torrent.display_name, torrent.info_hash) }}</link>
|
||||
{% endif %}
|
||||
|
@ -24,7 +24,7 @@
|
|||
{% else %}
|
||||
{# Database Torrent rows #}
|
||||
{% if torrent.has_torrent and not magnet_links %}
|
||||
<link>{{ url_for('download_torrent', torrent_id=torrent.id, _external=True) }}</link>
|
||||
<link>{{ url_for('torrents.download', torrent_id=torrent.id, _external=True) }}</link>
|
||||
{% else %}
|
||||
<link>{{ torrent.magnet_uri }}</link>
|
||||
{% endif %}
|
||||
|
|
|
@ -84,7 +84,7 @@
|
|||
{% endif %}
|
||||
</td>
|
||||
<td class="text-center" style="white-space: nowrap;">
|
||||
{% if torrent.has_torrent %}<a href="{{ url_for('download_torrent', torrent_id=torrent_id) }}"><i class="fa fa-fw fa-download"></i></a>{% endif %}
|
||||
{% if torrent.has_torrent %}<a href="{{ url_for('torrents.download', torrent_id=torrent_id) }}"><i class="fa fa-fw fa-download"></i></a>{% endif %}
|
||||
{% if use_elastic %}
|
||||
<a href="{{ create_magnet_from_es_info(torrent.display_name, torrent.info_hash) }}"><i class="fa fa-fw fa-magnet"></i></a>
|
||||
{% else %}
|
||||
|
|
|
@ -73,7 +73,7 @@
|
|||
</div><!--/.panel-body -->
|
||||
|
||||
<div class="panel-footer clearfix">
|
||||
{% if torrent.has_torrent %}<a href="{{ url_for('download_torrent', torrent_id=torrent.id )}}"><i class="fa fa-download fa-fw"></i>Download Torrent</a> or {% endif %}<a href="{{ torrent.magnet_uri }}" class="card-footer-item"><i class="fa fa-magnet fa-fw"></i>Magnet</a>
|
||||
{% if torrent.has_torrent %}<a href="{{ url_for('torrents.download', torrent_id=torrent.id )}}"><i class="fa fa-download fa-fw"></i>Download Torrent</a> or {% endif %}<a href="{{ torrent.magnet_uri }}" class="card-footer-item"><i class="fa fa-magnet fa-fw"></i>Magnet</a>
|
||||
{% if g.user %}
|
||||
<button type="button" class="btn btn-xs btn-danger pull-right" data-toggle="modal" data-target="#reportModal">
|
||||
Report
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
import json
|
||||
import os.path
|
||||
from urllib.parse import quote
|
||||
|
||||
import flask
|
||||
from werkzeug.datastructures import CombinedMultiDict
|
||||
|
||||
from sqlalchemy.orm import joinedload
|
||||
|
||||
from nyaa import backend, db, forms, models
|
||||
from nyaa import app, backend, db, forms, models, torrents
|
||||
from nyaa.utils import cached_function
|
||||
|
||||
bp = flask.Blueprint('torrents', __name__)
|
||||
|
@ -146,6 +148,35 @@ def edit_torrent(torrent_id):
|
|||
torrent=torrent)
|
||||
|
||||
|
||||
@bp.route('/view/<int:torrent_id>/magnet')
|
||||
def redirect_magnet(torrent_id):
|
||||
torrent = models.Torrent.by_id(torrent_id)
|
||||
|
||||
if not torrent:
|
||||
flask.abort(404)
|
||||
|
||||
return flask.redirect(torrents.create_magnet(torrent))
|
||||
|
||||
|
||||
@bp.route('/view/<int:torrent_id>/torrent')
|
||||
@bp.route('/download/<int:torrent_id>.torrent', endpoint='download')
|
||||
def download_torrent(torrent_id):
|
||||
torrent = models.Torrent.by_id(torrent_id)
|
||||
|
||||
if not torrent or not torrent.has_torrent:
|
||||
flask.abort(404)
|
||||
|
||||
torrent_file, torrent_file_size = _get_cached_torrent_file(torrent)
|
||||
disposition = 'attachment; filename="{0}"; filename*=UTF-8\'\'{0}'.format(
|
||||
quote(torrent.torrent_name.encode('utf-8')))
|
||||
|
||||
resp = flask.Response(torrent_file)
|
||||
resp.headers['Content-Type'] = 'application/x-bittorrent'
|
||||
resp.headers['Content-Disposition'] = disposition
|
||||
resp.headers['Content-Length'] = torrent_file_size
|
||||
return resp
|
||||
|
||||
|
||||
@bp.route('/upload', methods=['GET', 'POST'])
|
||||
def upload():
|
||||
upload_form = forms.UploadForm(CombinedMultiDict((flask.request.files, flask.request.form)))
|
||||
|
@ -175,3 +206,14 @@ def _create_upload_category_choices():
|
|||
cat_name = ' - '.join(cat_names)
|
||||
choices.append((key, cat_name, is_main_cat))
|
||||
return choices
|
||||
|
||||
|
||||
def _get_cached_torrent_file(torrent):
|
||||
# Note: obviously temporary
|
||||
cached_torrent = os.path.join(app.config['BASE_DIR'],
|
||||
'torrent_cache', str(torrent.id) + '.torrent')
|
||||
if not os.path.exists(cached_torrent):
|
||||
with open(cached_torrent, 'wb') as out_file:
|
||||
out_file.write(torrents.create_bencoded_torrent(torrent))
|
||||
|
||||
return open(cached_torrent, 'rb'), os.path.getsize(cached_torrent)
|
||||
|
|
Loading…
Reference in a new issue