From 224135ca377993e247cb61d90259a57abd022962 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 5 Feb 2024 20:05:00 +0000 Subject: [PATCH] Tidying --- newswire.py | 13 ++----------- person.py | 9 ++------- session.py | 27 +++------------------------ utils.py | 13 ++++++++++--- webapp_column_right.py | 4 ++-- webapp_utils.py | 17 +++-------------- 6 files changed, 22 insertions(+), 61 deletions(-) diff --git a/newswire.py b/newswire.py index 7d16ee894..98229922e 100644 --- a/newswire.py +++ b/newswire.py @@ -19,6 +19,7 @@ from datetime import timezone from collections import OrderedDict from utils import valid_post_date from categories import set_hashtag_category +from utils import image_mime_types_dict from utils import resembles_url from utils import get_url_from_post from utils import remove_zero_length_strings @@ -171,17 +172,7 @@ def _download_newswire_feed_favicon(session, base_dir: str, return False # update the favicon url - extensions_to_mime = { - 'ico': 'x-icon', - 'png': 'png', - 'jpg': 'jpeg', - 'jxl': 'jxl', - 'gif': 'gif', - 'avif': 'avif', - 'heic': 'heic', - 'svg': 'svg+xml', - 'webp': 'webp' - } + extensions_to_mime = image_mime_types_dict() for ext, mime_ext in extensions_to_mime.items(): if 'image/' + mime_ext in mime_type: fav_url = fav_url.replace('.ico', '.' + ext) diff --git a/person.py b/person.py index e664bc598..9d285dd5f 100644 --- a/person.py +++ b/person.py @@ -37,6 +37,7 @@ from roles import set_role from roles import actor_roles_from_list from roles import get_actor_roles_list from media import process_meta_data +from utils import get_image_mime_type from utils import get_instance_url from utils import get_url_from_post from utils import date_utcnow @@ -149,13 +150,7 @@ def set_profile_image(base_dir: str, http_prefix: str, extensions = get_image_extensions() for ext in extensions: if image_filename.endswith('.' + ext): - media_type = 'image/' + ext - if ext == 'svg': - media_type = 'image/' + ext + '+xml' - elif ext == 'jpg': - media_type = 'image/jpeg' - elif ext == 'ico': - media_type = 'image/x-icon' + media_type = get_image_mime_type(image_filename) icon_filename = icon_filename_base + '.' + ext profile_filename = acct_handle_dir(base_dir, handle) + '/' + icon_filename diff --git a/session.py b/session.py index 642dbe077..ffd064898 100644 --- a/session.py +++ b/session.py @@ -14,6 +14,7 @@ from utils import acct_dir from utils import url_permitted from utils import is_image_file from utils import binary_is_image +from utils import image_mime_types_dict from httpsig import create_signed_header import json from socket import error as SocketError @@ -752,18 +753,7 @@ def download_image(session, url: str, image_filename: str, debug: bool, return None # try different image types - image_formats = { - 'png': 'png', - 'jpg': 'jpeg', - 'jpeg': 'jpeg', - 'jxl': 'jxl', - 'gif': 'gif', - 'svg': 'svg+xml', - 'webp': 'webp', - 'avif': 'avif', - 'heic': 'heic', - 'ico': 'x-icon' - } + image_formats = image_mime_types_dict() session_headers = None for im_format, mime_type in image_formats.items(): if url.endswith('.' + im_format) or \ @@ -871,18 +861,7 @@ def download_image_any_mime_type(session, url: str, if not content_type: return None, None - image_formats = { - 'ico': 'x-icon', - 'png': 'png', - 'jpg': 'jpeg', - 'jxl': 'jxl', - 'jpeg': 'jpeg', - 'gif': 'gif', - 'svg': 'svg+xml', - 'webp': 'webp', - 'avif': 'avif', - 'heic': 'heic' - } + image_formats = image_mime_types_dict() for _, m_type in image_formats.items(): if 'image/' + m_type in content_type: mime_type = 'image/' + m_type diff --git a/utils.py b/utils.py index a38778c07..e82c9fe46 100644 --- a/utils.py +++ b/utils.py @@ -805,12 +805,13 @@ def get_image_extensions() -> []: 'svg', 'ico', 'jxl', 'png') -def get_image_mime_type(image_filename: str) -> str: - """Returns the mime type for the given image +def image_mime_types_dict() -> {}: + """Returns a dict of image mime types """ - extensions_to_mime = { + return { 'png': 'png', 'jpg': 'jpeg', + 'jpeg': 'jpeg', 'jxl': 'jxl', 'gif': 'gif', 'avif': 'avif', @@ -819,6 +820,12 @@ def get_image_mime_type(image_filename: str) -> str: 'webp': 'webp', 'ico': 'x-icon' } + + +def get_image_mime_type(image_filename: str) -> str: + """Returns the mime type for the given image filename + """ + extensions_to_mime = image_mime_types_dict() for ext, mime_ext in extensions_to_mime.items(): if image_filename.endswith('.' + ext): return 'image/' + mime_ext diff --git a/webapp_column_right.py b/webapp_column_right.py index 386787f56..1cf718bce 100644 --- a/webapp_column_right.py +++ b/webapp_column_right.py @@ -10,6 +10,7 @@ __module_group__ = "Web Interface Columns" import os from content import remove_long_words from content import limit_repeated_words +from utils import get_image_extensions from utils import get_fav_filename_from_url from utils import get_base_content_from_post from utils import remove_html @@ -255,8 +256,7 @@ def _html_newswire(base_dir: str, newswire: {}, nickname: str, moderator: bool, favicon_url = \ cached_favicon_filename.replace(base_dir, '') else: - extensions = \ - ('png', 'jpg', 'gif', 'avif', 'heic', 'svg', 'webp', 'jxl') + extensions = get_image_extensions() for ext in extensions: cached_favicon_filename = \ get_fav_filename_from_url(base_dir, favicon_url) diff --git a/webapp_utils.py b/webapp_utils.py index 5a6bfb512..b16bc51cf 100644 --- a/webapp_utils.py +++ b/webapp_utils.py @@ -12,6 +12,7 @@ from shutil import copyfile from collections import OrderedDict from session import get_json from session import get_json_valid +from utils import image_mime_types_dict from utils import get_url_from_post from utils import get_media_url_from_video from utils import get_attributed_to @@ -357,17 +358,7 @@ def update_avatar_image_cache(signing_priv_key_pem: str, avatar_image_path = base_dir + '/cache/avatars/' + actor_str # try different image types - image_formats = { - 'png': 'png', - 'jpg': 'jpeg', - 'jxl': 'jxl', - 'jpeg': 'jpeg', - 'gif': 'gif', - 'svg': 'svg+xml', - 'webp': 'webp', - 'avif': 'avif', - 'heic': 'heic' - } + image_formats = image_mime_types_dict() avatar_image_filename = None for im_format, mime_type in image_formats.items(): if avatar_url.endswith('.' + im_format) or \ @@ -1188,9 +1179,7 @@ def _is_attached_image(attachment_filename: str) -> bool: """ if '.' not in attachment_filename: return False - image_ext = ( - 'png', 'jpg', 'jpeg', 'webp', 'avif', 'heic', 'svg', 'gif', 'jxl' - ) + image_ext = get_image_extensions() ext = attachment_filename.split('.')[-1] if ext in image_ext: return True