-
Notifications
You must be signed in to change notification settings - Fork 832
/
views.py
212 lines (188 loc) · 6.94 KB
/
views.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
import errno
import grp
import json
import os
import psutil
import pwd
import shutil
from jadi import component
from aj.api.http import url, HttpPlugin
from aj.api.endpoint import endpoint, EndpointError, EndpointReturn
from aj.auth import authorize
@component(HttpPlugin)
class Handler(HttpPlugin):
def __init__(self, context):
self.context = context
@url(r'/api/filesystem/mountpoints')
@authorize('filesystem:read')
@endpoint(api=True)
def handle_api_fs_mountpoints(self, http_context):
return [x.mountpoint for x in psutil.disk_partitions()]
@url(r'/api/filesystem/read/(?P<path>.+)')
@authorize('filesystem:read')
@endpoint(api=True)
def handle_api_fs_read(self, http_context, path=None):
if not os.path.exists(path):
return http_context.respond_not_found()
try:
content = open(path).read()
if http_context.query:
encoding = http_context.query.get('encoding', None)
if encoding:
content = content.decode(encoding).encode('utf-8')
return content
except OSError as e:
http_context.respond_server_error()
return json.dumps({'error': str(e)})
@url(r'/api/filesystem/write/(?P<path>.+)')
@authorize('filesystem:write')
@endpoint(api=True)
def handle_api_fs_write(self, http_context, path=None):
try:
content = http_context.body
if http_context.query:
encoding = http_context.query.get('encoding', None)
if encoding:
content = content.decode('utf-8').encode(encoding)
with open(path, 'w') as f:
f.write(content)
except OSError as e:
raise EndpointError(e)
@url(r'/api/filesystem/upload')
@authorize('filesystem:write')
@endpoint(page=True)
def handle_api_fs_upload_chunk(self, http_context):
id = http_context.query['flowIdentifier']
chunk_index = http_context.query['flowChunkNumber']
chunk_dir = '/tmp/upload-%s' % id
try:
os.makedirs(chunk_dir)
except:
pass
chunk_path = os.path.join(chunk_dir, chunk_index)
if http_context.method == 'GET':
if os.path.exists(chunk_path):
http_context.respond('200 OK')
else:
http_context.respond('204 No Content')
else:
with open(chunk_path, 'w') as f:
f.write(http_context.query['file'])
http_context.respond('200 OK')
return ''
@url(r'/api/filesystem/finish-upload')
@authorize('filesystem:write')
@endpoint(api=True)
def handle_api_fs_finish_upload(self, http_context):
name = http_context.json_body()['name']
path = http_context.json_body()['path']
id = http_context.json_body()['id']
chunk_dir = '/tmp/upload-%s' % id
target = os.path.join(path, name.replace('/', ''))
with open(target, 'wb') as f:
for i in range(len(os.listdir(chunk_dir))):
f.write(open(os.path.join(chunk_dir, str(i + 1))).read())
shutil.rmtree(chunk_dir)
return target
@url(r'/api/filesystem/list/(?P<path>.+)')
@authorize('filesystem:read')
@endpoint(api=True)
def handle_api_fs_list(self, http_context, path=None):
if not os.path.exists(path):
raise EndpointReturn(404)
try:
items = []
for name in os.listdir(path):
item_path = os.path.join(path, name)
data = {
'name': name,
'path': item_path,
'isDir': os.path.isdir(item_path),
'isFile': os.path.isfile(item_path),
'isLink': os.path.islink(item_path),
}
try:
stat = os.stat(item_path)
data.update({
'mode': stat.st_mode,
'mtime': stat.st_mtime,
'uid': stat.st_uid,
'gid': stat.st_gid,
'size': stat.st_size,
})
except OSError as e:
data['accessError'] = str(e)
if e.errno == errno.ENOENT and os.path.islink(item_path):
data['brokenLink'] = True
items.append(data)
return {
'parent': os.path.dirname(os.path.normpath(path)) if path != '/' else None,
'items': items,
}
except OSError as e:
raise EndpointError(e)
@url(r'/api/filesystem/stat/(?P<path>.+)')
@authorize('filesystem:read')
@endpoint(api=True)
def handle_api_fs_stat(self, http_context, path=None):
if not os.path.exists(path):
raise EndpointReturn(404)
data = {
'name': os.path.split(path)[1],
'path': path,
'isDir': os.path.isdir(path),
'isFile': os.path.isfile(path),
'isLink': os.path.islink(path),
'readAccess': os.access(path, os.R_OK),
'writeAccess': os.access(path, os.W_OK),
'executeAccess': os.access(path, os.X_OK),
}
try:
stat = os.stat(path)
data.update({
'mode': stat.st_mode,
'mtime': stat.st_mtime,
'uid': stat.st_uid,
'gid': stat.st_gid,
'size': stat.st_size,
})
try:
data['user'] = pwd.getpwuid(stat.st_uid).pw_name
except KeyError:
pass
try:
data['group'] = grp.getgrgid(stat.st_gid).gr_name
except KeyError:
pass
except OSError as e:
data['accessError'] = str(e)
if e.errno == errno.ENOENT and os.path.islink(path):
data['brokenLink'] = True
return data
@url(r'/api/filesystem/chmod/(?P<path>.+)')
@authorize('filesystem:write')
@endpoint(api=True)
def handle_api_fs_chmod(self, http_context, path=None):
if not os.path.exists(path):
raise EndpointReturn(404)
data = json.loads(http_context.body)
try:
os.chmod(path, data['mode'])
except OSError as e:
raise EndpointError(e)
@url(r'/api/filesystem/create-file/(?P<path>.+)')
@authorize('filesystem:write')
@endpoint(api=True)
def handle_api_fs_create_file(self, http_context, path=None):
try:
os.mknod(path, int('644', 8))
except OSError as e:
raise EndpointError(e)
@url(r'/api/filesystem/create-directory/(?P<path>.+)')
@authorize('filesystem:write')
@endpoint(api=True)
def handle_api_fs_create_directory(self, http_context, path=None):
try:
os.makedirs(path)
except OSError as e:
raise EndpointError(e)