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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -25,6 +25,24 @@ def getImageExtensions() -> []:
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:
"""Returns a string of permissable image formats
used when selecting an image for a new post
@ -39,6 +57,20 @@ def getImageFormats() -> str:
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:
"""Removes html links from the given 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
This avoids duplicate entries with differing extensions
"""
avatarFilenameExtensions = ('png', 'jpg', 'gif', 'webp', 'avif')
avatarFilenameExtensions = getImageExtensions()
for extension in avatarFilenameExtensions:
avatarFilename = \
baseDir + '/cache/avatars/' + actorStr + '.' + extension

View File

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