From 869d95c2ca91f857a4e44530f0393f2c2a0ce993 Mon Sep 17 00:00:00 2001 From: Alexander-Porter Date: Tue, 9 Jul 2024 15:23:46 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96=E5=A4=B4=E5=83=8F?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/app.py b/app.py index cdfaf8c..2fc38d1 100644 --- a/app.py +++ b/app.py @@ -125,20 +125,23 @@ def updateInfo(): def updateAvatar(): #base64 encoded image user=flask_login.current_user - avatar = flask.request.form["avatar"] - #save base64 encoded image to file - thisUUid=uuid.uuid1() - uuidStr=str(thisUUid) - with open(f"{uuidStr}.png", "wb") as f: - import base64 - avatar=avatar.split(",")[1] - avatar = base64.b64decode(avatar) - f.write(avatar) - myS3.uploadObject(f"avatars/{uuidStr}.png",f"{uuidStr}.png") - user.avatar_url=f"avatars/{uuidStr}.png" - #删除 - import os - os.remove(f"{uuidStr}.png") + avatar=flask.request.form.get("avatar") + if avatar: + thisUUid=uuid.uuid1() + uuidStr=str(thisUUid) + fileType=avatar.split(";")[0].split("/")[1] + with open(f"{uuidStr}.{fileType}", "wb") as f: + import base64 + avatar=avatar.split(",")[1] + avatar=avatar.replace(" ","+") + avatar = base64.b64decode(avatar) + f.write(avatar) + if not fileType in ["png","gif","jpeg"]: + return flask.jsonify({"code": 403, "message": "Bad Avatar File Type", "data": {}}), 403 + myS3.uploadObject(f"avatars/{uuidStr}.{fileType}",f"{uuidStr}.{fileType}") + user.avatar_url=f"avatars/{uuidStr}.{fileType}" + import os + os.remove(f"{uuidStr}.{fileType}") db.session.commit() avatarUrl=myS3.getPreSignUrl(user.avatar_url) data={