Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions file_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,37 @@ def get(self, p=''):
else:
res = make_response('Not found', 404)
return res

def put(self, p=''):
if request.cookies.get('auth_cookie') == key:
path = os.path.join(root, p)
dir_path = os.path.dirname(path)
Path(dir_path).mkdir(parents=True, exist_ok=True)

info = {}
if os.path.isdir(dir_path):
try:
filename = secure_filename(os.path.basename(path))
with open(os.path.join(dir_path, filename), 'wb') as f:
f.write(request.stream.read())
except Exception as e:
info['status'] = 'error'
info['msg'] = str(e)
else:
info['status'] = 'success'
info['msg'] = 'File Saved'
else:
info['status'] = 'error'
info['msg'] = 'Invalid Operation'
res = make_response(json.JSONEncoder().encode(info), 201)
res.headers.add('Content-type', 'application/json')
else:
info = {}
info['status'] = 'error'
info['msg'] = 'Authentication failed'
res = make_response(json.JSONEncoder().encode(info), 401)
res.headers.add('Content-type', 'application/json')
return res

def post(self, p=''):
if request.cookies.get('auth_cookie') == key:
Expand Down Expand Up @@ -169,6 +200,37 @@ def post(self, p=''):
res = make_response(json.JSONEncoder().encode(info), 401)
res.headers.add('Content-type', 'application/json')
return res

def delete(self, p=''):
if request.cookies.get('auth_cookie') == key:
path = os.path.join(root, p)
dir_path = os.path.dirname(path)
Path(dir_path).mkdir(parents=True, exist_ok=True)

info = {}
if os.path.isdir(dir_path):
try:
filename = secure_filename(os.path.basename(path))
os.remove(os.path.join(dir_path, filename))
os.rmdir(dir_path)
except Exception as e:
info['status'] = 'error'
info['msg'] = str(e)
else:
info['status'] = 'success'
info['msg'] = 'File Deleted'
else:
info['status'] = 'error'
info['msg'] = 'Invalid Operation'
res = make_response(json.JSONEncoder().encode(info), 204)
res.headers.add('Content-type', 'application/json')
else:
info = {}
info['status'] = 'error'
info['msg'] = 'Authentication failed'
res = make_response(json.JSONEncoder().encode(info), 401)
res.headers.add('Content-type', 'application/json')
return res

path_view = PathView.as_view('path_view')
app.add_url_rule('/', view_func=path_view)
Expand Down