Commit fd1e273a authored by iv's avatar iv

Cleanup!

parent 4d7469c0
from flask import Flask, request, redirect, url_for, render_template, make_response, g from itsdangerous import Signer, base64_encode, base64_decode
from flask import Flask, request, render_template, make_response, g
from flask.views import MethodView from flask.views import MethodView
import shutil import shutil
import utils import utils
import os import os
DEBUG = True
app = Flask(__name__.split('.')[0]) app = Flask(__name__.split('.')[0])
app.config.from_object(__name__) app.config.from_object(__name__)
FS_PATH = '/tmp/couscous' FS_PATH = '/tmp/couscous'
SECRET_KEY = os.urandom(24)
def debug(content):
if app.debug: print(content)
URI_BEGINNING_PATH = { URI_BEGINNING_PATH = {
'authorization': '/login/', 'authorization': '/login/',
...@@ -57,7 +59,6 @@ def is_authorized(cookies_list): ...@@ -57,7 +59,6 @@ def is_authorized(cookies_list):
FS_HANDLER = utils.FilesystemHandler(FS_PATH, URI_BEGINNING_PATH['webdav']) FS_HANDLER = utils.FilesystemHandler(FS_PATH, URI_BEGINNING_PATH['webdav'])
@app.before_request @app.before_request
def before_request(): def before_request():
""" """
...@@ -71,14 +72,20 @@ def before_request(): ...@@ -71,14 +72,20 @@ def before_request():
headers['Access-Control-Max-Age'] = '3600' headers['Access-Control-Max-Age'] = '3600'
headers['Access-Control-Allow-Credentials'] = 'true' headers['Access-Control-Allow-Credentials'] = 'true'
content = '' content = ''
headers['Access-Control-Allow-Headers'] = \
'Origin, Accept, Accept-Encoding, Content-Length, Content-Type, ' + \
'Authorization, Depth, If-Modified-Since, If-None-Match'
headers['Access-Control-Expose-Headers'] = \
'Content-Type, Last-Modified, WWW-Authenticate'
if is_authorized(request.cookies): if is_authorized(request.cookies):
headers['Access-Control-Allow-Origin'] = request.headers.get('Origin', '*') headers['Access-Control-Allow-Origin'] = request.headers.get('Origin')
headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Accept-Encoding, Content-Length, Content-Type, Authorization, Depth, If-Modified-Since, If-None-Match'
headers['Access-Control-Expose-Headers'] = 'Content-Type, Last-Modified, WWW-Authenticate'
response = make_response(content, 200) response = make_response(content, 200)
response.headers = headers response.headers = headers
else: else:
headers['WWW-Authenticate'] = 'Nayookie login_url=' + request.url_root + URI_BEGINNING_PATH['authorization'] + '{?back_url}' headers['WWW-Authenticate'] = \
'Nayookie login_url=' + request.url_root + \
URI_BEGINNING_PATH['authorization'] + '{?back_url}'
response = make_response(content, 401) response = make_response(content, 401)
response.headers = headers response.headers = headers
# do not handle the request if not authorized # do not handle the request if not authorized
...@@ -87,7 +94,8 @@ def before_request(): ...@@ -87,7 +94,8 @@ def before_request():
g.response = response g.response = response
class WebDAV(MethodView): class WebDAV(MethodView):
methods = ['GET', 'PUT', 'PROPFIND', 'PROPPATCH', 'MKCOL', 'DELETE', 'COPY', 'MOVE'] methods = ['GET', 'PUT', 'PROPFIND', 'PROPPATCH', 'MKCOL', 'DELETE', 'COPY',
'MOVE']
def __init__(self): def __init__(self):
self.baseuri = URI_BEGINNING_PATH['webdav'] self.baseuri = URI_BEGINNING_PATH['webdav']
...@@ -102,7 +110,6 @@ class WebDAV(MethodView): ...@@ -102,7 +110,6 @@ class WebDAV(MethodView):
request_data = None request_data = None
return request_data return request_data
def get(self, pathname): def get(self, pathname):
""" """
GET: GET:
...@@ -170,7 +177,8 @@ class WebDAV(MethodView): ...@@ -170,7 +177,8 @@ class WebDAV(MethodView):
def mkcol(self, pathname): def mkcol(self, pathname):
""" """
MKCOL: MKCOL:
creates a collection (that corresponds to a directory on the file system) creates a collection (that corresponds to a directory on the file
system)
""" """
response = g.response response = g.response
...@@ -216,13 +224,14 @@ class WebDAV(MethodView): ...@@ -216,13 +224,14 @@ class WebDAV(MethodView):
host = request.headers['Host'] host = request.headers['Host']
destination = destination.split(host + URI_BEGINNING_PATH['webdav'], 1)[-1] destination = destination.split(host + URI_BEGINNING_PATH['webdav'], 1)[-1]
destination_path = FS_HANDLER.uri2local(destination) destination_path = FS_HANDLER.uri2local(destination)
print('COPY: %s -> %s' % (localpath, destination_path)) debug('COPY: %s -> %s' % (localpath, destination_path))
if not os.path.exists(localpath): if not os.path.exists(localpath):
response.status = '404' response.status = '404'
elif not destination_path: elif not destination_path:
response.status = '400' response.status = '400'
elif 'Overwrite' in request.headers and request.headers['Overwrite'] == 'F' and os.path.exists(destination_path): elif 'Overwrite' in request.headers and request.headers['Overwrite'] == 'F' \
and os.path.exists(destination_path):
response.status = '412' response.status = '412'
else: else:
response.status = '201' response.status = '201'
...@@ -234,12 +243,12 @@ class WebDAV(MethodView): ...@@ -234,12 +243,12 @@ class WebDAV(MethodView):
try: try:
shutil.copy2(localpath, destination_path) shutil.copy2(localpath, destination_path)
except Exception: except Exception:
print('problem with copy2') debug('problem with copy2')
else: else:
try: try:
shutil.copytree(localpath, destination_path) shutil.copytree(localpath, destination_path)
except Exception: except Exception:
print('problem with copytree') debug('problem with copytree')
return response return response
def move(self, pathname): def move(self, pathname):
...@@ -258,7 +267,8 @@ class WebDAV(MethodView): ...@@ -258,7 +267,8 @@ class WebDAV(MethodView):
response.status = '424' response.status = '424'
return response return response
app.add_url_rule(URI_BEGINNING_PATH['webdav'] + '<path:pathname>', view_func=WebDAV.as_view('dav')) app.add_url_rule(URI_BEGINNING_PATH['webdav'] + '<path:pathname>',
view_func=WebDAV.as_view('dav'))
@app.route(URI_BEGINNING_PATH['authorization'], methods=['GET', 'POST']) @app.route(URI_BEGINNING_PATH['authorization'], methods=['GET', 'POST'])
...@@ -299,7 +309,8 @@ def system(): ...@@ -299,7 +309,8 @@ def system():
@app.route('/') @app.route('/')
def links(): def links():
the_links = '<div><ul>' the_links = '<div><ul>'
the_links += '\n'.join(['<li>%s: %s </li>' % (key, URI_BEGINNING_PATH[key]) for key in URI_BEGINNING_PATH.keys()]) the_links += '\n'.join(['<li>%s: %s </li>' % (key, URI_BEGINNING_PATH[key])
for key in URI_BEGINNING_PATH.keys()])
the_links += '</ul></div>' the_links += '</ul></div>'
return 'TODO: nice set of links to useful local pages: %s <br> + HOWTO' % the_links return 'TODO: nice set of links to useful local pages: %s <br> + HOWTO' % the_links
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment