-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zfiles.py
208 lines (196 loc) · 6.63 KB
/
Zfiles.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
#!/usr/bin/env python
# coding=utf-8
import os
import torndb
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado import escape
#setting
from tornado.options import define, options
define("port", default=8888,help="run port",type=int)
define("mysql_host", default="127.0.0.1:3306", help="db host")
define("mysql_database", default="Zfile", help="db name")
define("mysql_user", default="root", help="db user")
define("mysql_password", default="123", help="db password")
TEMPLATE_PATH = os.path.join(os.path.dirname(__file__), "templates")
STATIC_PATH = os.path.join(os.path.dirname(__file__), "static")
UPLOADED_FILES = os.path.join(os.path.dirname(__file__),"files")
class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
return self.get_secure_cookie("user")
def check_user(self):
if not self.current_user:
self.redirect("/login")
return
def _print(self, page_name, base =None):
pass
class MyRequestHandler(tornado.web.RequestHandler):
def check_xsrf_cookie(self):
pass
def user_auth(name, pw=None):
db = torndb.Connection(
host = options.mysql_host,
database = options.mysql_database,
user = options.mysql_user,
password = options.mysql_password
)
user = db.get("select * from user where username=%s", name)
print(user)
if not pw:
if user == None:
return False
return True
else:
print(pw, user['username']);
if pw==user['password']:
return True
else:
return False
return False
#Account Action
class RegisterHandler(BaseHandler):
def get(self):
self.render("register.html")
def post(self):
db = self.application.db
name = self.get_argument("username")
pw = self.get_argument("password")
res = user_auth(name)
print("%s", res);
if not res:
sql = "INSERT INTO user(username, password)VALUES(%s,%s)"
db.insert(sql, name, pw)
self.set_secure_cookie("user", name)
user_path = os.path.join(UPLOADED_FILES, name)
os.mkdir(user_path)
self.redirect("/disk")
else:
self.redirect("/register")
class HomeHandler(BaseHandler):
def get(self):
cookie = self.get_current_user()
db = self.application.db
files = db.query("select * from files order by last_modify limit 10")
self.render("index.html", cookieName = cookie, recent = files)
def post(self):
name = self.get_argument("username")
pw = self.get_argument("password")
res = user_auth(name,pw)
print(res)
if res :
self.set_secure_cookie("user", name)
self.redirect("/disk")
else:
self.redirect("/")
class LogoutHandler(BaseHandler):
def get(self):
self.set_secure_cookie("user", 'None')
self.redirect("/")
class DiskHandler(BaseHandler):
def get(self):
self.check_user()
db = self.application.db
user = self.get_current_user()
files = db.query("select * from files where username = %s order by last_modify",user)
self.render("disk.html", files = files, cookieName = user)
class UploadHandler(BaseHandler):
def get(self):
self.write('''
<html>
<head><title>Upload File</title></head>
<body>
<form action='upload' enctype="multipart/form-data" method ='post'>
<input type='file' name='file'/><br/>
<input type='submit' value ='submit'/>
</form>
</body>
</html>
''' )
def post(self):
user = self.get_current_user()
user_path= os.path.join(UPLOADED_FILES, user)
file_metas = self.request.files['file']
db = self.application.db
for meta in file_metas:
filename = meta['filename']
filepath = os.path.join(user_path, filename)
with open(filepath,'wb') as up:
up.write(meta['body'])
sql = 'insert into files(filename, fileaddress, username)values("%s","%s","%s")'%(filename, filepath, user)
db.execute(sql)
self.redirect("/disk")
class RemoveHandler(BaseHandler):
def get(self,input):
self.check_user()
user = self.get_current_user()
file = input
user_path = os.path.join(UPLOADED_FILES,user)
file_path = os.path.join(user_path, file)
os.remove(file_path)
db = self.application.db
sql = 'delete from files where filename="%s" and username="%s"'%(file, user)
db.execute(sql)
self.redirect("/disk")
class DownloadHandler(BaseHandler):
def get(self,input):
user = self.get_current_user()
file = input
user_path = os.path.join(UPLOADED_FILES,user)
file_path = os.path.join(user_path, file)
with open(file_path,'rb') as f:
while True:
data = f.read()
if not data:
break
self.write(data)
self.finish()
self.redirect("/disk")
'''
#File Action
class FileHandler(BaseHandler):
def __init__(self):
def index(self):
def upload(self):
def download(self):
def remove(self):
'''
class Application(tornado.web.Application):
def __init__(self):
'''
handlers = [
(r"/", IndexHandler),
(r"/account/register", RegisterHandler)
(r"/account/login", Login)
]
'''
handlers =[
(r"/", HomeHandler),
(r"/register", RegisterHandler),
(r"/logout", LogoutHandler),
(r"/disk", DiskHandler),
(r"/upload", UploadHandler),
(r"/download/(\w+)", DownloadHandler),
(r"/remove/(\w+)", RemoveHandler),
]
settings = dict(
template_path = TEMPLATE_PATH,
static_path = STATIC_PATH,
debug = True,
cookie_secret = "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo="
)
tornado.web.Application.__init__(self, handlers, **settings)
self.db = torndb.Connection(
host = options.mysql_host,
database = options.mysql_database,
user = options.mysql_user,
password = options.mysql_password
)
def main():
tornado.options.parse_command_line()
app = tornado.httpserver.HTTPServer(Application())
app.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()