From 7377f82db658cc3432eddc954b2a136861f97403 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Wed, 9 Jul 2025 17:26:23 +0200 Subject: [PATCH] Favicon type detection - support for autodetecting mimetype for better reliability --- .github/test/Dockerfile-alpine | 1 + Dockerfile | 2 ++ changedetectionio/api/Watch.py | 13 +++++++++++-- changedetectionio/flask_app.py | 13 +++++++++++-- requirements.txt | 3 +++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/.github/test/Dockerfile-alpine b/.github/test/Dockerfile-alpine index 132391671..f9d424d17 100644 --- a/.github/test/Dockerfile-alpine +++ b/.github/test/Dockerfile-alpine @@ -18,6 +18,7 @@ RUN \ libxslt-dev \ openssl-dev \ python3-dev \ + file \ zip \ zlib-dev && \ apk add --update --no-cache \ diff --git a/Dockerfile b/Dockerfile index 573173893..5f3906500 100644 --- a/Dockerfile +++ b/Dockerfile @@ -53,6 +53,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ locales \ # For pdftohtml poppler-utils \ + # favicon type detection and other uses + file \ zlib1g \ && apt-get clean && rm -rf /var/lib/apt/lists/* diff --git a/changedetectionio/api/Watch.py b/changedetectionio/api/Watch.py index c9a1def1b..da074e50a 100644 --- a/changedetectionio/api/Watch.py +++ b/changedetectionio/api/Watch.py @@ -214,8 +214,17 @@ class WatchFavicon(Resource): favicon_filename = watch.get_favicon_filename() if favicon_filename: - import mimetypes - mime, encoding = mimetypes.guess_type(favicon_filename) + try: + import magic + mime = magic.from_file( + os.path.join(watch.watch_data_dir, favicon_filename), + mime=True + ) + except ImportError: + # Fallback, no python-magic + import mimetypes + mime, encoding = mimetypes.guess_type(favicon_filename) + response = make_response(send_from_directory(watch.watch_data_dir, favicon_filename)) response.headers['Content-type'] = mime response.headers['Cache-Control'] = 'max-age=300, must-revalidate' # Cache for 5 minutes, then revalidate diff --git a/changedetectionio/flask_app.py b/changedetectionio/flask_app.py index fb80f97f3..252c71002 100644 --- a/changedetectionio/flask_app.py +++ b/changedetectionio/flask_app.py @@ -438,8 +438,17 @@ def changedetection_app(config=None, datastore_o=None): favicon_filename = watch.get_favicon_filename() if favicon_filename: - import mimetypes - mime, encoding = mimetypes.guess_type(favicon_filename) + try: + import magic + mime = magic.from_file( + os.path.join(watch.watch_data_dir, favicon_filename), + mime=True + ) + except ImportError: + # Fallback, no python-magic + import mimetypes + mime, encoding = mimetypes.guess_type(favicon_filename) + response = make_response(send_from_directory(watch.watch_data_dir, favicon_filename)) response.headers['Content-type'] = mime response.headers['Cache-Control'] = 'max-age=300, must-revalidate' # Cache for 5 minutes, then revalidate diff --git a/requirements.txt b/requirements.txt index 2e90ccc37..01e5fecdf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -117,6 +117,9 @@ price-parser # flask_socket_io - incorrect package name, already have flask-socketio above +# So far for detecting correct favicon type, but for other things in the future +python-magic + # Scheduler - Windows seemed to miss a lot of default timezone info (even "UTC" !) tzdata