From ef56e54521b4201baaaed5270ec2401e302e40b3 Mon Sep 17 00:00:00 2001 From: Kfir Hadas Date: Mon, 24 Jul 2017 23:10:59 +0300 Subject: [PATCH] Move torrent magnet and torrent download into 'torrents' blueprint --- nyaa/routes.py | 45 +----------------------------- nyaa/templates/rss.xml | 4 +-- nyaa/templates/search_results.html | 2 +- nyaa/templates/view.html | 2 +- nyaa/views/torrents.py | 44 ++++++++++++++++++++++++++++- 5 files changed, 48 insertions(+), 49 deletions(-) diff --git a/nyaa/routes.py b/nyaa/routes.py index 1227d7b..b597733 100644 --- a/nyaa/routes.py +++ b/nyaa/routes.py @@ -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//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//torrent') -@app.route('/download/.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//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): diff --git a/nyaa/templates/rss.xml b/nyaa/templates/rss.xml index 9201c25..d9c8732 100644 --- a/nyaa/templates/rss.xml +++ b/nyaa/templates/rss.xml @@ -10,7 +10,7 @@ {% if use_elastic %} {# ElasticSearch Torrent instances #} {% if torrent.has_torrent and not magnet_links %} - {{ url_for('download_torrent', torrent_id=torrent.meta.id, _external=True) }} + {{ url_for('torrents.download', torrent_id=torrent.meta.id, _external=True) }} {% else %} {{ create_magnet_from_es_info(torrent.display_name, torrent.info_hash) }} {% endif %} @@ -24,7 +24,7 @@ {% else %} {# Database Torrent rows #} {% if torrent.has_torrent and not magnet_links %} - {{ url_for('download_torrent', torrent_id=torrent.id, _external=True) }} + {{ url_for('torrents.download', torrent_id=torrent.id, _external=True) }} {% else %} {{ torrent.magnet_uri }} {% endif %} diff --git a/nyaa/templates/search_results.html b/nyaa/templates/search_results.html index 095d09f..51a230a 100644 --- a/nyaa/templates/search_results.html +++ b/nyaa/templates/search_results.html @@ -84,7 +84,7 @@ {% endif %} - {% if torrent.has_torrent %}{% endif %} + {% if torrent.has_torrent %}{% endif %} {% if use_elastic %} {% else %} diff --git a/nyaa/templates/view.html b/nyaa/templates/view.html index a0d44eb..291974f 100644 --- a/nyaa/templates/view.html +++ b/nyaa/templates/view.html @@ -73,7 +73,7 @@