40 lines
1.7 KiB
Plaintext
40 lines
1.7 KiB
Plaintext
from PDAPI import *
|
|
|
|
|
|
__metadata__ = {
|
|
"methods": ["POST"],
|
|
"blueprint": "UGM",
|
|
"permission": 1.0,
|
|
"attributes": [],
|
|
"limit": "1 per 10 second"
|
|
}
|
|
|
|
def HandleRequest():
|
|
ELTUser: GERPUser = flask.g.ELTUser
|
|
if "file" not in flask.request.files or not flask.request.files["file"].filename:
|
|
return flask.jsonify({"code": 1, "msg": "文件流不存在"})
|
|
File = flask.request.files["file"]
|
|
FileBytes = File.stream.read()
|
|
if len(FileBytes) > 5 * 1024 * 1024:
|
|
return flask.jsonify({"code": 1, "msg": "文件大小不能超过5MB"})
|
|
if not FileBytes:
|
|
return flask.jsonify({"code": 1, "msg": "文件为空"})
|
|
AvatarRecord = GetELTUserAvatar() or GERPUserAvatar(UserUUID=ELTUser.UserUUID)
|
|
try:
|
|
with Image.open(BytesIO(FileBytes)) as Img:
|
|
if Img.format not in ['PNG', 'JPEG', 'BMP']:
|
|
return flask.jsonify({"code": 1, "msg": "图片格式不支持"})
|
|
Img.thumbnail((256, 256), Image.Resampling.LANCZOS)
|
|
# ResizedImg = Img.resize((256, 256), Image.Resampling.LANCZOS) # 强制尺寸
|
|
OutputStream = BytesIO()
|
|
Img.save(OutputStream, format=Img.format, quality=85, optimize=True)
|
|
OutputStream.seek(0)
|
|
AvatarRecord.UserAvatar = OutputStream.read()
|
|
db.session.add(AvatarRecord)
|
|
db.session.commit()
|
|
return flask.jsonify({"code": 0, "msg": "修改成功"})
|
|
except (IOError, SyntaxError, ValueError):
|
|
return flask.jsonify({"code": 1, "msg": "非图片或图片损坏"})
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
return flask.jsonify({"code": 1, "msg": f"未知错误:{str(e)}"}) |