This commit is contained in:
Solaris 2023-05-29 19:01:27 +02:00 committed by GitHub
commit 7eaa794787
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 9 deletions

View file

@ -14,6 +14,9 @@ general:
enable_metrics: true enable_metrics: true
brand: brand:
# Both favicon and logo are relative to SearXNG's root path unless they are absolute paths
favicon: static/themes/simple/img/favicon.png
logo: static/themes/simple/img/searxng.png
new_issue_url: https://github.com/searxng/searxng/issues/new new_issue_url: https://github.com/searxng/searxng/issues/new
docs_url: https://docs.searxng.org/ docs_url: https://docs.searxng.org/
public_instances: https://searx.space public_instances: https://searx.space

View file

@ -146,6 +146,8 @@ SCHEMA = {
'enable_metrics': SettingsValue(bool, True), 'enable_metrics': SettingsValue(bool, True),
}, },
'brand': { 'brand': {
'favicon': SettingsValue(str, "static/themes/simple/img/favicon.png"),
'logo': SettingsValue(str, "static/themes/simple/img/searxg.png"),
'issue_url': SettingsValue(str, 'https://github.com/searxng/searxng/issues'), 'issue_url': SettingsValue(str, 'https://github.com/searxng/searxng/issues'),
'new_issue_url': SettingsValue(str, 'https://github.com/searxng/searxng/issues/new'), 'new_issue_url': SettingsValue(str, 'https://github.com/searxng/searxng/issues/new'),
'docs_url': SettingsValue(str, 'https://docs.searxng.org'), 'docs_url': SettingsValue(str, 'https://docs.searxng.org'),

View file

@ -24,8 +24,8 @@
{% block head %} {% block head %}
<link title="{{ instance_name }}" type="application/opensearchdescription+xml" rel="search" href="{{ opensearch_url }}"/> <link title="{{ instance_name }}" type="application/opensearchdescription+xml" rel="search" href="{{ opensearch_url }}"/>
{% endblock %} {% endblock %}
<link rel="icon" href="{{ url_for('static', filename='img/favicon.png') }}" sizes="any"> <link rel="icon" href="/favicon.ico" sizes="any">
<link rel="icon" href="{{ url_for('static', filename='img/favicon.svg') }}" type="image/svg+xml"> <link rel="icon" href="/favicon.ico" type="image/svg+xml">
</head> </head>
<body class="{{ endpoint }}_endpoint" > <body class="{{ endpoint }}_endpoint" >
<main id="main_{{ self._TemplateReference__context.name|replace("simple/", "")|replace(".html", "") }}" class="{{body_class}}"> <main id="main_{{ self._TemplateReference__context.name|replace("simple/", "")|replace(".html", "") }}" class="{{body_class}}">

View file

@ -1,5 +1,5 @@
{% set body_class = "page_with_header" %} {% set body_class = "page_with_header" %}
{% extends "simple/base.html" %} {% extends "simple/base.html" %}
{% block header %} {% block header %}
<a href="{{ url_for('index') }}"><img class="logo" src="{{ url_for('static', filename='img/searxng.png') }}" alt="SearXNG"></a> <a href="{{ url_for('index') }}"><img class="logo" src="/logo" alt="SearXNG"></a>
{% endblock %} {% endblock %}

View file

@ -294,6 +294,79 @@ 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):
# 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()
theme = request.preferences.get_value("theme")
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(
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,
)
# 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 # pylint: disable=fixme
# ASAP to avoid repeated code like this
try:
# Pull image from it
request_headers = {
'User-Agent': gen_useragent(),
'Accept': 'image/webp,*/*',
'Accept-Encoding': 'gzip, deflate',
'Sec-GPC': '1',
'DNT': '1',
}
resp, stream = http_stream(method='GET', url=path, headers=request_headers, allow_redirects=True)
if resp.status_code != 200:
logger.debug(f"{imgtype}: Bad status code %i", resp.status_code)
return fallback
resp_ok = True
except httpx.HTTPError:
logger.debug(f"{imgtype}: HTTP error")
return fallback
finally:
if resp and not resp_ok:
try:
resp.close()
except httpx.HTTPError:
logger.exception(f'{imgtype}: HTTP error on closing')
def close_stream():
nonlocal resp, stream
try:
if resp:
resp.close()
del resp
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)
response.call_on_close(close_stream)
return response
except httpx.HTTPError:
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)
def custom_url_for(endpoint: str, **values): def custom_url_for(endpoint: str, **values):
suffix = "" suffix = ""
if endpoint == 'static' and values.get('filename'): if endpoint == 'static' and values.get('filename'):
@ -1286,12 +1359,12 @@ def opensearch():
@app.route('/favicon.ico') @app.route('/favicon.ico')
def favicon(): def favicon():
theme = request.preferences.get_value("theme") return get_favicon_or_logo("favicon")
return send_from_directory(
os.path.join(app.root_path, settings['ui']['static_path'], 'themes', theme, 'img'), # pyright: ignore
'favicon.png', @app.route('/logo')
mimetype='image/vnd.microsoft.icon', def logo():
) return get_favicon_or_logo("logo")
@app.route('/clear_cookies') @app.route('/clear_cookies')