Check media type

master
Bob Mottram 2019-08-30 19:01:29 +01:00
parent eb9dec67e3
commit 67ca4450a6
1 changed files with 12 additions and 8 deletions

View File

@ -30,11 +30,12 @@ def removeMetaData(imageFilename: str,outputFilename: str):
def getImageHash(imageFilename: str) -> str: def getImageHash(imageFilename: str) -> str:
return blurencode(numpy.array(Image.open(imageFilename).convert("RGB"))) return blurencode(numpy.array(Image.open(imageFilename).convert("RGB")))
def isImage(imageFilename: str) -> bool: def isMedia(imageFilename: str) -> bool:
if imageFilename.endswith('.png') or \ permittedMedia=['png','jpg','gif','mp4','ogv','mp3','ogg']
imageFilename.endswith('.jpg') or \ for m in permittedMedia:
imageFilename.endswith('.gif'): if imageFilename.endswith('.'+m):
return True return True
print('WARN: '+imageFilename+' is not a permitted media type')
return False return False
def createMediaDirs(baseDir: str,mediaPath: str) -> None: def createMediaDirs(baseDir: str,mediaPath: str) -> None:
@ -75,15 +76,18 @@ def attachImage(baseDir: str,httpPrefix: str,domain: str,port: int, \
The description can be None The description can be None
Blurhash is optional, since low power systems may take a long time to calculate it Blurhash is optional, since low power systems may take a long time to calculate it
""" """
if not isImage(imageFilename): if not isMedia(imageFilename):
return postJson return postJson
fileExtension=None
acceptedTypes=['png','jpg','gif','mp4','webm','ogv','mp3','ogg'] acceptedTypes=['png','jpg','gif','mp4','webm','ogv','mp3','ogg']
for mType in acceptedTypes: for mType in acceptedTypes:
if imageFilename.endswith('.'+mType): if imageFilename.endswith('.'+mType):
fileExtension=mType fileExtension=mType
if mType=='jpg': if mType=='jpg':
mType='jpeg' mType='jpeg'
if not fileExtension:
return postJson
mediaType=mediaType+'/'+fileExtension mediaType=mediaType+'/'+fileExtension
if fileExtension=='jpeg': if fileExtension=='jpeg':
@ -106,7 +110,7 @@ def attachImage(baseDir: str,httpPrefix: str,domain: str,port: int, \
'type': 'Document', 'type': 'Document',
'url': httpPrefix+'://'+domain+'/'+mediaPath 'url': httpPrefix+'://'+domain+'/'+mediaPath
} }
if useBlurhash and fileExtension=='png': if useBlurhash and mediaType=='image':
attachmentJson['blurhash']=getImageHash(imageFilename) attachmentJson['blurhash']=getImageHash(imageFilename)
postJson['attachment']=[attachmentJson] postJson['attachment']=[attachmentJson]