Tidy up the handling of media formats

main
Bob Mottram 2020-11-21 11:54:29 +00:00
parent 050c87d0e4
commit 30ebfda697
8 changed files with 60 additions and 21 deletions

View File

@ -15,6 +15,7 @@ from webapp import htmlHeaderWithExternalStyle
from webapp import htmlFooter from webapp import htmlFooter
from webapp_media import addEmbeddedElements from webapp_media import addEmbeddedElements
from webapp_utils import getPostAttachmentsAsHtml from webapp_utils import getPostAttachmentsAsHtml
from utils import getMediaFormats
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import locatePost from utils import locatePost
@ -745,8 +746,7 @@ def htmlEditBlog(mediaInstance: bool, translate: {},
editBlogImageSection += \ editBlogImageSection += \
' <input type="file" id="attachpic" name="attachpic"' ' <input type="file" id="attachpic" name="attachpic"'
editBlogImageSection += \ editBlogImageSection += \
' accept=".png, .jpg, .jpeg, .gif, .webp, .avif, ' + \ ' accept="' + getMediaFormats() + '">'
'.mp4, .webm, .ogv, .mp3, .ogg">'
editBlogImageSection += ' </div>' editBlogImageSection += ' </div>'
placeholderMessage = translate['Write something'] + '...' placeholderMessage = translate['Write something'] + '...'

View File

@ -9,6 +9,7 @@ __status__ = "Production"
import os import os
import email.parser import email.parser
from shutil import copyfile from shutil import copyfile
from utils import getImageExtensions
from utils import loadJson from utils import loadJson
from utils import fileLastModified from utils import fileLastModified
from utils import getLinkPrefixes from utils import getLinkPrefixes
@ -939,7 +940,7 @@ def saveMediaInFormPOST(mediaBytes, debug: bool,
break break
# remove any existing image files with a different format # remove any existing image files with a different format
extensionTypes = ('png', 'jpg', 'jpeg', 'gif', 'webp', 'avif') extensionTypes = getImageExtensions()
for ex in extensionTypes: for ex in extensionTypes:
if ex == detectedExtension: if ex == detectedExtension:
continue continue

View File

@ -166,6 +166,7 @@ from shares import getSharesFeedForPerson
from shares import addShare from shares import addShare
from shares import removeShare from shares import removeShare
from shares import expireShares from shares import expireShares
from utils import getImageExtensions
from utils import mediaFileMimeType from utils import mediaFileMimeType
from utils import getCSS from utils import getCSS
from utils import firstParagraphFromString from utils import firstParagraphFromString
@ -8412,7 +8413,8 @@ class PubServer(BaseHTTPRequestHandler):
GETstartTime, GETtimings: {}) -> bool: GETstartTime, GETtimings: {}) -> bool:
"""Show a background image """Show a background image
""" """
for ext in ('webp', 'gif', 'jpg', 'png', 'avif'): imageExtensions = getImageExtensions()
for ext in imageExtensions:
for bg in ('follow', 'options', 'login'): for bg in ('follow', 'options', 'login'):
# follow screen background image # follow screen background image
if path.endswith('/' + bg + '-background.' + ext): if path.endswith('/' + bg + '-background.' + ext):

View File

@ -13,6 +13,10 @@ import os
import datetime import datetime
from hashlib import sha1 from hashlib import sha1
from auth import createPassword from auth import createPassword
from utils import getImageExtensions
from utils import getVideoExtensions
from utils import getAudioExtensions
from utils import getMediaExtensions
from shutil import copyfile from shutil import copyfile
from shutil import rmtree from shutil import rmtree
from shutil import move from shutil import move
@ -56,8 +60,7 @@ def getImageHash(imageFilename: str) -> str:
def isMedia(imageFilename: str) -> bool: def isMedia(imageFilename: str) -> bool:
permittedMedia = ('png', 'jpg', 'gif', 'webp', 'avif', permittedMedia = getMediaExtensions()
'mp4', 'ogv', 'mp3', 'ogg')
for m in permittedMedia: for m in permittedMedia:
if imageFilename.endswith('.' + m): if imageFilename.endswith('.' + m):
return True return True
@ -83,16 +86,15 @@ def getAttachmentMediaType(filename: str) -> str:
image, video or audio image, video or audio
""" """
mediaType = None mediaType = None
imageTypes = ('png', 'jpg', 'jpeg', imageTypes = getImageExtensions()
'gif', 'webp', 'avif')
for mType in imageTypes: for mType in imageTypes:
if filename.endswith('.' + mType): if filename.endswith('.' + mType):
return 'image' return 'image'
videoTypes = ('mp4', 'webm', 'ogv') videoTypes = getVideoExtensions()
for mType in videoTypes: for mType in videoTypes:
if filename.endswith('.' + mType): if filename.endswith('.' + mType):
return 'video' return 'video'
audioTypes = ('mp3', 'ogg') audioTypes = getAudioExtensions()
for mType in audioTypes: for mType in audioTypes:
if filename.endswith('.' + mType): if filename.endswith('.' + mType):
return 'audio' return 'audio'
@ -143,8 +145,7 @@ def attachMedia(baseDir: str, httpPrefix: str, domain: str, port: int,
return postJson return postJson
fileExtension = None fileExtension = None
acceptedTypes = ('png', 'jpg', 'gif', 'webp', 'avif', acceptedTypes = getMediaExtensions()
'mp4', 'webm', 'ogv', 'mp3', 'ogg')
for mType in acceptedTypes: for mType in acceptedTypes:
if imageFilename.endswith('.' + mType): if imageFilename.endswith('.' + mType):
if mType == 'jpg': if mType == 'jpg':

View File

@ -16,6 +16,7 @@ from session import postImage
from utils import validNickname from utils import validNickname
from utils import loadJson from utils import loadJson
from utils import saveJson from utils import saveJson
from utils import getImageExtensions
from media import removeMetaData from media import removeMetaData
@ -54,7 +55,7 @@ def removeShare(baseDir: str, nickname: str, domain: str,
# remove any image for the item # remove any image for the item
itemIDfile = baseDir + '/sharefiles/' + nickname + '/' + itemID itemIDfile = baseDir + '/sharefiles/' + nickname + '/' + itemID
if sharesJson[itemID]['imageUrl']: if sharesJson[itemID]['imageUrl']:
formats = ('png', 'jpg', 'gif', 'webp', 'avif') formats = getImageExtensions()
for ext in formats: for ext in formats:
if sharesJson[itemID]['imageUrl'].endswith('.' + ext): if sharesJson[itemID]['imageUrl'].endswith('.' + ext):
if os.path.isfile(itemIDfile + '.' + ext): if os.path.isfile(itemIDfile + '.' + ext):
@ -108,7 +109,7 @@ def addShare(baseDir: str,
if not imageFilename: if not imageFilename:
sharesImageFilename = \ sharesImageFilename = \
baseDir + '/accounts/' + nickname + '@' + domain + '/upload' baseDir + '/accounts/' + nickname + '@' + domain + '/upload'
formats = ('png', 'jpg', 'gif', 'webp', 'avif') formats = getImageExtensions()
for ext in formats: for ext in formats:
if os.path.isfile(sharesImageFilename + '.' + ext): if os.path.isfile(sharesImageFilename + '.' + ext):
imageFilename = sharesImageFilename + '.' + ext imageFilename = sharesImageFilename + '.' + ext
@ -128,7 +129,7 @@ def addShare(baseDir: str,
if not os.path.isdir(baseDir + '/sharefiles/' + nickname): if not os.path.isdir(baseDir + '/sharefiles/' + nickname):
os.mkdir(baseDir + '/sharefiles/' + nickname) os.mkdir(baseDir + '/sharefiles/' + nickname)
itemIDfile = baseDir + '/sharefiles/' + nickname + '/' + itemID itemIDfile = baseDir + '/sharefiles/' + nickname + '/' + itemID
formats = ('png', 'jpg', 'gif', 'webp', 'avif') formats = getImageExtensions()
for ext in formats: for ext in formats:
if imageFilename.endswith('.' + ext): if imageFilename.endswith('.' + ext):
removeMetaData(imageFilename, itemIDfile + '.' + ext) removeMetaData(imageFilename, itemIDfile + '.' + ext)
@ -202,7 +203,7 @@ def expireSharesForAccount(baseDir: str, nickname: str, domain: str) -> None:
# remove any associated images # remove any associated images
itemIDfile = \ itemIDfile = \
baseDir + '/sharefiles/' + nickname + '/' + itemID baseDir + '/sharefiles/' + nickname + '/' + itemID
formats = ('png', 'jpg', 'gif', 'webp', 'avif') formats = getImageExtensions()
for ext in formats: for ext in formats:
if os.path.isfile(itemIDfile + '.' + ext): if os.path.isfile(itemIDfile + '.' + ext):
os.remove(itemIDfile + '.' + ext) os.remove(itemIDfile + '.' + ext)

View File

@ -9,6 +9,7 @@ __status__ = "Production"
import os import os
from utils import loadJson from utils import loadJson
from utils import saveJson from utils import saveJson
from utils import getImageExtensions
from shutil import copyfile from shutil import copyfile
from content import dangerousCSS from content import dangerousCSS
@ -473,7 +474,7 @@ def setThemeImages(baseDir: str, name: str) -> None:
backgroundNames = ('login', 'shares', 'delete', 'follow', backgroundNames = ('login', 'shares', 'delete', 'follow',
'options', 'block', 'search', 'calendar') 'options', 'block', 'search', 'calendar')
extensions = ('webp', 'gif', 'jpg', 'png', 'avif') extensions = getImageExtensions()
for subdir, dirs, files in os.walk(baseDir + '/accounts'): for subdir, dirs, files in os.walk(baseDir + '/accounts'):
for acct in dirs: for acct in dirs:

View File

@ -25,6 +25,24 @@ def getImageExtensions() -> []:
return ('png', 'jpg', 'jpeg', 'gif', 'webp', 'avif') return ('png', 'jpg', 'jpeg', 'gif', 'webp', 'avif')
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
"""
return getImageExtensions() + getVideoExtensions() + getAudioExtensions()
def getImageFormats() -> str: def getImageFormats() -> str:
"""Returns a string of permissable image formats """Returns a string of permissable image formats
used when selecting an image for a new post used when selecting an image for a new post
@ -39,6 +57,20 @@ def getImageFormats() -> str:
return imageFormats return imageFormats
def getMediaFormats() -> str:
"""Returns a string of permissable media formats
used when selecting an attachment for a new post
"""
mediaExt = getMediaExtensions()
mediaFormats = ''
for ext in mediaExt:
if mediaFormats:
mediaFormats += ', '
mediaFormats += '.' + ext
return mediaFormats
def removeHtml(content: str) -> str: def removeHtml(content: str) -> str:
"""Removes html links from the given content. """Removes html links from the given content.
Used to ensure that profile descriptions don't contain dubious content Used to ensure that profile descriptions don't contain dubious content
@ -213,7 +245,7 @@ def removeAvatarFromCache(baseDir: str, actorStr: str) -> None:
"""Removes any existing avatar entries from the cache """Removes any existing avatar entries from the cache
This avoids duplicate entries with differing extensions This avoids duplicate entries with differing extensions
""" """
avatarFilenameExtensions = ('png', 'jpg', 'gif', 'webp', 'avif') avatarFilenameExtensions = getImageExtensions()
for extension in avatarFilenameExtensions: for extension in avatarFilenameExtensions:
avatarFilename = \ avatarFilename = \
baseDir + '/cache/avatars/' + actorStr + '.' + extension baseDir + '/cache/avatars/' + actorStr + '.' + extension

View File

@ -10,6 +10,8 @@ import os
from utils import isPublicPostFromUrl from utils import isPublicPostFromUrl
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import getImageFormats
from utils import getMediaFormats
from webapp_utils import getIconsWebPath from webapp_utils import getIconsWebPath
from webapp_utils import getBannerFile from webapp_utils import getBannerFile
from webapp_utils import htmlHeaderWithExternalStyle from webapp_utils import htmlHeaderWithExternalStyle
@ -280,13 +282,12 @@ def htmlNewPost(cssCache: {}, mediaInstance: bool, translate: {},
newPostImageSection += \ newPostImageSection += \
' <input type="file" id="attachpic" name="attachpic"' ' <input type="file" id="attachpic" name="attachpic"'
newPostImageSection += \ newPostImageSection += \
' accept=".png, .jpg, .jpeg, .gif, .webp, .avif">\n' ' accept="' + getImageFormats() + '">\n'
else: else:
newPostImageSection += \ newPostImageSection += \
' <input type="file" id="attachpic" name="attachpic"' ' <input type="file" id="attachpic" name="attachpic"'
newPostImageSection += \ newPostImageSection += \
' accept=".png, .jpg, .jpeg, .gif, ' + \ ' accept="' + getMediaFormats() + '">\n'
'.webp, .avif, .mp4, .webm, .ogv, .mp3, .ogg">\n'
newPostImageSection += ' </div>\n' newPostImageSection += ' </div>\n'
scopeIcon = 'scope_public.png' scopeIcon = 'scope_public.png'