Skip to content

Commit

Permalink
making uploadhandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-D committed Oct 24, 2015
1 parent 12beccb commit 2690259
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 69 deletions.
13 changes: 9 additions & 4 deletions Zfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

class BaseHandler(tornado.web.RequestHandler):
resp = None
db = self.application.db
template_path = TEMPLATE_PATH
static = STATIC_PATH
def get_curret_user(self):
return self.get_secure_cookie("user")
def check_user(self):
Expand All @@ -43,13 +46,15 @@ def check_xsrf_cookie(self):
class HomeHandler(BaseHandler):
def get(self):
self.check_user()
self.render("static/index.html")
self.render("templates/index.html")
#Account Action
class AccountHandler(BaseHandler):
def login(self):

self.check_user()
self.render("templates/disk")
self.render("templates/login")
def login_auth(self):

def log_out(self):

def register(self):
Expand Down Expand Up @@ -77,7 +82,7 @@ def __init__(self):
'''
handlers =[
(r"/", HomeHandler),
(r"/account/(.*)", AccountHandler),
(r"/account/login", AccountHandler.login),
]
settings = dict(
template_path = TEMPLATE_PATH,
Expand Down
42 changes: 40 additions & 2 deletions Zfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def post(self):
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")
Expand All @@ -91,13 +93,48 @@ class LogoutHandler(BaseHandler):
def get(self):
self.set_secure_cookie("user", 'None')
self.redirect("/")
class DiskHandler():
class DiskHandler(BaseHandler):
def get(self):
self.check_user()
db = self.application.db
user = self.get_current_user()
files = db.query("select * from files order by last_modify limit 10")
self.render("disk.html", files = files)
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='file' 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.excute(sql)
class RemoveHandler(BaseHandler):
def get(self):
self.check_user()
user = self.get_current_user()
file = self.get_argument('file_name')
os.remove(UPLOADED_FILES+"/"+ user + file)

class DownloadHandler(BaseHandler):
def post(self):
pass

'''
#File Action
Expand Down Expand Up @@ -125,6 +162,7 @@ def __init__(self):
(r"/", HomeHandler),
(r"/register", RegisterHandler),
(r"/logout", LogoutHandler),
(r"/disk", DiskHandler),
]
settings = dict(
template_path = TEMPLATE_PATH,
Expand Down
55 changes: 0 additions & 55 deletions app.py

This file was deleted.

7 changes: 0 additions & 7 deletions controller.py

This file was deleted.

2 changes: 1 addition & 1 deletion templates/disk.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<li><a href='/' class='h4 text-primary'>首页</a></li>
<li>欢迎:{{cookieName}}</a></li>
<li><a href='/logout' class='h4 text-primary'>注销</a></li>
{%end%}
</ul>
{%end%}
{%block content%}
<ul>
{%for i in files%}
<li><a href='/disk' class='text-success h4'>【{{i['username']}}】</a><a href='/files/{{i['username']}}/{{i['filename']}}' class='text-primary h3'>{{i['filename']}}</a></p><p class='text-muted h6'>{{i['last_modify']}}</p></li>
<li><a href='/disk/remove' class='h4 text-primary'> Remove</li>
<br/>
{%end%}
</ul>
Expand Down

0 comments on commit 2690259

Please sign in to comment.