-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilemanager.py
151 lines (144 loc) · 6.64 KB
/
filemanager.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
FileName: filemanager.py
Author:[email protected]
Create date: 19, Sept. 2014
Description:the core operations of web interactions
'''
import tornado.web
import settings
from tornado.web import MissingArgumentError
from logic import *
class BrowserHandler(tornado.web.RequestHandler):
filemanager=Filemanager({
"server_root":settings.BASE_PATH.replace(os.path.sep,'/'),
"filemanager_root":settings.FILE_MANAGER_PATH.replace(os.path.sep,'/')
})
def getvar(self, var, preserve = None):
fm=self.__class__.filemanager
try:
arg=self.get_query_argument(var)
if arg:
fm.get[var] = fm.sanitize(arg, preserve)
return True
else:
fm.error(fm.lang('INVALID_VAR') % var)
return False
except MissingArgumentError:
fm.error(fm.lang('INVALID_VAR') % var)
return False
def postvar(self, var, sanitize = True):
fm=self.__class__.filemanager
try:
arg=self.get_body_argument(var)
if not arg or (var != 'content' and arg==''):
fm.error(fm.lang('INVALID_VAR') % var)
return False
else:
if(sanitize):
fm.post[var] = fm.sanitize(arg)
else:
fm.post[var] = arg
return True
except MissingArgumentError:
fm.error(fm.lang('INVALID_VAR') % var)
return False
def get(self):
fm=self.__class__.filemanager
fm.setParams(self.request.full_url())
fm.user_ip=self.request.remote_ip
try:
mode=self.get_query_argument('mode')
if mode:
if mode=="getinfo": #done
if self.getvar('path'):
self.write(json.dumps(fm.getinfo()))
elif mode=="getfolder": #need fix
if self.getvar('path'):
self.write(json.dumps(fm.getfolder()))
elif mode=="rename":
if self.getvar('old') and self.getvar('new'):
self.write(json.dumps(fm.rename()))
elif mode=="move":
# allow "../"
if self.getvar('old') and self.getvar('new', 'parent_dir') and self.getvar('root'):
self.write(json.dumps(fm.move()))
elif mode=="editfile": #done
if self.getvar('path'):
self.write(json.dumps(fm.editfile()))
elif mode=='delete': #done
if self.getvar('path'):
self.write(json.dumps(fm.delete()))
elif mode=="addfolder":
if self.getvar('path') and self.getvar('name'):
self.write(json.dumps(fm.addfolder()))
elif mode=="download": #done
if self.getvar('path'):
file_name=fm.download()
if not isinstance(file_name,str):
self.write(json.dumps(file_name))
else:
buf_size = 4096
self.set_header('Content-Type','application/octet-stream')
self.set_header("Content-Transfer-Encoding","Binary");
self.set_header("Content-length",os.path.getsize(file_name))
self.set_header('Content-Disposition','attachment; filename="'+ os.path.split(file_name)[-1] + '"')
with open(file_name, 'rb') as f:
while True:
data = f.read(buf_size)
if not data:
break
self.write(data)
self.finish()
elif mode=="preview": #done
if self.getvar('path'):
try:
self.get_query_argument('thumbnail')
thumbnail = True
except MissingArgumentError:
thumbnail = False
finally:
file_name = fm.preview(thumbnail)
if not isinstance(file_name,str):
self.write(json.dumps(file_name))
else:
buf_size = 4096
self.set_header('Content-Type', 'image/'+(os.path.splitext(file_name)[-1]).lstrip('.').lower())
self.set_header("Content-Transfer-Encoding","Binary")
self.set_header("Content-length",os.path.getsize(file_name))
self.set_header('Content-Disposition','inline; filename="' + os.path.split(file_name)[-1] + '"')
with open(file_name, 'rb') as f:
while True:
data = f.read(buf_size)
if not data:
break
self.write(data)
self.finish()
elif mode=="maxuploadfilesize":
fm.getMaxUploadFileSize()
else:
self.write(json.dumps(fm.error(fm.lang('MODE_ERROR'))))
except MissingArgumentError:
fm.loadLanguageFile()
self.render("filemanager.html")
def post(self):
fm=self.__class__.filemanager
fm.setParams(self.request.full_url())
fm.user_ip=self.request.remote_ip
try:
mode=self.get_body_argument('mode')
if mode:
if mode=="add":
if self.postvar('currentpath'):
self.write('<textarea>'+json.dumps(fm.add(self.request.files))+'</textarea>')
elif mode=="replace":
if self.postvar('newfilepath'):
self.write('<textarea>'+json.dumps(fm.replace(self.request.files))+'</textarea>')
elif mode=="savefile":
if self.postvar('content', False) and self.postvar('path'):
self.write(json.dumps(fm.savefile()))
else:
self.write(json.dumps(fm.error(fm.lang('MODE_ERROR'))))
except MissingArgumentError:
self.write(json.dumps(fm.error(fm.lang('INVALID_ACTION'))))