From beb451baf7ff81b1a7d2b2db3bdc83bb16e18cc8 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 3 Aug 2021 10:09:04 +0100 Subject: [PATCH] Tidying of mime types --- content.py | 1 + utils.py | 25 +++++++++++++------------ webapp_utils.py | 43 ++++++++++++++++++++++--------------------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/content.py b/content.py index 3f3b1d8ec..1e4bd8daf 100644 --- a/content.py +++ b/content.py @@ -951,6 +951,7 @@ def saveMediaInFormPOST(mediaBytes, debug: bool, 'ogv': 'video/ogv', 'mp3': 'audio/mpeg', 'ogg': 'audio/ogg', + 'flac': 'audio/flac', 'zip': 'application/zip' } detectedExtension = None diff --git a/utils.py b/utils.py index 7c5a88421..a2014a5c2 100644 --- a/utils.py +++ b/utils.py @@ -297,6 +297,18 @@ def isArtist(baseDir: str, nickname: str) -> bool: return False +def getVideoExtensions() -> []: + """Returns a list of the possible video file extensions + """ + return ('mp4', 'webm', 'ogv') + + +def getAudioExtensions() -> []: + """Returns a list of the possible audio file extensions + """ + return ('mp3', 'ogg', 'flac') + + def getImageExtensions() -> []: """Returns a list of the possible image file extensions """ @@ -337,18 +349,6 @@ def getImageExtensionFromMimeType(contentType: str) -> str: return 'png' -def getVideoExtensions() -> []: - """Returns a list of the possible video file extensions - """ - return ('mp4', 'webm', 'ogv') - - -def getAudioExtensions() -> []: - """Returns a list of the possible audio file extensions - """ - return ('mp3', 'ogg') - - def getMediaExtensions() -> []: """Returns a list of the possible media file extensions """ @@ -2216,6 +2216,7 @@ def mediaFileMimeType(filename: str) -> str: 'avif': 'image/avif', 'mp3': 'audio/mpeg', 'ogg': 'audio/ogg', + 'flac': 'audio/flac', 'mp4': 'video/mp4', 'ogv': 'video/ogv' } diff --git a/webapp_utils.py b/webapp_utils.py index 676acba5b..1cda40ee0 100644 --- a/webapp_utils.py +++ b/webapp_utils.py @@ -12,7 +12,6 @@ from collections import OrderedDict from session import getJson from utils import isAccountDir from utils import removeHtml -from utils import getImageExtensions from utils import getProtocolPrefixes from utils import loadJson from utils import getCachedPostFilename @@ -20,6 +19,9 @@ from utils import getConfigParam from utils import acctDir from utils import getNicknameFromActor from utils import isfloat +from utils import getAudioExtensions +from utils import getVideoExtensions +from utils import getImageExtensions from cache import storePersonInCache from content import addHtmlTags from content import replaceEmojiFromTags @@ -793,15 +795,13 @@ def addEmojiToDisplayName(baseDir: str, httpPrefix: str, def _isImageMimeType(mimeType: str) -> bool: """Is the given mime type an image? """ - imageMimeTypes = ( - 'image/png', - 'image/jpeg', - 'image/webp', - 'image/avif', - 'image/svg+xml', - 'image/gif' - ) - if mimeType in imageMimeTypes: + extensions = getImageExtensions() + if mimeType == 'image/svg+xml': + return True + if not mimeType.startswith('image/'): + return False + ext = mimeType.split('/')[1] + if ext in extensions: return True return False @@ -809,12 +809,11 @@ def _isImageMimeType(mimeType: str) -> bool: def _isVideoMimeType(mimeType: str) -> bool: """Is the given mime type a video? """ - videoMimeTypes = ( - 'video/mp4', - 'video/webm', - 'video/ogv' - ) - if mimeType in videoMimeTypes: + extensions = getVideoExtensions() + if not mimeType.startswith('video/'): + return False + ext = mimeType.split('/')[1] + if ext in extensions: return True return False @@ -822,11 +821,13 @@ def _isVideoMimeType(mimeType: str) -> bool: def _isAudioMimeType(mimeType: str) -> bool: """Is the given mime type an audio file? """ - audioMimeTypes = ( - 'audio/mpeg', - 'audio/ogg' - ) - if mimeType in audioMimeTypes: + extensions = getAudioExtensions() + if mimeType == 'audio/mpeg': + return True + if not mimeType.startswith('audio/'): + return False + ext = mimeType.split('/')[1] + if ext in extensions: return True return False