diff --git a/bazarr/api.py b/bazarr/api.py index fbe3de5ef..a6011524d 100644 --- a/bazarr/api.py +++ b/bazarr/api.py @@ -6,7 +6,7 @@ import datetime import pretty from get_args import args -from config import settings +from config import settings, base_url from init import * import logging @@ -19,7 +19,7 @@ from flask import Flask, jsonify, request, Response, Blueprint from flask_restful import Resource, Api -api_bp = Blueprint('api', __name__, url_prefix='/api') +api_bp = Blueprint('api', __name__, url_prefix=base_url.rstrip('/')+'/api') api = Api(api_bp) diff --git a/bazarr/main.py b/bazarr/main.py index 5994d92de..4398d1c6c 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -44,7 +44,8 @@ from io import BytesIO from six import text_type, PY2 from datetime import timedelta from get_languages import load_language_in_db, language_from_alpha3, language_from_alpha2, alpha2_from_alpha3 -from flask import Flask, make_response, request, redirect, abort, render_template, Response, session, flash, url_for, send_file +from flask import Flask, make_response, request, redirect, abort, render_template, Response, session, flash, url_for, \ + send_file, stream_with_context from get_providers import get_providers, get_providers_auth, list_throttled_providers from get_series import * @@ -65,27 +66,23 @@ from flask_debugtoolbar import DebugToolbarExtension from functools import wraps -class PrefixMiddleware(object): - - def __init__(self, app, prefix=''): - self.app = app - self.prefix = prefix - - def __call__(self, environ, start_response): - - if environ['PATH_INFO'].startswith(self.prefix): - environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):] - environ['SCRIPT_NAME'] = self.prefix - return self.app(environ, start_response) - else: - start_response('404', [('Content-Type', 'text/plain')]) - return ["This url does not belong to the app.".encode()] +def prefix_route(route_function, prefix='', mask='{0}{1}'): + # Defines a new route function with a prefix. + # The mask argument is a `format string` formatted with, in that order: prefix, route + def newroute(route, *args, **kwargs): + # New function to prefix the route + return route_function(mask.format(prefix, route), *args, **kwargs) + return newroute # Flask Setup app = Flask(__name__, template_folder=os.path.join(os.path.dirname(__file__), '..', 'views'), static_folder=os.path.join(os.path.dirname(__file__), '..', 'static')) -app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=base_url) +app.route = prefix_route(app.route, base_url.rstrip('/')) + +@app.errorhandler(404) +def http_error_handler(error): + return redirect(base_url.rstrip('/')), 302 app.config["SECRET_KEY"] = 'test' @@ -511,17 +508,13 @@ def download_log(): @login_required def image_proxy(url): apikey = settings.sonarr.apikey - url_image = url_sonarr_short() + '/' + url + '?apikey=' + apikey + url_image = (url_sonarr_short() + '/' + url + '?apikey=' + apikey).replace('poster-250', 'poster-500') try: - image_buffer = BytesIO( - requests.get(url_sonarr() + '/api' + url_image.split(url_sonarr())[1], timeout=15, verify=False).content) + req = requests.get(url_sonarr() + '/api' + url_image.split(url_sonarr())[1], stream=True, timeout=15, verify=False) except: return None else: - image_buffer.seek(0) - bytes = image_buffer.read() - request.set_header('Content-type', 'image/jpeg') - return bytes + return Response(stream_with_context(req.iter_content(2048)), content_type=req.headers['content-type']) @app.route('/image_proxy_movies/', methods=['GET']) @@ -530,17 +523,12 @@ def image_proxy_movies(url): apikey = settings.radarr.apikey try: url_image = (url_radarr_short() + '/' + url + '?apikey=' + apikey).replace('/fanart.jpg', '/banner.jpg') - image_buffer = BytesIO( - requests.get(url_radarr() + '/api' + url_image.split(url_radarr())[1], timeout=15, verify=False).content) + req = requests.get(url_radarr() + '/api' + url_image.split(url_radarr())[1], stream=True, timeout=15, verify=False) except: url_image = url_radarr_short() + '/' + url + '?apikey=' + apikey - image_buffer = BytesIO( - requests.get(url_radarr() + '/api' + url_image.split(url_radarr())[1], timeout=15, verify=False).content) + req = requests.get(url_radarr() + '/api' + url_image.split(url_radarr())[1], stream=True, timeout=15, verify=False) else: - image_buffer.seek(0) - bytes = image_buffer.read() - request.set_header('Content-type', 'image/jpeg') - return bytes + return Response(stream_with_context(req.iter_content(2048)), content_type=req.headers['content-type']) @app.route("/")