mirror of
https://github.com/searxng/searxng
synced 2024-01-01 19:24:07 +01:00
Pass tests
This commit is contained in:
parent
54993ad2fa
commit
d5ce6a27bb
1 changed files with 76 additions and 77 deletions
|
@ -299,7 +299,7 @@ def get_result_template(theme_name: str, template_name: str):
|
|||
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
|
||||
# It is used to not repeat code for /favicon.ico and /logo
|
||||
# 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))
|
||||
relpath = os.path.join(app.root_path, path)
|
||||
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
|
||||
'favicon.png' if imgtype == "favicon" else "searxng.png",
|
||||
mimetype=mimetype
|
||||
) )
|
||||
mimetype=mimetype,
|
||||
)
|
||||
|
||||
# If path is a URL
|
||||
if "://" in path:
|
||||
resp_ok = False
|
||||
resp = None
|
||||
# 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:
|
||||
# Pull image from it
|
||||
request_headers = {
|
||||
|
@ -342,6 +344,7 @@ def get_favicon_or_logo(imgtype: str, request):
|
|||
resp.close()
|
||||
except httpx.HTTPError:
|
||||
logger.exception(f'{imgtype}: HTTP error on closing')
|
||||
|
||||
def close_stream():
|
||||
nonlocal resp, stream
|
||||
try:
|
||||
|
@ -351,6 +354,7 @@ def get_favicon_or_logo(imgtype: str, request):
|
|||
del stream
|
||||
except httpx.HTTPError as e:
|
||||
logger.debug(f'{imgtype}Exception while closing response', e)
|
||||
|
||||
try:
|
||||
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)
|
||||
|
@ -360,19 +364,14 @@ def get_favicon_or_logo(imgtype: str, request):
|
|||
close_stream()
|
||||
return fallback
|
||||
|
||||
|
||||
|
||||
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
|
||||
return fallback
|
||||
else:
|
||||
# Otherwise we're good to go, send whatever is specified.
|
||||
# Works with both relative and absolute.
|
||||
return send_from_directory(
|
||||
os.path.dirname(path),
|
||||
os.path.basename(path),
|
||||
mimetype=mimetype
|
||||
)
|
||||
return send_from_directory(os.path.dirname(path), os.path.basename(path), mimetype=mimetype)
|
||||
|
||||
|
||||
def custom_url_for(endpoint: str, **values):
|
||||
suffix = ""
|
||||
|
@ -1380,12 +1379,12 @@ def opensearch():
|
|||
def favicon():
|
||||
return get_favicon_or_logo("favicon", request)
|
||||
|
||||
|
||||
@app.route('/logo')
|
||||
def logo():
|
||||
return get_favicon_or_logo("logo", request)
|
||||
|
||||
|
||||
|
||||
@app.route('/clear_cookies')
|
||||
def clear_cookies():
|
||||
resp = make_response(redirect(url_for('index', _external=True)))
|
||||
|
|
Loading…
Add table
Reference in a new issue