Added error handling for image upload

This commit is contained in:
krateng 2022-10-19 19:53:13 +02:00
parent ddc78c5756
commit a7d286c90c
2 changed files with 16 additions and 1 deletions

View File

@ -72,6 +72,14 @@ errors = {
'desc':"The database is being upgraded. Please try again later."
}
}),
images.MalformedB64: lambda e: (400,{
"status":"failure",
"error":{
'type':'malformed_b64',
'value':None,
'desc':"The provided base 64 string is not valid."
}
}),
# for http errors, use their status code
Exception: lambda e: ((e.status_code if hasattr(e,'statuscode') else 500),{
"status":"failure",

View File

@ -267,6 +267,9 @@ def local_files(artist=None,artists=None,title=None):
class MalformedB64(Exception):
pass
def set_image(b64,**keys):
track = "title" in keys
if track:
@ -279,7 +282,10 @@ def set_image(b64,**keys):
log("Trying to set image, b64 string: " + str(b64[:30] + "..."),module="debug")
regex = r"data:image/(\w+);base64,(.+)"
type,b64 = re.fullmatch(regex,b64).groups()
match = re.fullmatch(regex,b64)
if not match: raise MalformedB64()
type,b64 = match.groups()
b64 = base64.b64decode(b64)
filename = "webupload" + str(int(datetime.datetime.now().timestamp())) + "." + type
for folder in get_all_possible_filenames(**keys):
@ -293,6 +299,7 @@ def set_image(b64,**keys):
with open(data_dir['images'](folder,filename),"wb") as f:
f.write(b64)
log("Saved image as " + data_dir['images'](folder,filename),module="debug")
# set as current picture in rotation