diff --git a/blog.py b/blog.py index 273d531d9..30b36f404 100644 --- a/blog.py +++ b/blog.py @@ -32,7 +32,6 @@ from utils import is_account_dir from utils import remove_html from utils import get_config_param from utils import get_full_domain -from utils import get_media_formats from utils import get_nickname_from_actor from utils import get_domain_from_actor from utils import locate_post @@ -41,6 +40,7 @@ from utils import first_paragraph_from_string from utils import get_actor_property_url from utils import acct_dir from utils import escape_text +from formats import get_media_formats from posts import create_blogs_timeline from newswire import rss2header from newswire import rss2footer diff --git a/cache.py b/cache.py index 48ceec02f..1566e2b6c 100644 --- a/cache.py +++ b/cache.py @@ -26,7 +26,7 @@ from utils import load_json from utils import save_json from utils import get_file_case_insensitive from utils import get_user_paths -from utils import get_image_extensions +from formats import get_image_extensions from timeFunctions import date_from_string_format from timeFunctions import date_utcnow from content import remove_script diff --git a/content.py b/content.py index b78098957..a57e40724 100644 --- a/content.py +++ b/content.py @@ -36,7 +36,6 @@ from utils import has_object_dict from utils import valid_hash_tag from utils import dangerous_svg from utils import remove_domain_port -from utils import get_image_extensions from utils import load_json from utils import save_json from utils import file_last_modified @@ -46,6 +45,7 @@ from utils import acct_dir from utils import get_currencies from utils import remove_html from utils import remove_eol +from formats import get_image_extensions from petnames import get_pet_name from session import download_image diff --git a/daemon_get_images.py b/daemon_get_images.py index fa321cfba..d95f72f21 100644 --- a/daemon_get_images.py +++ b/daemon_get_images.py @@ -21,9 +21,9 @@ from httpheaders import set_headers_etag from utils import data_dir from utils import get_nickname_from_actor from utils import media_file_mime_type -from utils import get_image_mime_type -from utils import get_image_extensions from utils import acct_dir +from formats import get_image_mime_type +from formats import get_image_extensions from flags import is_image_file from daemon_utils import etag_exists from fitnessFunctions import fitness_performance diff --git a/daemon_post_image.py b/daemon_post_image.py index b7c2d8f87..e7e7469d0 100644 --- a/daemon_post_image.py +++ b/daemon_post_image.py @@ -12,8 +12,8 @@ import errno from socket import error as SocketError from httpcodes import http_404 from utils import acct_dir -from utils import get_image_extension_from_mime_type from utils import binary_is_image +from formats import get_image_extension_from_mime_type def receive_image_attachment(self, length: int, path: str, base_dir: str, diff --git a/flags.py b/flags.py index 18a61033a..4d40ad0c9 100644 --- a/flags.py +++ b/flags.py @@ -18,7 +18,6 @@ from utils import get_domain_from_actor from utils import acct_dir from utils import data_dir from utils import get_config_param -from utils import get_image_extensions from utils import evil_incarnate from utils import get_local_network_addresses from utils import get_attributed_to @@ -30,6 +29,7 @@ from utils import has_object_string_type from utils import get_reply_to from utils import text_in_file from utils import get_group_paths +from formats import get_image_extensions from quote import get_quote_toot_url diff --git a/formats.py b/formats.py new file mode 100644 index 000000000..458a02a80 --- /dev/null +++ b/formats.py @@ -0,0 +1,109 @@ +__filename__ = "formats.py" +__author__ = "Bob Mottram" +__license__ = "AGPL3+" +__version__ = "1.6.0" +__maintainer__ = "Bob Mottram" +__email__ = "bob@libreserver.org" +__status__ = "Production" +__module_group__ = "Core" + + +def get_video_extensions() -> []: + """Returns a list of the possible video file extensions + """ + return ('mp4', 'webm', 'ogv') + + +def get_audio_extensions() -> []: + """Returns a list of the possible audio file extensions + """ + return ('mp3', 'ogg', 'flac', 'opus', 'spx', 'wav') + + +def get_image_extensions() -> []: + """Returns a list of the possible image file extensions + """ + return ('jpg', 'jpeg', 'gif', 'webp', 'avif', 'heic', + 'svg', 'ico', 'jxl', 'png') + + +def image_mime_types_dict() -> {}: + """Returns a dict of image mime types + """ + return { + 'png': 'png', + 'jpg': 'jpeg', + 'jpeg': 'jpeg', + 'jxl': 'jxl', + 'gif': 'gif', + 'avif': 'avif', + 'heic': 'heic', + 'svg': 'svg+xml', + '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 + return 'image/png' + + +def get_image_extension_from_mime_type(content_type: str) -> str: + """Returns the image extension from a mime type, such as image/jpeg + """ + image_media = { + 'png': 'png', + 'jpeg': 'jpg', + 'jxl': 'jxl', + 'gif': 'gif', + 'svg+xml': 'svg', + 'webp': 'webp', + 'avif': 'avif', + 'heic': 'heic', + 'x-icon': 'ico' + } + for mime_ext, ext in image_media.items(): + if content_type.endswith(mime_ext): + return ext + return 'png' + + +def get_media_extensions() -> []: + """Returns a list of the possible media file extensions + """ + return get_image_extensions() + \ + get_video_extensions() + get_audio_extensions() + + +def get_image_formats() -> str: + """Returns a string of permissable image formats + used when selecting an image for a new post + """ + image_ext = get_image_extensions() + + image_formats = '' + for ext in image_ext: + if image_formats: + image_formats += ', ' + image_formats += '.' + ext + return image_formats + + +def get_media_formats() -> str: + """Returns a string of permissable media formats + used when selecting an attachment for a new post + """ + media_ext = get_media_extensions() + + media_formats = '' + for ext in media_ext: + if media_formats: + media_formats += ', ' + media_formats += '.' + ext + return media_formats diff --git a/mastoapiv2.py b/mastoapiv2.py index c4e122688..888dd4f48 100644 --- a/mastoapiv2.py +++ b/mastoapiv2.py @@ -15,13 +15,13 @@ from utils import acct_dir from utils import remove_html from utils import get_attachment_property_value from utils import no_of_accounts -from utils import get_image_extensions -from utils import get_video_extensions -from utils import get_audio_extensions -from utils import get_image_mime_type from utils import lines_in_file from utils import data_dir from utils import account_is_indexable +from formats import get_image_mime_type +from formats import get_image_extensions +from formats import get_audio_extensions +from formats import get_video_extensions def _get_masto_api_v2id_from_nickname(nickname: str) -> int: diff --git a/media.py b/media.py index f7230652c..ce795ed80 100644 --- a/media.py +++ b/media.py @@ -20,13 +20,13 @@ from timeFunctions import date_epoch from utils import safe_system_string from utils import get_base_content_from_post from utils import get_full_domain -from utils import get_image_extensions -from utils import get_video_extensions -from utils import get_audio_extensions -from utils import get_media_extensions from utils import has_object_dict from utils import acct_dir from utils import get_watermark_file +from formats import get_media_extensions +from formats import get_image_extensions +from formats import get_audio_extensions +from formats import get_video_extensions from shutil import copyfile from shutil import rmtree from shutil import move diff --git a/newswire.py b/newswire.py index 3518e5b16..e31a7ad65 100644 --- a/newswire.py +++ b/newswire.py @@ -24,7 +24,6 @@ from flags import is_local_network_address from flags import is_public_post from utils import data_dir from utils import string_contains -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 @@ -47,6 +46,7 @@ from utils import acct_dir from utils import local_actor_url from utils import escape_text from utils import unescaped_text +from formats import image_mime_types_dict from timeFunctions import date_from_string_format from blocking import is_blocked_domain from blocking import is_blocked_hashtag diff --git a/person.py b/person.py index 6f58c3629..df001a00d 100644 --- a/person.py +++ b/person.py @@ -42,7 +42,6 @@ from timeFunctions import date_utcnow from timeFunctions import get_current_time_int from utils import get_person_icon from utils import account_is_indexable -from utils import get_image_mime_type from utils import get_instance_url from utils import get_url_from_post from utils import get_memorials @@ -67,7 +66,6 @@ from utils import get_config_param from utils import refresh_newswire from utils import get_protocol_prefixes from utils import has_users_path -from utils import get_image_extensions from utils import acct_dir from utils import get_user_paths from utils import get_group_paths @@ -77,6 +75,8 @@ from utils import text_in_file from utils import contains_statuses from utils import get_actor_from_post from utils import data_dir +from formats import get_image_mime_type +from formats import get_image_extensions from status import get_status_number from session import get_json_valid from session import create_session diff --git a/posts.py b/posts.py index 64c6691f4..8177177ee 100644 --- a/posts.py +++ b/posts.py @@ -63,7 +63,6 @@ from utils import remove_eol from utils import text_in_file from utils import get_media_descriptions_from_post from utils import valid_hash_tag -from utils import get_audio_extensions from utils import get_summary_from_post from utils import get_user_paths from utils import has_object_string_type @@ -97,6 +96,7 @@ from utils import local_actor_url from utils import get_reply_to from utils import get_actor_from_post from utils import data_dir +from formats import get_audio_extensions from status import get_status_number from media import get_music_metadata from media import attach_media diff --git a/reading.py b/reading.py index 36283bafc..275c19257 100644 --- a/reading.py +++ b/reading.py @@ -19,7 +19,7 @@ from utils import get_attributed_to from utils import load_json from utils import save_json from utils import remove_html -from utils import get_image_extensions +from formats import get_image_extensions from timeFunctions import date_epoch from timeFunctions import date_from_string_format diff --git a/session.py b/session.py index c92042534..27b8a0806 100644 --- a/session.py +++ b/session.py @@ -18,8 +18,8 @@ from flags import url_permitted from utils import text_in_file from utils import acct_dir from utils import binary_is_image -from utils import image_mime_types_dict from utils import get_domain_from_actor +from formats import image_mime_types_dict from mitm import detect_mitm from httpsig import create_signed_header diff --git a/shares.py b/shares.py index 74abb5717..2c32f2ad8 100644 --- a/shares.py +++ b/shares.py @@ -33,7 +33,6 @@ from utils import data_dir from utils import resembles_url from utils import dangerous_markup from utils import remove_html -from utils import get_media_extensions from utils import acct_handle_dir from utils import remove_eol from utils import has_object_string_type @@ -42,7 +41,8 @@ from utils import get_full_domain from utils import valid_nickname from utils import load_json from utils import save_json -from utils import get_image_extensions +from formats import get_media_extensions +from formats import get_image_extensions from utils import remove_domain_port from utils import is_account_dir from utils import acct_dir diff --git a/theme.py b/theme.py index 4d9de4cc7..8d6530607 100644 --- a/theme.py +++ b/theme.py @@ -17,7 +17,6 @@ from utils import string_ends_with from utils import is_account_dir from utils import load_json from utils import save_json -from utils import get_image_extensions from utils import copytree from utils import acct_dir from utils import dangerous_svg @@ -26,6 +25,7 @@ from utils import remove_html from utils import text_in_file from utils import remove_eol from utils import language_right_to_left +from formats import get_image_extensions from content import dangerous_css from textmode import set_text_mode_theme diff --git a/utils.py b/utils.py index f69624fac..df5ca3aaa 100644 --- a/utils.py +++ b/utils.py @@ -21,6 +21,7 @@ from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes from followingCalendar import add_person_to_calendar from unicodetext import standardize_text +from formats import get_image_extensions VALID_HASHTAG_CHARS = \ set('_0123456789' + @@ -610,107 +611,6 @@ def get_full_domain(domain: str, port: int) -> str: return domain + ':' + str(port) -def get_video_extensions() -> []: - """Returns a list of the possible video file extensions - """ - return ('mp4', 'webm', 'ogv') - - -def get_audio_extensions() -> []: - """Returns a list of the possible audio file extensions - """ - return ('mp3', 'ogg', 'flac', 'opus', 'spx', 'wav') - - -def get_image_extensions() -> []: - """Returns a list of the possible image file extensions - """ - return ('jpg', 'jpeg', 'gif', 'webp', 'avif', 'heic', - 'svg', 'ico', 'jxl', 'png') - - -def image_mime_types_dict() -> {}: - """Returns a dict of image mime types - """ - return { - 'png': 'png', - 'jpg': 'jpeg', - 'jpeg': 'jpeg', - 'jxl': 'jxl', - 'gif': 'gif', - 'avif': 'avif', - 'heic': 'heic', - 'svg': 'svg+xml', - '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 - return 'image/png' - - -def get_image_extension_from_mime_type(content_type: str) -> str: - """Returns the image extension from a mime type, such as image/jpeg - """ - image_media = { - 'png': 'png', - 'jpeg': 'jpg', - 'jxl': 'jxl', - 'gif': 'gif', - 'svg+xml': 'svg', - 'webp': 'webp', - 'avif': 'avif', - 'heic': 'heic', - 'x-icon': 'ico' - } - for mime_ext, ext in image_media.items(): - if content_type.endswith(mime_ext): - return ext - return 'png' - - -def get_media_extensions() -> []: - """Returns a list of the possible media file extensions - """ - return get_image_extensions() + \ - get_video_extensions() + get_audio_extensions() - - -def get_image_formats() -> str: - """Returns a string of permissable image formats - used when selecting an image for a new post - """ - image_ext = get_image_extensions() - - image_formats = '' - for ext in image_ext: - if image_formats: - image_formats += ', ' - image_formats += '.' + ext - return image_formats - - -def get_media_formats() -> str: - """Returns a string of permissable media formats - used when selecting an attachment for a new post - """ - media_ext = get_media_extensions() - - media_formats = '' - for ext in media_ext: - if media_formats: - media_formats += ', ' - media_formats += '.' + ext - return media_formats - - def remove_html(content: str) -> str: """Removes html links from the given content. Used to ensure that profile descriptions don't contain dubious content diff --git a/webapp_column_right.py b/webapp_column_right.py index 9a6d6c708..1423c361c 100644 --- a/webapp_column_right.py +++ b/webapp_column_right.py @@ -13,7 +13,6 @@ from content import limit_repeated_words from flags import is_editor from utils import replace_strings from utils import data_dir -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 @@ -24,6 +23,7 @@ from utils import get_nickname_from_actor from utils import get_config_param from utils import remove_domain_port from utils import acct_dir +from formats import get_image_extensions from timeFunctions import date_from_string_format from posts import is_moderator from newswire import get_newswire_favicon_url diff --git a/webapp_create_post.py b/webapp_create_post.py index 5b9617a30..6479a76ea 100644 --- a/webapp_create_post.py +++ b/webapp_create_post.py @@ -21,7 +21,6 @@ from utils import locate_post from utils import get_new_post_endpoints from utils import get_nickname_from_actor from utils import get_domain_from_actor -from utils import get_media_formats from utils import get_config_param from utils import acct_dir from utils import get_currencies @@ -29,6 +28,7 @@ from utils import get_category_types from utils import get_supported_languages from utils import get_attributed_to from utils import get_full_domain +from formats import get_media_formats from timeFunctions import get_account_timezone from blocking import sending_is_blocked2 from webapp_utils import open_content_warning diff --git a/webapp_login.py b/webapp_login.py index 60a22cad2..77617713a 100644 --- a/webapp_login.py +++ b/webapp_login.py @@ -12,10 +12,10 @@ import time import filecmp from shutil import copyfile from utils import data_dir -from utils import get_image_extensions from utils import get_config_param from utils import no_of_accounts from utils import get_nickname_validation_pattern +from formats import get_image_extensions from webapp_utils import set_custom_background from webapp_utils import html_header_with_website_markup from webapp_utils import html_footer diff --git a/webapp_profile.py b/webapp_profile.py index 018de1656..72ab08d41 100644 --- a/webapp_profile.py +++ b/webapp_profile.py @@ -41,7 +41,6 @@ from utils import get_domain_from_actor from utils import remove_html from utils import load_json from utils import get_config_param -from utils import get_image_formats from utils import acct_dir from utils import get_supported_languages from utils import local_actor_url @@ -49,6 +48,7 @@ from utils import get_reply_interval_hours from utils import remove_eol from utils import get_actor_from_post from utils import resembles_url +from formats import get_image_formats from timeFunctions import time_days_ago from timeFunctions import get_account_timezone from languages import get_actor_languages diff --git a/webapp_utils.py b/webapp_utils.py index adeca5d29..9d071ff41 100644 --- a/webapp_utils.py +++ b/webapp_utils.py @@ -19,7 +19,6 @@ from utils import get_image_file from utils import data_dir from utils import string_contains from utils import get_post_attachments -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 @@ -37,14 +36,15 @@ from utils import get_config_param from utils import acct_dir from utils import get_nickname_from_actor from utils import get_domain_from_actor -from utils import get_audio_extensions -from utils import get_video_extensions -from utils import get_image_extensions from utils import local_actor_url from utils import text_in_file from utils import remove_eol from utils import binary_is_image from utils import resembles_url +from formats import image_mime_types_dict +from formats import get_image_extensions +from formats import get_audio_extensions +from formats import get_video_extensions from filters import is_filtered from cache import get_actor_public_key_from_id from cache import store_person_in_cache diff --git a/webapp_welcome_profile.py b/webapp_welcome_profile.py index 318f9e5a8..395616503 100644 --- a/webapp_welcome_profile.py +++ b/webapp_welcome_profile.py @@ -14,10 +14,10 @@ from utils import data_dir from utils import remove_html from utils import load_json from utils import get_config_param -from utils import get_image_extensions -from utils import get_image_formats from utils import acct_dir from utils import local_actor_url +from formats import get_image_formats +from formats import get_image_extensions from webapp_utils import html_header_with_external_style from webapp_utils import html_footer from webapp_utils import edit_text_field