Support different image formats

merge-requests/6/head
Bob Mottram 2019-11-14 13:30:54 +00:00
parent 9af5129fcb
commit b94d1e3877
3 changed files with 65 additions and 11 deletions

View File

@ -440,11 +440,13 @@ def saveMediaInFormPOST(mediaBytes,debug: bool, \
'png': 'image/png',
'jpeg': 'image/jpeg',
'gif': 'image/gif',
'webp': 'image/webp',
'mp4': 'video/mp4',
'ogv': 'video/ogv',
'mp3': 'audio/mpeg',
'ogg': 'audio/ogg'
}
detectedExtension=None
for extension,contentType in extensionList.items():
searchStr=b'Content-Type: '+contentType.encode('utf8', 'ignore')
mediaLocation=mediaBytes.find(searchStr)
@ -457,6 +459,7 @@ def saveMediaInFormPOST(mediaBytes,debug: bool, \
filename=filenameBase+'.'+extension
attachmentMediaType= \
searchStr.decode().split('/')[0].replace('Content-Type: ','')
detectedExtension=extension
break
if not filename:
@ -471,6 +474,16 @@ def saveMediaInFormPOST(mediaBytes,debug: bool, \
startPos+=offset
break
# remove any existing image files with a different format
extensionTypes=('png','jpg','jpeg','gif','webp')
if detectedExtension in extensionTypes:
for ex in extensionTypes:
if ex==detectedExtension:
continue
possibleOtherFormat=filename.replace('.'+detectedExtension,'.'+ex)
if os.path.isfile(possibleOtherFormat):
os.remove(possibleOtherFormat)
fd = open(filename, 'wb')
fd.write(mediaBytes[startPos:])
fd.close()

View File

@ -999,9 +999,12 @@ class PubServer(BaseHTTPRequestHandler):
return
# image on login screen
if self.path=='/login.png':
if self.path=='/login.png' or \
self.path=='/login.gif' or \
self.path=='/login.jpeg' or \
self.path=='/login.jpg':
mediaFilename= \
self.server.baseDir+'/accounts/login.png'
self.server.baseDir+'/accounts'+self.path
if os.path.isfile(mediaFilename):
tries=0
mediaBinary=None
@ -3222,6 +3225,7 @@ class PubServer(BaseHTTPRequestHandler):
# extract each image type
actorChanged=True
profileMediaTypes=['avatar','image','banner','instanceLogo']
profileMediaTypesUploaded={}
for mType in profileMediaTypes:
if self.server.debug:
print('DEBUG: profile update extracting '+mType+' image from POST')
@ -3260,7 +3264,10 @@ class PubServer(BaseHTTPRequestHandler):
removeMetaData(filename,postImageFilename)
if os.path.isfile(postImageFilename):
print('profile update POST '+mType+' image saved to '+postImageFilename)
actorChanged=True
if mType!='instanceLogo':
lastPartOfImageFilename=postImageFilename.split('/')[-1]
profileMediaTypesUploaded[mType]=lastPartOfImageFilename
actorChanged=True
else:
print('ERROR: profile update POST '+mType+' image could not be saved to '+postImageFilename)
@ -3276,7 +3283,21 @@ class PubServer(BaseHTTPRequestHandler):
nickname+'@'+self.server.domain+'.json'
if os.path.isfile(actorFilename):
actorJson=loadJson(actorFilename)
if actorJson:
if actorJson:
# update the avatar/image url file extension
for mType,lastPartOfImageFilename in profileMediaTypesUploaded.items():
if mType=='avatar':
lastPartOfUrl=actorJson['icon']['url'].split('/')[-1]
actorJson['icon']['url']= \
actorJson['icon']['url'].replace('/'+lastPartOfUrl, \
'/'+lastPartOfImageFilename)
elif mType=='image':
lastPartOfUrl=actorJson['image']['url'].split('/')[-1]
actorJson['image']['url']= \
actorJson['image']['url'].replace('/'+lastPartOfUrl, \
'/'+lastPartOfImageFilename)
skillCtr=1
newSkills={}
while skillCtr<10:

View File

@ -505,6 +505,7 @@ def htmlSkillsSearch(translate: {},baseDir: str, \
def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int) -> str:
"""Shows the edit profile screen
"""
imageFormats='.png .jpg .jpeg .gif .webp'
pathOriginal=path
path=path.replace('/inbox','').replace('/outbox','').replace('/shares','')
nickname=getNicknameFromActor(path)
@ -601,7 +602,7 @@ def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int)
instanceStr+=' <textarea id="message" name="instanceDescription" style="height:200px">'+instanceDescription+'</textarea>'
instanceStr+=' '+translate['Instance Logo']
instanceStr+=' <input type="file" id="instanceLogo" name="instanceLogo"'
instanceStr+=' accept=".png">'
instanceStr+=' accept="'+imageFormats+'">'
instanceStr+='</div>'
moderators=''
@ -633,13 +634,13 @@ def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int)
editProfileForm+=' '+translate['The files attached below should be no larger than 10MB in total uploaded at once.']+'<br>'
editProfileForm+=' '+translate['Avatar image']
editProfileForm+=' <input type="file" id="avatar" name="avatar"'
editProfileForm+=' accept=".png">'
editProfileForm+=' accept="'+imageFormats+'">'
editProfileForm+=' <br>'+translate['Background image']
editProfileForm+=' <input type="file" id="image" name="image"'
editProfileForm+=' accept=".png">'
editProfileForm+=' accept="'+imageFormats+'">'
editProfileForm+=' <br>'+translate['Timeline banner image']
editProfileForm+=' <input type="file" id="banner" name="banner"'
editProfileForm+=' accept=".png">'
editProfileForm+=' accept="'+imageFormats+'">'
editProfileForm+=' </div>'
editProfileForm+=' <div class="container">'
editProfileForm+=translate['Change Password']+'<br>'
@ -706,8 +707,26 @@ def htmlLogin(translate: {},baseDir: str,autocomplete=True) -> str:
"""
accounts=noOfAccounts(baseDir)
if not os.path.isfile(baseDir+'/accounts/login.png'):
copyfile(baseDir+'/img/login.png',baseDir+'/accounts/login.png')
loginImage='login.png'
loginImageFilename=None
if os.path.isfile(os.path.isfile(baseDir+'/accounts/'+loginImage)):
loginImageFilename=baseDir+'/accounts/'+loginImage
if os.path.isfile(os.path.isfile(baseDir+'/accounts/login.jpg')):
loginImage='login.jpg'
loginImageFilename=baseDir+'/accounts/'+loginImage
if os.path.isfile(os.path.isfile(baseDir+'/accounts/login.jpeg')):
loginImage='login.jpeg'
loginImageFilename=baseDir+'/accounts/'+loginImage
if os.path.isfile(os.path.isfile(baseDir+'/accounts/login.gif')):
loginImage='login.gif'
loginImageFilename=baseDir+'/accounts/'+loginImage
if os.path.isfile(os.path.isfile(baseDir+'/accounts/login.webp')):
loginImage='login.webp'
loginImageFilename=baseDir+'/accounts/'+loginImage
if not loginImageFilename:
loginImageFilename=baseDir+'/accounts/'+loginImage
copyfile(baseDir+'/img/login.png',loginImageFilename)
if os.path.isfile(baseDir+'/img/login-background.png'):
if not os.path.isfile(baseDir+'/accounts/login-background.png'):
copyfile(baseDir+'/img/login-background.png',baseDir+'/accounts/login-background.png')
@ -747,10 +766,11 @@ def htmlLogin(translate: {},baseDir: str,autocomplete=True) -> str:
if not autocomplete:
autocompleteStr='autocomplete="off" value=""'
loginForm=htmlHeader(cssFilename,loginCSS)
loginForm+='<form method="POST" action="/login">'
loginForm+=' <div class="imgcontainer">'
loginForm+=' <img loading="lazy" src="login.png" alt="login image" class="loginimage">'
loginForm+=' <img loading="lazy" src="'+loginImage+'" alt="login image" class="loginimage">'
loginForm+=loginText+TOSstr
loginForm+=' </div>'
loginForm+=''