File formats in a separate module

main
Bob Mottram 2025-05-29 21:06:28 +01:00
parent be79b3b8d8
commit 76d1769787
23 changed files with 144 additions and 135 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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

109
formats.py 100644
View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

102
utils.py
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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