Skip to content

Commit

Permalink
chore: 添加头像上传功能和S3Api访问权限
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Porter committed Jul 9, 2024
1 parent 0034832 commit 309bae4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 22 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,29 @@ def register():
email = flask.request.form["email"]

password = flask.request.form["password"]
#如果有avatar字段
avatar=flask.request.form.get("avatar")
if User.query.filter_by(email=email).first():
#409
return flask.jsonify({"code": 409, "message": "Email already exists", "data": {}}), 409
user = User(username=username, email=email)
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}")
user.set_password(password)
db.session.add(user)
db.session.commit()
Expand Down Expand Up @@ -252,7 +271,7 @@ def getTaskToken():
task=SyncTask.query.filter_by(id=task_id).first()
if task.user_id!=user.id:
return flask.jsonify({"code": 403, "message": "Permission Denied", "data": {}}), 403
allow_prefix=task.s3Dir
allow_prefix=[task.s3Dir,task.s3Dir+"*"]
if(task.syncType==1):
role="upload_download"
elif(task.syncType==2):
Expand All @@ -272,8 +291,8 @@ def getTaskTokenByS3Dir():
s3Dir=flask.request.args.get("s3Dir")
task=SyncTask.query.filter_by(s3Dir=s3Dir,user_id=user.id).first()
if task==None:
return flask.jsonify({"code": 404, "message": "Task Not Found", "data": {}}), 404
allow_prefix=task.s3Dir
return flask.jsonify({"code": 404, "message": f"Task {s3Dir} Not Found", "data": {}}), 404
allow_prefix=[task.s3Dir,task.s3Dir+"*"]
if(task.syncType==1):
role="upload_download"
elif(task.syncType==2):
Expand Down
2 changes: 2 additions & 0 deletions s3Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def get_actions_list(self, role):
'name/cos:ListParts',
'name/cos:UploadPart',
'name/cos:CompleteMultipartUpload',
'name/cos:PutObjectCopy',
'name/cos:HeadBucket'
]
elif role == 'download':
Expand All @@ -40,6 +41,7 @@ def get_actions_list(self, role):
'name/cos:HeadBucket',
'name/cos:DeleteObject',
'name/cos:PutObject',
'name/cos:PutObjectCopy',
'name/cos:PostObject',
'name/cos:InitiateMultipartUpload',
'name/cos:ListMultipartUploads',
Expand Down

0 comments on commit 309bae4

Please sign in to comment.