Compare commits

...

2 commits

Author SHA1 Message Date
2ca6377634 refac: webdav files prefixing issue 2023-10-22 12:49:34 +02:00
3d8cdca5cd make .txt work again 2023-10-22 12:49:04 +02:00
2 changed files with 24 additions and 5 deletions

View file

@ -50,7 +50,7 @@ class WebDAVSettings(DAVSettings):
username: str = "ovd_user" username: str = "ovd_user"
password: str = "password" password: str = "password"
config_filename: str = "config.toml" config_filename: str = "config.txt"
disable_check: bool = False disable_check: bool = False
retries: int = 20 retries: int = 20

View file

@ -5,6 +5,7 @@ from io import BytesIO
from asyncify import asyncify from asyncify import asyncify
from cache import AsyncTTL from cache import AsyncTTL
from cache.key import KEY from cache.key import KEY
from requests import Response
from webdav3.client import Client as WebDAVclient from webdav3.client import Client as WebDAVclient
from .settings import SETTINGS from .settings import SETTINGS
@ -13,7 +14,24 @@ _logger = logging.getLogger(__name__)
class WebDAV: class WebDAV:
_webdav_client = WebDAVclient( class __WebDAVclient(WebDAVclient):
def execute_request(
self,
action,
path,
data=None,
headers_ext=None,
) -> Response:
res = super().execute_request(action, path, data, headers_ext)
# the "Content-Length" header can randomly be missing on txt files,
# this should fix that (probably serverside bug)
if action == "download" and "Content-Length" not in res.headers:
res.headers["Content-Length"] = str(len(res.text))
return res
_webdav_client = __WebDAVclient(
{ {
"webdav_hostname": SETTINGS.webdav.url, "webdav_hostname": SETTINGS.webdav.url,
"webdav_login": SETTINGS.webdav.username, "webdav_login": SETTINGS.webdav.username,
@ -41,7 +59,7 @@ class WebDAV:
_logger.debug(f"list_files {directory!r}") _logger.debug(f"list_files {directory!r}")
ls = await asyncify(cls._webdav_client.list)(directory) ls = await asyncify(cls._webdav_client.list)(directory)
return [f"{directory}/{path}" for path in ls if regex.search(path)] return [path for path in ls if regex.search(path)]
@classmethod @classmethod
@AsyncTTL( @AsyncTTL(
@ -72,7 +90,8 @@ class WebDAV:
_logger.debug(f"read_bytes {path!r}") _logger.debug(f"read_bytes {path!r}")
buffer = BytesIO() buffer = BytesIO()
await asyncify(cls._webdav_client.resource(path).write_to)(buffer) await asyncify(cls._webdav_client.download_from)(buffer, path)
buffer.seek(0)
return buffer.read() return buffer.read()
@ -92,7 +111,7 @@ class WebDAV:
""" """
_logger.debug(f"write_bytes {path!r}") _logger.debug(f"write_bytes {path!r}")
await asyncify(cls._webdav_client.resource(path).read_from)(buffer) await asyncify(cls._webdav_client.upload_to)(buffer, path)
try: try:
# hack: zugehörigen Cache-Eintrag entfernen # hack: zugehörigen Cache-Eintrag entfernen