Skip to content

Commit 59f1d19

Browse files
authored
Merge pull request #12 from Atridis/master
support of PUT requests for upload
2 parents 6240fd1 + 3e68949 commit 59f1d19

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

file_server.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,37 @@ def get(self, p=''):
138138
else:
139139
res = make_response('Not found', 404)
140140
return res
141+
142+
def put(self, p=''):
143+
if request.cookies.get('auth_cookie') == key:
144+
path = os.path.join(root, p)
145+
dir_path = os.path.dirname(path)
146+
Path(dir_path).mkdir(parents=True, exist_ok=True)
147+
148+
info = {}
149+
if os.path.isdir(dir_path):
150+
try:
151+
filename = secure_filename(os.path.basename(path))
152+
with open(os.path.join(dir_path, filename), 'wb') as f:
153+
f.write(request.stream.read())
154+
except Exception as e:
155+
info['status'] = 'error'
156+
info['msg'] = str(e)
157+
else:
158+
info['status'] = 'success'
159+
info['msg'] = 'File Saved'
160+
else:
161+
info['status'] = 'error'
162+
info['msg'] = 'Invalid Operation'
163+
res = make_response(json.JSONEncoder().encode(info), 201)
164+
res.headers.add('Content-type', 'application/json')
165+
else:
166+
info = {}
167+
info['status'] = 'error'
168+
info['msg'] = 'Authentication failed'
169+
res = make_response(json.JSONEncoder().encode(info), 401)
170+
res.headers.add('Content-type', 'application/json')
171+
return res
141172

142173
def post(self, p=''):
143174
if request.cookies.get('auth_cookie') == key:
@@ -169,6 +200,37 @@ def post(self, p=''):
169200
res = make_response(json.JSONEncoder().encode(info), 401)
170201
res.headers.add('Content-type', 'application/json')
171202
return res
203+
204+
def delete(self, p=''):
205+
if request.cookies.get('auth_cookie') == key:
206+
path = os.path.join(root, p)
207+
dir_path = os.path.dirname(path)
208+
Path(dir_path).mkdir(parents=True, exist_ok=True)
209+
210+
info = {}
211+
if os.path.isdir(dir_path):
212+
try:
213+
filename = secure_filename(os.path.basename(path))
214+
os.remove(os.path.join(dir_path, filename))
215+
os.rmdir(dir_path)
216+
except Exception as e:
217+
info['status'] = 'error'
218+
info['msg'] = str(e)
219+
else:
220+
info['status'] = 'success'
221+
info['msg'] = 'File Deleted'
222+
else:
223+
info['status'] = 'error'
224+
info['msg'] = 'Invalid Operation'
225+
res = make_response(json.JSONEncoder().encode(info), 204)
226+
res.headers.add('Content-type', 'application/json')
227+
else:
228+
info = {}
229+
info['status'] = 'error'
230+
info['msg'] = 'Authentication failed'
231+
res = make_response(json.JSONEncoder().encode(info), 401)
232+
res.headers.add('Content-type', 'application/json')
233+
return res
172234

173235
path_view = PathView.as_view('path_view')
174236
app.add_url_rule('/', view_func=path_view)

0 commit comments

Comments
 (0)