Pass tests

This commit is contained in:
Solirs 2023-03-22 22:03:51 +01:00
parent 54993ad2fa
commit d5ce6a27bb

View file

@ -299,7 +299,7 @@ def get_result_template(theme_name: str, template_name: str):
return 'result_templates/' + template_name return 'result_templates/' + template_name
def get_favicon_or_logo(imgtype: str, request): def get_favicon_or_logo(imgtype: str):
# This method returns a favicon or regular image depending on the imgtype parameter # This method returns a favicon or regular image depending on the imgtype parameter
# It is used to not repeat code for /favicon.ico and /logo # It is used to not repeat code for /favicon.ico and /logo
# Called by logo() and favicon() # Called by logo() and favicon()
@ -307,17 +307,19 @@ def get_favicon_or_logo(imgtype: str, request):
path = os.path.expanduser(settings.get("brand").get(imgtype)) path = os.path.expanduser(settings.get("brand").get(imgtype))
relpath = os.path.join(app.root_path, path) relpath = os.path.join(app.root_path, path)
mimetype = 'image/vnd.microsoft.icon' if imgtype == "favicon" else None mimetype = 'image/vnd.microsoft.icon' if imgtype == "favicon" else None
fallback = ( send_from_directory( fallback = send_from_directory(
os.path.join(app.root_path, settings['ui']['static_path'], 'themes', theme, 'img'), # pyright: ignore os.path.join(app.root_path, settings['ui']['static_path'], 'themes', theme, 'img'), # pyright: ignore
'favicon.png' if imgtype == "favicon" else "searxng.png", 'favicon.png' if imgtype == "favicon" else "searxng.png",
mimetype=mimetype mimetype=mimetype,
) ) )
# If path is a URL # If path is a URL
if "://" in path: if "://" in path:
resp_ok = False resp_ok = False
resp = None resp = None
# TODO: Make a a function for this used by both this function and the image proxy # TODO: Make a a function for this used by both this function and the image proxy
# ASAP to avoid repeated code like this
# pylint: disable=fixme
try: try:
# Pull image from it # Pull image from it
request_headers = { request_headers = {
@ -342,6 +344,7 @@ def get_favicon_or_logo(imgtype: str, request):
resp.close() resp.close()
except httpx.HTTPError: except httpx.HTTPError:
logger.exception(f'{imgtype}: HTTP error on closing') logger.exception(f'{imgtype}: HTTP error on closing')
def close_stream(): def close_stream():
nonlocal resp, stream nonlocal resp, stream
try: try:
@ -351,6 +354,7 @@ def get_favicon_or_logo(imgtype: str, request):
del stream del stream
except httpx.HTTPError as e: except httpx.HTTPError as e:
logger.debug(f'{imgtype}Exception while closing response', e) logger.debug(f'{imgtype}Exception while closing response', e)
try: try:
headers = dict_subset(resp.headers, {'Content-Type', 'Content-Encoding', 'Content-Length', 'Length'}) headers = dict_subset(resp.headers, {'Content-Type', 'Content-Encoding', 'Content-Length', 'Length'})
response = Response(stream, mimetype=resp.headers['Content-Type'], headers=headers, direct_passthrough=True) response = Response(stream, mimetype=resp.headers['Content-Type'], headers=headers, direct_passthrough=True)
@ -360,19 +364,14 @@ def get_favicon_or_logo(imgtype: str, request):
close_stream() close_stream()
return fallback return fallback
elif not os.path.isfile(path) and not os.path.isfile(relpath): elif not os.path.isfile(path) and not os.path.isfile(relpath):
# If path doesn't exist neither relatively nor absolutely, fallback to whatever is in the theme # If path doesn't exist neither relatively nor absolutely, fallback to whatever is in the theme
return fallback return fallback
else: else:
# Otherwise we're good to go, send whatever is specified. # Otherwise we're good to go, send whatever is specified.
# Works with both relative and absolute. # Works with both relative and absolute.
return send_from_directory( return send_from_directory(os.path.dirname(path), os.path.basename(path), mimetype=mimetype)
os.path.dirname(path),
os.path.basename(path),
mimetype=mimetype
)
def custom_url_for(endpoint: str, **values): def custom_url_for(endpoint: str, **values):
suffix = "" suffix = ""
@ -1380,12 +1379,12 @@ def opensearch():
def favicon(): def favicon():
return get_favicon_or_logo("favicon", request) return get_favicon_or_logo("favicon", request)
@app.route('/logo') @app.route('/logo')
def logo(): def logo():
return get_favicon_or_logo("logo", request) return get_favicon_or_logo("logo", request)
@app.route('/clear_cookies') @app.route('/clear_cookies')
def clear_cookies(): def clear_cookies():
resp = make_response(redirect(url_for('index', _external=True))) resp = make_response(redirect(url_for('index', _external=True)))