2017-06-23 18:38:37 +03:00
|
|
|
import os.path
|
|
|
|
from urllib.parse import quote
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-06-23 18:38:37 +03:00
|
|
|
import flask
|
|
|
|
|
2017-07-24 23:10:36 +03:00
|
|
|
from nyaa import api_handler, app, db, forms, models, template_utils, torrents, views
|
|
|
|
from nyaa.backend import get_category_id_map
|
2017-05-14 02:01:26 -06:00
|
|
|
|
2017-05-12 20:51:49 +02:00
|
|
|
DEBUG_API = False
|
|
|
|
|
|
|
|
|
2017-05-19 15:24:09 +03:00
|
|
|
@app.template_global()
|
|
|
|
def category_name(cat_id):
|
|
|
|
''' Given a category id (eg. 1_2), returns a category name (eg. Anime - English-translated) '''
|
|
|
|
return ' - '.join(get_category_id_map().get(cat_id, ['???']))
|
|
|
|
|
|
|
|
|
2017-05-18 15:30:03 +03:00
|
|
|
# Routes start here #
|
|
|
|
|
2017-05-22 23:01:23 +02:00
|
|
|
@app.route('/view/<int:torrent_id>/comment/<int:comment_id>/delete', methods=['POST'])
|
2017-05-14 09:52:57 +10:00
|
|
|
def delete_comment(torrent_id, comment_id):
|
2017-05-22 23:01:23 +02:00
|
|
|
if not flask.g.user:
|
|
|
|
flask.abort(403)
|
2017-05-26 16:11:31 +03:00
|
|
|
torrent = models.Torrent.by_id(torrent_id)
|
|
|
|
if not torrent:
|
|
|
|
flask.abort(404)
|
2017-05-22 23:01:23 +02:00
|
|
|
|
|
|
|
comment = models.Comment.query.filter_by(id=comment_id).first()
|
|
|
|
if not comment:
|
|
|
|
flask.abort(404)
|
|
|
|
|
|
|
|
if not (comment.user.id == flask.g.user.id or flask.g.user.is_moderator):
|
2017-05-14 09:52:57 +10:00
|
|
|
flask.abort(403)
|
2017-05-22 18:35:48 +02:00
|
|
|
|
2017-05-22 23:01:23 +02:00
|
|
|
db.session.delete(comment)
|
2017-05-26 16:11:31 +03:00
|
|
|
db.session.flush()
|
|
|
|
torrent.update_comment_count()
|
2017-07-04 23:13:59 -05:00
|
|
|
|
2017-07-11 01:00:03 +03:00
|
|
|
url = flask.url_for('torrents.view', torrent_id=torrent.id)
|
2017-07-04 23:13:59 -05:00
|
|
|
if flask.g.user.is_moderator:
|
|
|
|
log = "Comment deleted on torrent [#{}]({})".format(torrent.id, url)
|
|
|
|
adminlog = models.AdminLog(log=log, admin_id=flask.g.user.id)
|
|
|
|
db.session.add(adminlog)
|
2017-05-22 23:01:23 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
flask.flash('Comment successfully deleted.', 'success')
|
|
|
|
|
2017-07-04 23:13:59 -05:00
|
|
|
return flask.redirect(url)
|
2017-05-14 09:52:57 +10:00
|
|
|
|
2017-05-13 22:24:42 +10:00
|
|
|
|
2017-05-12 20:51:49 +02:00
|
|
|
@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')
|
2017-05-25 11:15:45 +03:00
|
|
|
@app.route('/download/<int:torrent_id>.torrent')
|
2017-05-12 20:51:49 +02:00
|
|
|
def download_torrent(torrent_id):
|
|
|
|
torrent = models.Torrent.by_id(torrent_id)
|
|
|
|
|
2017-05-22 20:08:41 +02:00
|
|
|
if not torrent or not torrent.has_torrent:
|
2017-05-12 20:51:49 +02:00
|
|
|
flask.abort(404)
|
|
|
|
|
2017-07-20 11:00:20 +03:00
|
|
|
torrent_file, torrent_file_size = _get_cached_torrent_file(torrent)
|
|
|
|
disposition = 'attachment; filename="{0}"; filename*=UTF-8\'\'{0}'.format(
|
2017-05-12 20:51:49 +02:00
|
|
|
quote(torrent.torrent_name.encode('utf-8')))
|
|
|
|
|
2017-07-20 11:00:20 +03:00
|
|
|
resp = flask.Response(torrent_file)
|
|
|
|
resp.headers['Content-Type'] = 'application/x-bittorrent'
|
|
|
|
resp.headers['Content-Disposition'] = disposition
|
|
|
|
resp.headers['Content-Length'] = torrent_file_size
|
2017-05-12 20:51:49 +02:00
|
|
|
return resp
|
|
|
|
|
|
|
|
|
2017-05-19 20:03:47 +03:00
|
|
|
@app.route('/view/<int:torrent_id>/submit_report', methods=['POST'])
|
|
|
|
def submit_report(torrent_id):
|
2017-05-20 13:33:58 +03:00
|
|
|
if not flask.g.user:
|
|
|
|
flask.abort(403)
|
|
|
|
|
2017-05-19 20:03:47 +03:00
|
|
|
form = forms.ReportForm(flask.request.form)
|
|
|
|
|
|
|
|
if flask.request.method == 'POST' and form.validate():
|
2017-05-20 13:33:58 +03:00
|
|
|
report_reason = form.reason.data
|
|
|
|
current_user_id = flask.g.user.id
|
|
|
|
report = models.Report(
|
|
|
|
torrent_id=torrent_id,
|
|
|
|
user_id=current_user_id,
|
|
|
|
reason=report_reason)
|
|
|
|
|
|
|
|
db.session.add(report)
|
|
|
|
db.session.commit()
|
|
|
|
flask.flash('Successfully reported torrent!', 'success')
|
2017-05-19 20:03:47 +03:00
|
|
|
|
2017-07-11 01:00:03 +03:00
|
|
|
return flask.redirect(flask.url_for('torrents.view', torrent_id=torrent_id))
|
2017-05-19 20:03:47 +03:00
|
|
|
|
|
|
|
|
2017-05-12 20:51:49 +02:00
|
|
|
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))
|
|
|
|
|
2017-07-20 11:00:20 +03:00
|
|
|
return open(cached_torrent, 'rb'), os.path.getsize(cached_torrent)
|
2017-05-12 20:51:49 +02:00
|
|
|
|
|
|
|
|
2017-07-11 00:13:19 +03:00
|
|
|
# #################################### BLUEPRINTS ####################################
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-07-11 00:13:19 +03:00
|
|
|
def register_blueprints(flask_app):
|
|
|
|
""" Register the blueprints using the flask_app object """
|
2017-05-20 01:13:04 +02:00
|
|
|
|
2017-07-23 22:30:41 +03:00
|
|
|
# Template filters and globals
|
|
|
|
flask_app.register_blueprint(template_utils.bp)
|
2017-07-11 00:13:19 +03:00
|
|
|
# API routes
|
|
|
|
flask_app.register_blueprint(api_handler.api_blueprint, url_prefix='/api')
|
|
|
|
# Site routes
|
2017-07-20 21:01:25 +03:00
|
|
|
flask_app.register_blueprint(views.account_bp)
|
2017-07-08 00:48:28 +03:00
|
|
|
flask_app.register_blueprint(views.admin_bp)
|
2017-07-11 00:44:10 +03:00
|
|
|
flask_app.register_blueprint(views.main_bp)
|
2017-07-11 00:13:19 +03:00
|
|
|
flask_app.register_blueprint(views.site_bp)
|
2017-07-11 01:00:03 +03:00
|
|
|
flask_app.register_blueprint(views.torrents_bp)
|
2017-07-08 00:50:55 +03:00
|
|
|
flask_app.register_blueprint(views.users_bp)
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-22 22:54:33 +03:00
|
|
|
|
2017-07-11 00:13:19 +03:00
|
|
|
# When done, this can be moved to nyaa/__init__.py instead of importing this file
|
|
|
|
register_blueprints(app)
|