main
Bob Mottram 2021-07-09 21:53:49 +01:00
parent d1b1e18af4
commit e8553eb192
2 changed files with 44 additions and 52 deletions

View File

@ -208,6 +208,8 @@ from shares import addShare
from shares import removeShare from shares import removeShare
from shares import expireShares from shares import expireShares
from categories import setHashtagCategory from categories import setHashtagCategory
from utils import getImageExtensionFromMimeType
from utils import getImageMimeType
from utils import hasObjectDict from utils import hasObjectDict
from utils import userAgentDomain from utils import userAgentDomain
from utils import isLocalNetworkAddress from utils import isLocalNetworkAddress
@ -3234,18 +3236,9 @@ class PubServer(BaseHTTPRequestHandler):
return return
mediaFilenameBase = accountsDir + '/upload' mediaFilenameBase = accountsDir + '/upload'
mediaFilename = mediaFilenameBase + '.png' mediaFilename = \
imageMedia = { mediaFilenameBase + '.' + \
'jpeg': 'jpg', getImageExtensionFromMimeType(self.headers['Content-type'])
'gif': 'gif',
'svg+xml': 'svg',
'webp': 'webp',
'avif': 'avif'
}
for mimeExt, ext in imageMedia.items():
if self.headers['Content-type'].endswith(mimeExt):
mediaFilename = mediaFilenameBase + '.' + ext
break
with open(mediaFilename, 'wb') as avFile: with open(mediaFilename, 'wb') as avFile:
avFile.write(mediaBytes) avFile.write(mediaBytes)
if debug: if debug:
@ -6004,23 +5997,11 @@ class PubServer(BaseHTTPRequestHandler):
self._304() self._304()
return return
mediaImageType = 'png' mediaImageType = getImageMimeType(emojiFilename)
if emojiFilename.endswith('.png'):
mediaImageType = 'png'
elif emojiFilename.endswith('.jpg'):
mediaImageType = 'jpeg'
elif emojiFilename.endswith('.webp'):
mediaImageType = 'webp'
elif emojiFilename.endswith('.avif'):
mediaImageType = 'avif'
elif emojiFilename.endswith('.svg'):
mediaImageType = 'svg+xml'
else:
mediaImageType = 'gif'
with open(emojiFilename, 'rb') as avFile: with open(emojiFilename, 'rb') as avFile:
mediaBinary = avFile.read() mediaBinary = avFile.read()
self._set_headers_etag(emojiFilename, self._set_headers_etag(emojiFilename,
'image/' + mediaImageType, mediaImageType,
mediaBinary, None, mediaBinary, None,
self.server.domainFull) self.server.domainFull)
self._write(mediaBinary) self._write(mediaBinary)
@ -10059,23 +10040,11 @@ class PubServer(BaseHTTPRequestHandler):
self._304() self._304()
return True return True
mediaFileType = 'png' mediaFileType = getImageMimeType(mediaFilename)
if mediaFilename.endswith('.png'):
mediaFileType = 'png'
elif mediaFilename.endswith('.jpg'):
mediaFileType = 'jpeg'
elif mediaFilename.endswith('.webp'):
mediaFileType = 'webp'
elif mediaFilename.endswith('.avif'):
mediaFileType = 'avif'
elif mediaFilename.endswith('.svg'):
mediaFileType = 'svg+xml'
else:
mediaFileType = 'gif'
with open(mediaFilename, 'rb') as avFile: with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read() mediaBinary = avFile.read()
self._set_headers_etag(mediaFilename, self._set_headers_etag(mediaFilename,
'image/' + mediaFileType, mediaFileType,
mediaBinary, None, mediaBinary, None,
self.server.domainFull) self.server.domainFull)
self._write(mediaBinary) self._write(mediaBinary)
@ -10128,18 +10097,7 @@ class PubServer(BaseHTTPRequestHandler):
# The file has not changed # The file has not changed
self._304() self._304()
return True return True
mediaImageType = 'image/png' mediaImageType = getImageMimeType(avatarFile)
extensionsToMime = {
'jpg': 'jpeg',
'gif': 'gif',
'avif': 'avif',
'svg': 'svg+xml',
'webp': 'webp'
}
for ext, mimeExt in extensionsToMime.items():
if avatarFile.endswith('.' + ext):
mediaImageType = 'image/' + mimeExt
break
with open(avatarFilename, 'rb') as avFile: with open(avatarFilename, 'rb') as avFile:
mediaBinary = avFile.read() mediaBinary = avFile.read()
self._set_headers_etag(avatarFilename, self._set_headers_etag(avatarFilename,

View File

@ -238,6 +238,40 @@ def getImageExtensions() -> []:
return ('png', 'jpg', 'jpeg', 'gif', 'webp', 'avif', 'svg') return ('png', 'jpg', 'jpeg', 'gif', 'webp', 'avif', 'svg')
def getImageMimeType(imageFilename: str) -> str:
"""Returns the mime type for the given image
"""
extensionsToMime = {
'png': 'png',
'jpg': 'jpeg',
'gif': 'gif',
'avif': 'avif',
'svg': 'svg+xml',
'webp': 'webp'
}
for ext, mimeExt in extensionsToMime.items():
if imageFilename.endswith('.' + ext):
return 'image/' + mimeExt
return 'image/png'
def getImageExtensionFromMimeType(contentType: str) -> str:
"""Returns the image extension from a mime type, such as image/jpeg
"""
imageMedia = {
'png': 'png',
'jpeg': 'jpg',
'gif': 'gif',
'svg+xml': 'svg',
'webp': 'webp',
'avif': 'avif'
}
for mimeExt, ext in imageMedia.items():
if contentType.endswith(mimeExt):
return ext
return 'png'
def getVideoExtensions() -> []: def getVideoExtensions() -> []:
"""Returns a list of the possible video file extensions """Returns a list of the possible video file extensions
""" """