-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
555 lines (451 loc) · 21.3 KB
/
main.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#!/usr/bin/python
# coding:utf-8
import shutil
from flask import Flask,request,session,redirect,Response
from flask import send_from_directory
from login import *
from thisistest import *
from update import *
import os
import time
import time
from thisdef import *
from share import *
#from sharehtml import *
from manage import *
from serverstatus import *
basedir = os.path.abspath(os.path.dirname(__file__))
app =Flask(__name__)
@app.route('/cloud',methods=['GET'])
def cloud_redirect():
username=request.cookies.get('username')
md5credit=request.cookies.get('credit')
if Checkmd5(md5credit,username)==0:
return redirect('/login')
else:
return redirect('/folder')
@app.route('/folder',methods=['GET'])
def cloud_get():
username =request.cookies.get('username')
md5credit = request.cookies.get('credit')
if Checkmd5(md5credit, username) == 0:
print(Checkmd5(md5credit, username))
return redirect('/login')
else:
path = os.getcwd() + '/cloud/' + username
return (ReturnContentForCloud_ForcurrentFolder(path, username, ''))
@app.route('/folder/<path:folders>',methods=['GET'])
def cloud_folder_get(folders):
username = request.cookies.get('username')
md5credit = request.cookies.get('credit')
if Checkmd5(md5credit, username) ==0:
return redirect('/login')
else:
path = os.getcwd() + '/cloud/' + username + '/' + folders
return (ReturnContentForCloud_ForcurrentFolder(path, username, folders))
@app.route('/file/<path:filename>',methods=['GET'])
def file_get(filename):
username = request.cookies.get('username')
md5credit = request.cookies.get('credit')
if Checkmd5(md5credit, username) == 0:
return redirect('/login')
else:
path = os.getcwd() + '/cloud/' + username
nextpath = checkFilenameAndFolder(filename)[1]
path1 = path + '/' + nextpath
thisisfilename = checkFilenameAndFolder(filename)[0]
if thisisfilename[0:1] == ' ':
if nextpath == 'nodirhere':
return send_from_directory(path, filename=thisisfilename[1:], as_attachment=True)
else:
return send_from_directory(path1, filename=thisisfilename[1:], as_attachment=True)
else:
if nextpath == 'nodirhere':
return send_from_directory(path, filename=thisisfilename, as_attachment=True)
else:
return send_from_directory(path1, filename=thisisfilename, as_attachment=True)
@app.route('/login', methods=['GET'])
def signin_form():
return (loginhtm)
@app.route('/login', methods=['POST'])
def login_post():
username = str(request.form['username'])
password = str(request.form['password'])
if CheckUsername(username) > 1:
if CheckPassword(username, password) > 1:
md5hash = md5(password)
response = redirect('/folder')
response.set_cookie('username', username, max_age=7 * 24 * 3600)
response.set_cookie('credit', md5hash, max_age=7 * 24 * 3600)
return response
else:
return (('error') + '<p>用户名或密码错误</p><br><a href="/login">返回登录</a>')
else:
return (('error') + '<p>用户名或密码错误</p><br><a href="/login">返回登录</a>')
@app.route('/lib/<path:filename>',methods=['GET'])
def lib_get(filename):
path = os.getcwd() + '/' + 'lib'
nextpath = checkFilenameAndFolder(filename)[1]
path1 = path + '/' + nextpath
thisisfilename = checkFilenameAndFolder(filename)[0]
if thisisfilename[0:1] == ' ':
if nextpath == 'nodirhere':
return send_from_directory(path, filename=thisisfilename[1:], as_attachment=True)
else:
return send_from_directory(path1, filename=thisisfilename[1:], as_attachment=True)
else:
if nextpath == 'nodirhere':
return send_from_directory(path, filename=thisisfilename, as_attachment=True)
else:
return send_from_directory(path1, filename=thisisfilename, as_attachment=True)
@app.route('/pan/<path:filename>',methods=['GET'])
def pan_get(filename):
path = os.getcwd() + '/lib/' + 'pan'
nextpath = checkFilenameAndFolder(filename)[1]
path1 = path + '/' + nextpath
thisisfilename = checkFilenameAndFolder(filename)[0]
if thisisfilename[0:1] == ' ':
if nextpath == 'nodirhere':
return send_from_directory(path, filename=thisisfilename[1:], as_attachment=True)
else:
return send_from_directory(path1, filename=thisisfilename[1:], as_attachment=True)
else:
if nextpath == 'nodirhere':
return send_from_directory(path, filename=thisisfilename, as_attachment=True)
else:
return send_from_directory(path1, filename=thisisfilename, as_attachment=True)
@app.route('/folder/pan/<path:filename>',methods=['GET'])
def folder_pan_get(filename):
username = request.cookies.get('username')
md5credit = request.cookies.get('credit')
if Checkmd5(md5credit, username) == 0:
return redirect('/login')
else:
return redirect('/pan/' + filename)
@app.route('/', methods=['GET'])
def login_firstpage():
username = request.cookies.get('username')
md5credit = request.cookies.get('credit')
if Checkmd5(md5credit, username) == 0:
return redirect('/login')
else:
return redirect('/folder')
@app.route('/test',methods=['GET'])
def test_get():
username = request.cookies.get('username')
md5credit = request.cookies.get('credit')
if Checkmd5(md5credit, username) == 0:
return redirect('/login')
else:
return (testdata)
@app.route('/test',methods=['post'])
def test_POST():
username = request.cookies.get('username')
md5credit = request.cookies.get('credit')
if Checkmd5(md5credit, username) == 0:
return redirect('/login')
else:
if request.form['submit'] == '提交':
print('that is awsome')
else:
if request.form['submit'] == '删除':
print('that is cool')
mycheck = request.form.getlist('mycheck')
print(mycheck)
mycheck = request.form.getlist('mycheck')
for i in mycheck:
if i[0:1] == ' ':
i = i[1:]
print(i)
return redirect('/cloud')
@app.route('/folder/<path:folders>', methods=['POST'])
def folderpath_POST(folders):
username = request.cookies.get('username')
md5credit = request.cookies.get('credit')
if Checkmd5(md5credit, username) == 0:
return redirect('/login')
else:
path = os.getcwd() + '/cloud/' + username + '/' + folders
pathtemp = path + '/'
if request.form['formname'] == 'fileorfolderlist':
mycheck = request.form.getlist('mycheck')
if request.form['submit'] == '删除':
for i in mycheck:
if i[0:1] == ' ':
i = i[1:]
try:
os.remove(pathtemp + i)
except:
shutil.rmtree(pathtemp + i)
return ('删除完毕<br>' + ReturnContentForCloud_ForcurrentFolder(path, username, folders))
elif request.form['submit'] == '分享':
fenxiang = 1
for i in mycheck:
if i[0:1] == ' ':
i = i[1:]
pathshare = pathtemp + i
fenxiang = sharefileorfolder(path + '/', username, i)
if fenxiang == 0:
return ('该文件/文件夹已经分享过了<br>' + ReturnContentForCloud_ForcurrentFolder(path, username, folders))
else:
return ('分享成功,请去已分享查询<br>' + ReturnContentForCloud_ForcurrentFolder(path, username, folders))
elif request.form['formname'] == 'search':
pathsearch = path
#print(pathsearch)
searchinfo=request.form['search']
b=searchfilename(searchinfo,pathsearch,username)
return (b)
elif request.form['formname'] == 'newfile':
path = os.getcwd() + '/cloud/' + username + '/' + folders
file = request.files.get('file')
pathwithfilename = path + '/' + file.filename
file.save(pathwithfilename)
return ('文件上传成功,页面应该刷新!<br>' + ReturnContentForCloud_ForcurrentFolder(path, username, folders))
elif request.form['formname'] == 'newfolder':
newfolder = str(request.form['newfoldername'])
pathnew = os.getcwd() + '/cloud/' + username + '/' + folders + '/' + newfolder
path = os.getcwd() + '/cloud/' + username + '/' + folders
# os.system('mkdir -p '+pathnew)
os.mkdir(pathnew, 777)
# print(path)
return ('文件夹创建成功,页面应该刷新!<br>' + ReturnContentForCloud_ForcurrentFolder(path, username, folders))
else:
return ('禁止调皮')
@app.route('/folder',methods=['POST'])
def folderroot_POST():
username = request.cookies.get('username')
md5credit = request.cookies.get('credit')
if Checkmd5(md5credit, username) == 0:
return redirect('/login')
else:
path = os.getcwd().replace('\\', '/') + '/cloud/' + username
pathtemp = path + '/'
if request.form['formname'] == 'fileorfolderlist':
mycheck = request.form.getlist('mycheck')
if request.form['submit'] == '删除':
for i in mycheck:
if i[0:1] == ' ':
i = i[1:]
try:
os.remove(pathtemp + i)
except:
shutil.rmtree(pathtemp + i)
return ('删除完毕<br>' + ReturnContentForCloud_ForcurrentFolder(path, username, ''))
elif request.form['submit'] == '下载':
return ('暂时不支持文件批量下载<br>' + ReturnContentForCloud_ForcurrentFolder(path, username, ''))
elif request.form['submit'] == '分享':
fenxiang = 1
for i in mycheck:
if i[0:1] == ' ':
i = i[1:]
pathshare = pathtemp + i
fenxiang = sharefileorfolder(path + '/', username, i)
if fenxiang == 0:
return ('该文件/文件夹已经分享过了<br>' + ReturnContentForCloud_ForcurrentFolder(path, username, ''))
else:
return ('分享成功,请去已分享查询<br>' + ReturnContentForCloud_ForcurrentFolder(path, username, ''))
elif request.form['formname'] == 'search':
path = os.getcwd().replace('\\', '/') + '/cloud/' + username
searchinfo=request.form['search']
b=searchfilename(searchinfo,path,username)
return (b)
elif request.form['formname'] == 'newfile':
path = os.getcwd() + '/cloud/' + username + '/'
file = request.files.get('file')
pathwithfilename = path + '/' + file.filename
# path='D:\Config'
file.save(pathwithfilename)
return ('文件上传成功,页面应该刷新!<br>' + ReturnContentForCloud_ForcurrentFolder(path, username, ''))
elif request.form['formname'] == 'newfolder':
newfolder = str(request.form['newfoldername'])
pathnew = os.getcwd() + '/cloud/' + username + '/' + newfolder
path = os.getcwd() + '/cloud/' + username
# os.system('mkdir -p ' + pathnew)
os.mkdir(pathnew, 777)
# print(path)
return ('文件夹创建成功,页面应该刷新!<br>' + ReturnContentForCloud_ForcurrentFolder(path, username, ''))
@app.route('/share/<folders>',methods=['GET'])
def share_get(folders):
ifshareid = checksharebyshareid(folders)
if ifshareid == 0:
return ('分享链接无效!')
elif ifshareid == 1:
return ('这是个文件夹')
else:
ifshareid = list(ifshareid)
ifshareid = list(str(ifshareid[0]).replace('(', '').replace(')', '').replace("'", '').split(','))
###以下解决ifshareid[1]开头带空格问题
if ifshareid[1][0:1] == ' ':
ifshareid[1] = ifshareid[1][1:]
return send_from_directory(ifshareid[0], filename=ifshareid[1], as_attachment=True)
@app.route('/shared',methods=['GET'])
def shared_get():
username=request.cookies.get('username')
md5credit = request.cookies.get('credit')
if checkUserislogined(md5credit,username)==0:
return ('你没有登录,' + '''<a href="/"'>点此登录</a>''')
else:
path = os.getcwd() + '/cloud/' + username
return (ReturnContentForCloud_ForcurrentFolder_share(username))
@app.route('/shared', methods=['POST'])
def sharedroot_POST():
username = request.cookies.get('username')
md5credit = request.cookies.get('credit')
if checkUserislogined(md5credit, username) == 0:
return ('你没有登录,' + '''<a href="/"'>点此登录</a>''')
else:
path = os.getcwd().replace('\\', '/') + '/cloud/' + username
pathtemp = path + '/'
if request.form['formname'] == 'fileorfolderlist':
mycheck = request.form.getlist('mycheck')
##永久有效思路,设validto为0,取消分享,即删除shareid存在得条目。
if request.form['submit'] == '设为永久分享':
for i in mycheck:
print(i)
alwaysvalid(i)
return ('设置选定文件(夹)的永久分享完毕<br>' + ReturnContentForCloud_ForcurrentFolder_share(username))
elif request.form['submit'] == '取消分享':
for i in mycheck:
cancelshare(i)
return ('已取消选定文件(夹)的分享<br>' + ReturnContentForCloud_ForcurrentFolder_share(username))
elif request.form['formname']=='search':
searchinfo = request.form['search']
return (searchfromshared(username,searchinfo))
@app.route('/manage',methods=['GET'])
def manager_get():
username=request.cookies.get('username')
md5credit = request.cookies.get('credit')
if checkUserislogined(md5credit,username)==0:
return ('你没有登录,' + '''<a href="/"'>点此登录</a>''')
else:
if Checkisadmin(username) == 0:
return ('非管理员无权访问该页面' + "<a href='/folder'>返回首页</a>")
return (manageUserandServer(username))
@app.route('/manage',methods=['POST'])
def manage_POST():
username = request.cookies.get('username')
md5credit = request.cookies.get('credit')
if checkUserislogined(md5credit, username) == 0:
return ('你没有登录,' + '''<a href="/"'>点此登录</a>''')
else:
if Checkisadmin(username) == 0:
return ('非管理员无权访问该页面' + "<a href='/folder'>返回首页</a>")
path = os.getcwd().replace('\\', '/') + '/cloud/' + username
pathtemp = path + '/'
if request.form['formname'] == 'userlist':
mycheck = request.form.getlist('mycheck')
##永久有效思路,设validto为0,取消分享,即删除shareid存在得条目。
if request.form['submit'] == '删除':
for i in mycheck:
print(i)
if deleteuser(i) == 0:
return ('无法删除默认用户admin<br>' + manageUserandServer(username))
else:
return ('删除选定用户成功<br>' + manageUserandServer(username))
elif request.form['formname'] == 'newuser':
username = request.form['username']
passwd = request.form['password']
if adduser(username, passwd) == 0:
return ('该用户已存在<br>' + manageUserandServer(username))
else:
return ('用户添加成功<br>' + manageUserandServer(username))
elif request.form['formname'] == 'registerstatus':
signupstate=str(request.form['registercode'])
print(signupstate)
with open('static/signupstate', 'w', encoding='utf-8') as f:
f.write(signupstate)
print('1')
return('开/关注册 设置成功<br>' + manageUserandServer(username))
elif request.form['formname']=='registerbyinvitecode':
invitecode=str(request.form['invitecode'])
with open('static/invitecode', 'w', encoding='utf-8') as f:
f.write(invitecode)
return('开/关邀请码注册 设置成功<br>' + manageUserandServer(username))
@app.route('/serverstatus', methods=['GET'])
def serverstatus_get():
notice=''
username = request.cookies.get('username')
md5credit = request.cookies.get('credit')
if checkupdate()!=0:
if checkupdate()==-1:
notice='版本检测失败,可能是网络无法连接,请检查服务器日志。<br>'
else:
notice='有新版本'+str(checkupdate())+'可更新<br>'
if checkUserislogined(md5credit, username) == 0:
return ('你没有登录,' + '''<a href="/"'>点此登录</a>''')
else:
if Checkisadmin(username) == 0:
return ('非管理员无权访问该页面' + "<a href='/folder'>返回首页</a>")
return (notice+returnsysteminfo())
@app.route('/logout',methods=['POST','GET'])
def logout():
response=redirect('/login')
response.delete_cookie('username')
response.delete_cookie('credit')
return response
@app.route('/logedout',methods=['POST','GET'])
def logedout():
return ('成功注销<a href="/login">返回登录</a>')
@app.route('/updateversion',methods=['GET'])
def updateversion_get():
username = request.cookies.get('username')
md5credit = request.cookies.get('credit')
if Checkmd5(md5credit, username) == 0:
return redirect('/login')
updateversion()
return (title_setup_pc('升级成功')+'升级成功!请返回首页<br><a href="/">返回首页</a>')
@app.route('/signup',methods=['POST','GET'])
def signup():
if request.method == 'GET':
signupstate = ''
invitecode = ''
with open('static/signupstate', 'r', encoding='utf-8') as f:
signupstate = f.read()
with open('static/invitecode', 'r', encoding='utf-8') as f:
invitecode = f.read()
if str(signupstate) != '1':
return ('管理员关闭了注册功能,请联系管理员<br><a href="/">返回首页</a>')
else:
if str(invitecode) == '0':
return ("""
<title>注册</title><form action="/signup" method="post">
<input type="hidden" style="visibility: hidden;" name="formname" value="usernamepasswd" />
<p>用户名:<input name="username"></p>
<p>密码:   <input name="password" type="password"></p>
<p><button type="submit">注册</button></p>
</form>
""")
else:
return(
"""
<title>注册</title><form action="/signup" method="post">
<input type="hidden" style="visibility: hidden;" name="formname" value="invitecode" />
<p>用户名:<input name="username"></p>
<p>密码:   <input name="password" type="password"></p>
<p>邀请码:<input name="invitecode"></p>
<p><button type="submit">注册</button></p>
</form>
""")
if request.method=='POST':
if request.form['formname']=='usernamepasswd':
username=request.form['username']
password=request.form['password']
if adduser(username, password) == 0:
return ('该用户已存在,无法注册<br>' + '<a href="/signup">点此重新注册</a>')
else:
return ('注册成功 <br><a href="/">点此登录</a>')
elif request.form['formname']=='invitecode':
username = request.form['username']
password = request.form['password']
incominginvitecode=request.form['invitecode']
invitecode=''
with open('static/invitecode', 'r', encoding='utf-8') as f:
invitecode = f.read()
if incominginvitecode==invitecode:
adduser(username,password)
return ('注册成功 <br><a href="/">点此登录</a>')
@app.route('/favicon',methods=['GET'])
def get_fav():
return app.send_static_file('favicon.png')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=True,threaded=True)