Supporting webp image format

main
Bob Mottram 2019-11-14 15:11:20 +00:00
parent 7c6f413e95
commit b0b5f1fece
3 changed files with 22 additions and 4 deletions

View File

@ -517,6 +517,8 @@ class PubServer(BaseHTTPRequestHandler):
fileExtension='jpg'
elif mediaTypeStr.endswith('gif'):
fileExtension='gif'
elif mediaTypeStr.endswith('webp'):
fileExtension='webp'
elif mediaTypeStr.endswith('audio/mpeg'):
fileExtension='mp3'
elif mediaTypeStr.endswith('ogg'):
@ -1085,6 +1087,8 @@ class PubServer(BaseHTTPRequestHandler):
mediaImageType='png'
elif emojiFilename.endswith('.jpg'):
mediaImageType='jpeg'
elif emojiFilename.endswith('.webp'):
mediaImageType='webp'
else:
mediaImageType='gif'
with open(emojiFilename, 'rb') as avFile:
@ -1101,6 +1105,7 @@ class PubServer(BaseHTTPRequestHandler):
if self.path.endswith('.png') or \
self.path.endswith('.jpg') or \
self.path.endswith('.gif') or \
self.path.endswith('.webp') or \
self.path.endswith('.mp4') or \
self.path.endswith('.ogv') or \
self.path.endswith('.mp3') or \
@ -1116,6 +1121,8 @@ class PubServer(BaseHTTPRequestHandler):
mediaFileType='image/jpeg'
elif mediaFilename.endswith('.gif'):
mediaFileType='image/gif'
elif mediaFilename.endswith('.webp'):
mediaFileType='image/webp'
elif mediaFilename.endswith('.mp4'):
mediaFileType='video/mp4'
elif mediaFilename.endswith('.ogv'):
@ -1137,6 +1144,7 @@ class PubServer(BaseHTTPRequestHandler):
if '/sharefiles/' in self.path:
if self.path.endswith('.png') or \
self.path.endswith('.jpg') or \
self.path.endswith('.webp') or \
self.path.endswith('.gif'):
mediaStr=self.path.split('/sharefiles/')[1]
mediaFilename= \
@ -1147,6 +1155,8 @@ class PubServer(BaseHTTPRequestHandler):
mediaFileType='png'
elif mediaFilename.endswith('.jpg'):
mediaFileType='jpeg'
elif mediaFilename.endswith('.webp'):
mediaFileType='webp'
else:
mediaFileType='gif'
with open(mediaFilename, 'rb') as avFile:
@ -4347,6 +4357,8 @@ class PubServer(BaseHTTPRequestHandler):
mediaFilename=mediaFilenameBase+'.jpg'
if self.headers['Content-type'].endswith('gif'):
mediaFilename=mediaFilenameBase+'.gif'
if self.headers['Content-type'].endswith('webp'):
mediaFilename=mediaFilenameBase+'.webp'
with open(mediaFilename, 'wb') as avFile:
avFile.write(mediaBytes)
if self.server.debug:

View File

@ -35,7 +35,7 @@ def getImageHash(imageFilename: str) -> str:
return blurencode(numpy.array(Image.open(imageFilename).convert("RGB")))
def isMedia(imageFilename: str) -> bool:
permittedMedia=['png','jpg','gif','mp4','ogv','mp3','ogg']
permittedMedia=['png','jpg','gif','webp','mp4','ogv','mp3','ogg']
for m in permittedMedia:
if imageFilename.endswith('.'+m):
return True
@ -58,7 +58,7 @@ def getAttachmentMediaType(filename: str) -> str:
image, video or audio
"""
mediaType=None
imageTypes=['png','jpg','jpeg','gif']
imageTypes=['png','jpg','jpeg','gif','webp']
for mType in imageTypes:
if filename.endswith('.'+mType):
return 'image'
@ -84,7 +84,7 @@ def attachMedia(baseDir: str,httpPrefix: str,domain: str,port: int, \
return postJson
fileExtension=None
acceptedTypes=['png','jpg','gif','mp4','webm','ogv','mp3','ogg']
acceptedTypes=['png','jpg','gif','webp','mp4','webm','ogv','mp3','ogg']
for mType in acceptedTypes:
if imageFilename.endswith('.'+mType):
if mType=='jpg':

View File

@ -72,6 +72,9 @@ def updateAvatarImageCache(session,baseDir: str,httpPrefix: str,actor: str,avata
elif avatarUrl.endswith('.gif') or '.gif?' in avatarUrl:
sessionHeaders = {'Accept': 'image/gif'}
avatarImageFilename=avatarImagePath+'.gif'
elif avatarUrl.endswith('.webp') or '.webp?' in avatarUrl:
sessionHeaders = {'Accept': 'image/webp'}
avatarImageFilename=avatarImagePath+'.webp'
else:
return None
if not os.path.isfile(avatarImageFilename) or force:
@ -130,6 +133,8 @@ def getPersonAvatarUrl(baseDir: str,personUrl: str,personCache: {}) -> str:
return '/avatars/'+actorStr+'.jpg'
if os.path.isfile(avatarImagePath+'.gif'):
return '/avatars/'+actorStr+'.gif'
if os.path.isfile(avatarImagePath+'.webp'):
return '/avatars/'+actorStr+'.webp'
if os.path.isfile(avatarImagePath):
return '/avatars/'+actorStr
@ -1052,7 +1057,7 @@ def htmlNewPost(translate: {},baseDir: str, \
newPostForm+=' <div class="container">'
newPostForm+=' <input type="text" placeholder="'+translate['Image description']+'" name="imageDescription">'
newPostForm+=' <input type="file" id="attachpic" name="attachpic"'
newPostForm+=' accept=".png, .jpg, .jpeg, .gif, .mp4, .webm, .ogv, .mp3, .ogg">'
newPostForm+=' accept=".png, .jpg, .jpeg, .gif, .webp, .mp4, .webm, .ogv, .mp3, .ogg">'
newPostForm+=' </div>'
newPostForm+=' </div>'
newPostForm+='</form>'
@ -2057,6 +2062,7 @@ def individualPostAsHtml(iconsDir: str,translate: {}, \
if attach['url'].endswith('.png') or \
attach['url'].endswith('.jpg') or \
attach['url'].endswith('.jpeg') or \
attach['url'].endswith('.webp') or \
attach['url'].endswith('.gif'):
if attachmentCtr>0:
attachmentStr+='<br>'