From 3d8cdca5cd42de292b2c6a4bc33d284480f71b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sun, 22 Oct 2023 12:49:04 +0200 Subject: [PATCH] make .txt work again --- api/ovdashboard_api/core/settings.py | 2 +- api/ovdashboard_api/core/webdav.py | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/api/ovdashboard_api/core/settings.py b/api/ovdashboard_api/core/settings.py index e361a96..35181a5 100644 --- a/api/ovdashboard_api/core/settings.py +++ b/api/ovdashboard_api/core/settings.py @@ -50,7 +50,7 @@ class WebDAVSettings(DAVSettings): username: str = "ovd_user" password: str = "password" - config_filename: str = "config.toml" + config_filename: str = "config.txt" disable_check: bool = False retries: int = 20 diff --git a/api/ovdashboard_api/core/webdav.py b/api/ovdashboard_api/core/webdav.py index fdaed1d..6e71eac 100644 --- a/api/ovdashboard_api/core/webdav.py +++ b/api/ovdashboard_api/core/webdav.py @@ -5,6 +5,7 @@ from io import BytesIO from asyncify import asyncify from cache import AsyncTTL from cache.key import KEY +from requests import Response from webdav3.client import Client as WebDAVclient from .settings import SETTINGS @@ -13,7 +14,24 @@ _logger = logging.getLogger(__name__) 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_login": SETTINGS.webdav.username, @@ -72,7 +90,8 @@ class WebDAV: _logger.debug(f"read_bytes {path!r}") 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() @@ -92,7 +111,7 @@ class WebDAV: """ _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: # hack: zugehörigen Cache-Eintrag entfernen