Update.
This commit is contained in:
parent
d2e2bb8117
commit
d23ed3711a
@ -15,11 +15,21 @@ import aiohttp
|
|||||||
import aiohttp.web
|
import aiohttp.web
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
|
|
||||||
|
@aiohttp.web.middleware
|
||||||
|
async def debug_middleware(request, handler):
|
||||||
|
print(request.method, request.path, request.headers)
|
||||||
|
return await handler(request)
|
||||||
|
|
||||||
|
|
||||||
class WebdavApplication(aiohttp.web.Application):
|
class WebdavApplication(aiohttp.web.Application):
|
||||||
def __init__(self, parent, *args, **kwargs):
|
def __init__(self, parent, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
middlewares = [debug_middleware]
|
||||||
|
|
||||||
|
super().__init__(middlewares=middlewares, *args, **kwargs)
|
||||||
self.locks = {}
|
self.locks = {}
|
||||||
|
|
||||||
|
self.relative_url = "/webdav"
|
||||||
|
print(self.router)
|
||||||
|
|
||||||
self.router.add_route("OPTIONS", "/{filename:.*}", self.handle_options)
|
self.router.add_route("OPTIONS", "/{filename:.*}", self.handle_options)
|
||||||
self.router.add_route("GET", "/{filename:.*}", self.handle_get)
|
self.router.add_route("GET", "/{filename:.*}", self.handle_get)
|
||||||
@ -30,8 +40,8 @@ class WebdavApplication(aiohttp.web.Application):
|
|||||||
self.router.add_route("COPY", "/{filename:.*}", self.handle_copy)
|
self.router.add_route("COPY", "/{filename:.*}", self.handle_copy)
|
||||||
self.router.add_route("PROPFIND", "/{filename:.*}", self.handle_propfind)
|
self.router.add_route("PROPFIND", "/{filename:.*}", self.handle_propfind)
|
||||||
self.router.add_route("PROPPATCH", "/{filename:.*}", self.handle_proppatch)
|
self.router.add_route("PROPPATCH", "/{filename:.*}", self.handle_proppatch)
|
||||||
# self.router.add_route("LOCK", "/{filename:.*}", self.handle_lock)
|
self.router.add_route("LOCK", "/{filename:.*}", self.handle_lock)
|
||||||
# self.router.add_route("UNLOCK", "/{filename:.*}", self.handle_unlock)
|
self.router.add_route("UNLOCK", "/{filename:.*}", self.handle_unlock)
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -46,11 +56,11 @@ class WebdavApplication(aiohttp.web.Application):
|
|||||||
# session = request.session
|
# session = request.session
|
||||||
# if session.get('uid'):
|
# if session.get('uid'):
|
||||||
# request['user'] = await self.services.user.get(uid=session['uid'])
|
# request['user'] = await self.services.user.get(uid=session['uid'])
|
||||||
# try:
|
#try:
|
||||||
# request['home'] = await self.services.user.get_home_folder(user_uid=request['user']['uid'])
|
# request['home'] = await self.services.user.get_home_folder(user_uid=request['user']['uid'])
|
||||||
# except:
|
#except:
|
||||||
# pass
|
# pass
|
||||||
# return request['user']
|
#return request['user']
|
||||||
|
|
||||||
auth_header = request.headers.get("Authorization", "")
|
auth_header = request.headers.get("Authorization", "")
|
||||||
if not auth_header.startswith("Basic "):
|
if not auth_header.startswith("Basic "):
|
||||||
@ -66,6 +76,7 @@ class WebdavApplication(aiohttp.web.Application):
|
|||||||
request["user"]["uid"]
|
request["user"]["uid"]
|
||||||
)
|
)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
|
print("GRRRRRRRRRR")
|
||||||
print(ex)
|
print(ex)
|
||||||
pass
|
pass
|
||||||
return request["user"]
|
return request["user"]
|
||||||
@ -98,6 +109,7 @@ class WebdavApplication(aiohttp.web.Application):
|
|||||||
status=401, headers={"WWW-Authenticate": 'Basic realm="WebDAV"'}
|
status=401, headers={"WWW-Authenticate": 'Basic realm="WebDAV"'}
|
||||||
)
|
)
|
||||||
file_path = request["home"] / request.match_info["filename"]
|
file_path = request["home"] / request.match_info["filename"]
|
||||||
|
print("WRITETO_", file_path)
|
||||||
file_path.parent.mkdir(parents=True, exist_ok=True)
|
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
async with aiofiles.open(file_path, "wb") as f:
|
async with aiofiles.open(file_path, "wb") as f:
|
||||||
while chunk := await request.content.read(1024):
|
while chunk := await request.content.read(1024):
|
||||||
@ -195,9 +207,11 @@ class WebdavApplication(aiohttp.web.Application):
|
|||||||
abs_path = pathlib.Path(full_path)
|
abs_path = pathlib.Path(full_path)
|
||||||
relative_path = str(full_path.relative_to(request["home"]))
|
relative_path = str(full_path.relative_to(request["home"]))
|
||||||
|
|
||||||
href_path = f"{relative_path}".strip("/")
|
|
||||||
# href_path = href_path.replace("./","/")
|
href_path = f"{self.relative_url}/{relative_path}".strip(".")
|
||||||
|
href_path = href_path.replace("./","/")
|
||||||
href_path = href_path.replace("//", "/")
|
href_path = href_path.replace("//", "/")
|
||||||
|
|
||||||
response = etree.SubElement(response_xml, "{DAV:}response")
|
response = etree.SubElement(response_xml, "{DAV:}response")
|
||||||
href = etree.SubElement(response, "{DAV:}href")
|
href = etree.SubElement(response, "{DAV:}href")
|
||||||
href.text = href_path
|
href.text = href_path
|
||||||
@ -240,7 +254,7 @@ class WebdavApplication(aiohttp.web.Application):
|
|||||||
etree.SubElement(lock_type_2, "{DAV:}write")
|
etree.SubElement(lock_type_2, "{DAV:}write")
|
||||||
etree.SubElement(propstat, "{DAV:}status").text = "HTTP/1.1 200 OK"
|
etree.SubElement(propstat, "{DAV:}status").text = "HTTP/1.1 200 OK"
|
||||||
|
|
||||||
if abs_path.is_dir() and depth != -1:
|
if abs_path.is_dir():
|
||||||
for item in abs_path.iterdir():
|
for item in abs_path.iterdir():
|
||||||
await self.create_node(request, response_xml, item, depth - 1)
|
await self.create_node(request, response_xml, item, depth - 1)
|
||||||
|
|
||||||
@ -255,7 +269,11 @@ class WebdavApplication(aiohttp.web.Application):
|
|||||||
depth = int(request.headers.get("Depth", "0"))
|
depth = int(request.headers.get("Depth", "0"))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
print(request)
|
||||||
|
|
||||||
requested_path = request.match_info.get("filename", "")
|
requested_path = request.match_info.get("filename", "")
|
||||||
|
|
||||||
abs_path = request["home"] / requested_path
|
abs_path = request["home"] / requested_path
|
||||||
if not abs_path.exists():
|
if not abs_path.exists():
|
||||||
return aiohttp.web.Response(status=404, text="Directory not found")
|
return aiohttp.web.Response(status=404, text="Directory not found")
|
||||||
@ -267,6 +285,7 @@ class WebdavApplication(aiohttp.web.Application):
|
|||||||
xml_output = etree.tostring(
|
xml_output = etree.tostring(
|
||||||
response_xml, encoding="utf-8", xml_declaration=True
|
response_xml, encoding="utf-8", xml_declaration=True
|
||||||
).decode()
|
).decode()
|
||||||
|
print(xml_output)
|
||||||
return aiohttp.web.Response(
|
return aiohttp.web.Response(
|
||||||
status=207, text=xml_output, content_type="application/xml"
|
status=207, text=xml_output, content_type="application/xml"
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user