1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-22 08:49:59 +00:00

Move torrent magnet and torrent download into 'torrents' blueprint

This commit is contained in:
Kfir Hadas 2017-07-24 23:10:59 +03:00
parent 9fef343c1b
commit ef56e54521
5 changed files with 48 additions and 49 deletions

View file

@ -1,9 +1,6 @@
import os.path
from urllib.parse import quote
import flask 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 from nyaa.backend import get_category_id_map
DEBUG_API = False DEBUG_API = False
@ -48,35 +45,6 @@ def delete_comment(torrent_id, comment_id):
return flask.redirect(url) 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']) @app.route('/view/<int:torrent_id>/submit_report', methods=['POST'])
def submit_report(torrent_id): def submit_report(torrent_id):
if not flask.g.user: 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)) 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 #################################### # #################################### BLUEPRINTS ####################################
def register_blueprints(flask_app): def register_blueprints(flask_app):

View file

@ -10,7 +10,7 @@
{% if use_elastic %} {% if use_elastic %}
{# ElasticSearch Torrent instances #} {# ElasticSearch Torrent instances #}
{% if torrent.has_torrent and not magnet_links %} {% 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 %} {% else %}
<link>{{ create_magnet_from_es_info(torrent.display_name, torrent.info_hash) }}</link> <link>{{ create_magnet_from_es_info(torrent.display_name, torrent.info_hash) }}</link>
{% endif %} {% endif %}
@ -24,7 +24,7 @@
{% else %} {% else %}
{# Database Torrent rows #} {# Database Torrent rows #}
{% if torrent.has_torrent and not magnet_links %} {% 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 %} {% else %}
<link>{{ torrent.magnet_uri }}</link> <link>{{ torrent.magnet_uri }}</link>
{% endif %} {% endif %}

View file

@ -84,7 +84,7 @@
{% endif %} {% endif %}
</td> </td>
<td class="text-center" style="white-space: nowrap;"> <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 %} {% 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> <a href="{{ create_magnet_from_es_info(torrent.display_name, torrent.info_hash) }}"><i class="fa fa-fw fa-magnet"></i></a>
{% else %} {% else %}

View file

@ -73,7 +73,7 @@
</div><!--/.panel-body --> </div><!--/.panel-body -->
<div class="panel-footer clearfix"> <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 %} {% if g.user %}
<button type="button" class="btn btn-xs btn-danger pull-right" data-toggle="modal" data-target="#reportModal"> <button type="button" class="btn btn-xs btn-danger pull-right" data-toggle="modal" data-target="#reportModal">
Report Report

View file

@ -1,11 +1,13 @@
import json import json
import os.path
from urllib.parse import quote
import flask import flask
from werkzeug.datastructures import CombinedMultiDict from werkzeug.datastructures import CombinedMultiDict
from sqlalchemy.orm import joinedload 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 from nyaa.utils import cached_function
bp = flask.Blueprint('torrents', __name__) bp = flask.Blueprint('torrents', __name__)
@ -146,6 +148,35 @@ def edit_torrent(torrent_id):
torrent=torrent) 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']) @bp.route('/upload', methods=['GET', 'POST'])
def upload(): def upload():
upload_form = forms.UploadForm(CombinedMultiDict((flask.request.files, flask.request.form))) 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) cat_name = ' - '.join(cat_names)
choices.append((key, cat_name, is_main_cat)) choices.append((key, cat_name, is_main_cat))
return choices 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)