[enh] image proxy base

This commit is contained in:
Adam Tauber 2015-01-16 16:02:21 +01:00
parent 306c644fa7
commit 60eb831966
1 changed files with 36 additions and 1 deletions

View File

@ -27,6 +27,7 @@ import cStringIO
import os import os
from datetime import datetime, timedelta from datetime import datetime, timedelta
from requests import get as http_get
from itertools import chain from itertools import chain
from flask import ( from flask import (
Flask, request, render_template, url_for, Response, make_response, Flask, request, render_template, url_for, Response, make_response,
@ -39,7 +40,7 @@ from searx.engines import (
) )
from searx.utils import ( from searx.utils import (
UnicodeWriter, highlight_content, html_to_text, get_themes, UnicodeWriter, highlight_content, html_to_text, get_themes,
get_static_files, get_result_templates get_static_files, get_result_templates, gen_useragent
) )
from searx.version import VERSION_STRING from searx.version import VERSION_STRING
from searx.languages import language_codes from searx.languages import language_codes
@ -531,6 +532,40 @@ def preferences():
theme=get_current_theme_name()) theme=get_current_theme_name())
@app.route('/image_proxy', methods=['GET'])
def image_proxy():
url = request.args.get('url')
if not url:
return '', 400
resp = http_get(url,
stream=True,
timeout=settings['server'].get('request_timeout', 2),
headers={'User-Agent': gen_useragent()})
if resp.status_code != 200:
logger.debug('image-proxy: wrong response code: {0}'.format(resp.status_code))
if resp.status_code >= 400:
return '', resp.status_code
return '', 400
if not resp.headers.get('content-type', '').startswith('image/'):
logger.debug('image-proxy: wrong content-type: {0}'.format(resp.get('content-type')))
return '', 400
img = ''
chunk_counter = 0
for chunk in resp.iter_content(1024*1024):
chunk_counter += 1
if chunk_counter > 5:
return '', 502 # Bad gateway - file is too big (>5M)
img += chunk
return Response(img, mimetype=resp.headers['content-type'])
@app.route('/stats', methods=['GET']) @app.route('/stats', methods=['GET'])
def stats(): def stats():
"""Render engine statistics page.""" """Render engine statistics page."""