-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
151 lines (108 loc) · 4.35 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
# -*- coding: UTF-8 -*-
from flask import Flask,Response,request,redirect,render_template,session,send_file
from os import path,makedirs,walk,getcwd,remove
from time import strftime,localtime
from codecs import open
from json import dumps,loads
from random import choice,sample
def pull(arg,error=None):
return request.args.get(arg,error)
def save(path_,mode,text):
open(path_,mode,"UTF-8-sig").write(text)
def load(path_):
return open(path_,"r","UTF-8-sig").read()
def test(list_):
return "".join(list_).translate(table).isalnum()
def result(code,type_,text,arg={"head":"作者:PAPEREE(ee) 来自:DHQZ"}):
return Response(dumps({"code":code,"type":type_,"text":text,**arg},ensure_ascii=False),mimetype="application/json")
table=str.maketrans("", "", ".-+=_/|~")
method=["POST","GET"]
config=loads(load("config.json").replace("'", '"'))
app=Flask(str(id("DHQZ")),static_folder="templates",static_url_path="/templates")
app.secret_key="".join(sample("NeverGonnaGiveYouUp",12))
@app.errorhandler(Exception)
def error(e):
return redirect(f"https://1145.s3.ladydaily.com/{choice(['rock','cxk'])}.mp4")
@app.route("/")
def index():
if request.args:
return redirect(request.base_url)
if "user" not in session:
return render_template("login.html")
user=session["user"]
data=list()
for root,dirs,files in walk(f"{getcwd()}/homework/{'/'.join(user)}"):
for file in files:
data.append(path.join(root,file).replace("\\","/").split("/")[-1].split("_",1)[-1])
return render_template("index.html",user="".join(user),data=" ".join(data))
@app.route("/login",methods=method)
def login():
form=list(request.form.values())
if not all(form):
return result(0,"login","小分队信息不完善")
if not test(form):
return result(0,"login","无效的小分队名称/密码")
form[-2]+="小分队"
user_="".join(form[:-1])
pwd=config.get(user_)
if pwd:
if form[-1]!=pwd:
return result(0,"login","密码错误")
else:
config[user_]=form[-1]
save("config.json","w",str(config))
session["user"]=form[:-1]
return result(1,"login","注册/登陆成功",{"user":user_})
@app.route("/leave",methods=method)
def leave():
session.pop("user")
return redirect("/")
@app.route("/upload",methods=method)
def upload():
if "user" not in session:
return result(0,"upload","无效的登陆状态")
files=request.files.getlist("files[]",None)
files_=list()
if not files:
return result(0,"upload","你传了个寂寞")
if int(request.content_length)>1*1024*1024:
return result(0,"upload","无效的文件大小")
if not test([i.filename for i in files]):
return result(0,"upload","无效的文件名称")
ip=request.headers.get("X-Forwarded-For",request.remote_addr)
user=session["user"]
time=strftime("%Y.%m.%d %H:%M:%S",localtime())
path_=f"homework/{'/'.join(user)}"
makedirs(path_,exist_ok=True)
for file in files:
file.save(f"{path_}/{''.join(user)}_{file.filename}")
files_.append(file.filename)
return result(1,"upload","文件上传成功",{"files":files_,"time":time})
@app.route("/delete",methods=method)
def delete():
if "user" not in session:
return result(0,"delete","无效的登陆状态")
filename=pull("filename")
if not filename:
return result(0,"delete","请输入文件名称")
user=session["user"]
path_=f"homework/{'/'.join(user)}/{''.join(user)}_{filename}"
time=strftime("%Y.%m.%d %H:%M:%S",localtime())
if not path.isfile(path_):
return result(0,"delete","无效的文件路径")
remove(path_)
return result(1,"delete","文件删除成功",{"file":filename,"time":time})
@app.route("/download/<path:filename>",methods=method)
def download(filename):
if "user" not in session:
return result(0,"download","无效的登陆状态")
user=session["user"]
path_=f"homework/{'/'.join(user)}/{''.join(user)}_{filename}"
if not path.isfile(path_):
return result(0,"download","无效的文件路径")
try:
return send_file(path_,as_attachment=True,attachment_filename=filename)
except:
return send_file(path_,as_attachment=True,download_name=filename)
if __name__=="__main__":
app.run(host="0.0.0.0",port=1919,debug=True,threaded=True)