-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
399 lines (359 loc) · 13.8 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import datetime
import flask
import flask_login
import os
import uuid
import logging # Add this line to import the logging module
from flask_sqlalchemy import SQLAlchemy
from dotenv import load_dotenv
from s3Utils import S3Utils
load_dotenv()
app = flask.Flask(__name__)
app.secret_key = os.getenv("SECRET_KEY") # 从.env文件读取
# 使用环境变量配置数据库
db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")
db_name = os.getenv("DB_NAME")
db_host = os.getenv("DB_HOST")
if db_host==None:
db_host="mysql"
if db_user==None:
db_user="root"
app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+mysqlconnector://{db_user}:{db_password}@{db_host}/{db_name}"
logger = logging.getLogger(__name__) # Add this line to define the logger object
myS3=S3Utils(os.getenv("S3_SECRET_ID"), os.getenv("S3_SECRET_KEY"), os.getenv("S3_BUCKET"),os.getenv("S3_APPID"), os.getenv("S3_REGION"))
login_manager = flask_login.LoginManager()
login_manager.init_app(app)
db = SQLAlchemy(app)
from werkzeug.security import generate_password_hash,check_password_hash
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), unique=False, nullable=False)
email = db.Column(db.String(100), unique=True, nullable=False)
password_hash = db.Column(db.String(200), nullable=False)
avatar_url = db.Column(db.String(200))
tasks = db.relationship('SyncTask', backref='user', lazy=True)
devices = db.relationship('Device', backref='user', lazy=True)
is_active = db.Column(db.Boolean, default=True)
__table_args__ = { 'mysql_charset' : 'utf8mb4'}
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
def get_id(self):
return self.id
def is_authenticated(self):
return True
class Device(db.Model):
id = db.Column(db.Integer, primary_key=True)
deviceName = db.Column(db.String(100), nullable=False)
lastOnline = db.Column(db.DateTime, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
__table_args__ = { 'mysql_charset' : 'utf8mb4'}
class SyncTask(db.Model):
id = db.Column(db.Integer, primary_key=True)
localDir = db.Column(db.String(200), nullable=False)
s3Dir = db.Column(db.String(200), nullable=False)
#syncType is an integer
syncType = db.Column(db.Integer, nullable=False)
usedSize = db.Column(db.BigInteger, nullable=False)
totalSize = db.Column(db.BigInteger, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
created_at = db.Column(db.DateTime, nullable=False)
__table_args__ = { 'mysql_charset' : 'utf8mb4'}
@login_manager.user_loader
def user_loader(id):
return db.session.get(User, id)
@app.route("/login", methods=["GET", "POST"])
def login():
if flask.request.method == "POST":
email = flask.request.form["email"]
password = flask.request.form["password"]
print(email,password,generate_password_hash(password))
user = User.query.filter_by(email=email).first()
if user and check_password_hash(user.password_hash,password):
flask_login.login_user(user)
return flask.redirect(flask.url_for("profile"))
else:
#403
return flask.jsonify({"code": 403, "message": "Email or password incorrect", "data": {}}), 403
else:
#method not allowed
# Set status code to 405
return flask.jsonify({"code": 405, "message": "Method Not Allowed", "data": {}}), 405
@app.route("/info", methods=["GET"])
@flask_login.login_required
def profile():
user=flask_login.current_user
if(user.avatar_url==None):
avatarUrl=None
else:
avatarUrl=myS3.getPreSignUrl(user.avatar_url)
data={
"username":user.username,
"email":user.email,
"avatar_url":avatarUrl,
"tasks":[]
}
return flask.jsonify({"code": 200, "message": "success", "data": data})
@app.route("/updateInfo", methods=["POST"])
@flask_login.login_required
def updateInfo():
user=flask_login.current_user
username = flask.request.form["username"]
password = flask.request.form["password"]
user.username=username
user.set_password(password)
db.session.commit()
#200
return flask.jsonify({"code": 200, "message": "success", "data": {}})
@app.route("/updateAvatar", methods=["POST"])
@flask_login.login_required
def updateAvatar():
#base64 encoded image
user=flask_login.current_user
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()
if user.avatar_url==None:
avatarUrl=None
else:
avatarUrl=myS3.getPreSignUrl(user.avatar_url)
data={
"avatar_url":avatarUrl
}
#200
return flask.jsonify({"code": 200, "message": "success", "data": data})
@app.route("/register", methods=["POST"])
def register():
#step=flask.request.form["step"]
username = flask.request.form["username"]
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)
#md5 email
import hashlib
hashed_email=hashlib.md5(email.encode()).hexdigest()
myS3.createDir(f"{hashed_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()
#201
return flask.jsonify({"code": 201, "message": "success", "data": {}}), 201
@app.route("/deleteAccount",methods=["get"])
@flask_login.login_required
def deleteAccount():
user=flask_login.current_user
db.session.delete(user)
db.session.commit()
#200
return flask.jsonify({"code": 200, "message": "success", "data": {}})
@app.route("/tasks",methods=["get"])
@flask_login.login_required
def getTasks():
user=flask_login.current_user
tasks=SyncTask.query.filter_by(user_id=user.id)
data=[]
for task in tasks:
data.append({
"localDir":task.localDir,
"s3Dir":task.s3Dir,
"syncType":task.syncType,
"usedSize":task.usedSize,
"totalSize":task.totalSize,
"created_at":task.created_at,
"id":task.id
})
return flask.jsonify({"code": 200, "message": "success", "data": data})
@app.route("/addTask",methods=["post"])
@flask_login.login_required
def addTask():
user=flask_login.current_user
localDir=flask.request.form["localDir"]
s3Dir=flask.request.form["s3Dir"]
syncType=flask.request.form["syncType"]
usedSize=flask.request.form["usedSize"]
totalSize=flask.request.form["totalSize"]
#否则,检查S3目录是否存在
import hashlib
hashed_email=hashlib.md5(user.email.encode()).hexdigest()
realS3Dir=f"{hashed_email}/{s3Dir}"
if not myS3.isValidDir(realS3Dir):
return flask.jsonify({"code": 403, "message": "Bad S3 Dir Name", "data": {}}), 403
if not myS3.isObjectExist(realS3Dir):
try:
myS3.createDir(realS3Dir)
except Exception as e:
return flask.jsonify({"code": 500, "message": e, "data": {}}), 500
mySameTask=SyncTask.query.filter_by(localDir=localDir, s3Dir=s3Dir, user_id=user.id).first()
if mySameTask:
return flask.jsonify({"code": 201, "message": "Same Task Already Exists", "data": {}}), 201
created_at = datetime.datetime.now()
task = SyncTask(localDir=localDir, s3Dir=s3Dir, syncType=syncType, usedSize=usedSize, totalSize=totalSize, user_id=user.id, created_at=created_at)
db.session.add(task)
db.session.commit()
#get this task and return
task = SyncTask.query.filter_by(localDir=localDir, s3Dir=s3Dir, user_id=user.id).first()
data={
"localDir":task.localDir,
"s3Dir":task.s3Dir,
"syncType":task.syncType,
"usedSize":task.usedSize,
"totalSize":task.totalSize,
"created_at":task.created_at,
"id":task.id
}
#201
return flask.jsonify({"code": 201, "message": "success", "data": data}), 201
@app.route("/deleteTask",methods=["get"])
@flask_login.login_required
def deleteTask():
user=flask_login.current_user
task_id=flask.request.args.get("task_id")
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
db.session.delete(task)
db.session.commit()
#200
return flask.jsonify({"code": 200, "message": "success", "data": {}})
@app.route("/getTaskToken",methods=["get"])
@flask_login.login_required
def getTaskToken():
user=flask_login.current_user
task_id=flask.request.args.get("task_id")
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
import hashlib
hashed_email=hashlib.md5(user.email.encode()).hexdigest()
realS3Dir=f"{hashed_email}/{task.s3Dir}"
allow_prefix=[realS3Dir,realS3Dir+"*"]
if(task.syncType==1):
role="upload_download"
elif(task.syncType==2):
role="upload"
else:
role="download"
try:
data=myS3.get_credential_demo(allow_prefix,role)
except Exception as e:
return flask.jsonify({"code": 500, "message": e, "data": {}}), 500
return flask.jsonify({"code": 200, "message": "success", "data": data})
@app.route("/getTaskUnifiedToken",methods=["get"])
@flask_login.login_required
def getTaskUnifiedToken():
user=flask_login.current_user
import hashlib
hashed_email=hashlib.md5(user.email.encode()).hexdigest()
realS3Dir=f"{hashed_email}/"
try:
data=myS3.getUnifyToken([],[],[realS3Dir+"*"])
return flask.jsonify({"code": 200, "message": "success", "data": data})
except Exception as e:
return flask.jsonify({"code": 500, "message": e, "data": {}}), 500
@app.route("/updateDevice",methods=["post"])
@flask_login.login_required
def updateDevice():
user=flask_login.current_user
deviceName=flask.request.form["deviceName"]
#import time and use standard ts
import datetime
lastOnline=datetime.datetime.now()
device=Device.query.filter_by(user_id=user.id,deviceName=deviceName).first()
if device:
device.lastOnline=lastOnline
else:
device=Device(deviceName=deviceName,lastOnline=lastOnline,user_id=user.id)
db.session.add(device)
db.session.commit()
#201
return flask.jsonify({"code": 201, "message": "success", "data": {}}), 201
@app.route("/getDevices",methods=["get"])
@flask_login.login_required
def getDevices():
user=flask_login.current_user
devices=Device.query.filter_by(user_id=user.id)
data=[]
for device in devices:
data.append({
"deviceName":device.deviceName,
"lastOnline":device.lastOnline
})
return flask.jsonify({"code": 200, "message": "success", "data": data})
@app.route("/deleteDevice",methods=["get"])
@flask_login.login_required
def deleteDevice():
user=flask_login.current_user
deviceName=flask.request.args.get("deviceName")
device=Device.query.filter_by(user_id=user.id,deviceName=deviceName).first()
if device:
db.session.delete(device)
db.session.commit()
#200
return flask.jsonify({"code": 200, "message": "success", "data": {}})
@app.route("/logout")
@flask_login.login_required
def logout():
flask_login.logout_user()
#200
return flask.jsonify({"code": 200, "message": "success", "data": {}})
@app.route("/s3Config",methods=["get"])
@flask_login.login_required
def s3Config():
user=flask_login.current_user
import hashlib
hashed_email=hashlib.md5(user.email.encode()).hexdigest()
data={
"bucket":os.getenv("S3_BUCKET"),
"region":os.getenv("S3_REGION"),
"appid":os.getenv("S3_APPID"),
"allowPrefix":f"{hashed_email}/"
}
return flask.jsonify({"code": 200, "message": "success", "data": data})
#全局修饰器,程序抛出异常会返回500
@app.errorhandler(500)
def internal_error(error):
#转成字符串
error=str(error)
return flask.jsonify({"code": 500, "message": error, "data": {}}), 500
with app.app_context():
db.create_all()
if __name__ == "__main__":
app.run(debug=True)