From b0b5f1feced3368ebfdb4dde5da43cd30f77c679 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 14 Nov 2019 15:11:20 +0000 Subject: [PATCH] Supporting webp image format --- daemon.py | 12 ++++++++++++ media.py | 6 +++--- webinterface.py | 8 +++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/daemon.py b/daemon.py index 4f500d97a..ac7e3c190 100644 --- a/daemon.py +++ b/daemon.py @@ -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: diff --git a/media.py b/media.py index 0ca9dc932..112ac9e65 100644 --- a/media.py +++ b/media.py @@ -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': diff --git a/webinterface.py b/webinterface.py index a973286c0..d7fd7cf71 100644 --- a/webinterface.py +++ b/webinterface.py @@ -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+='
' newPostForm+=' ' newPostForm+=' 0: attachmentStr+='
'