From abedef158397e48d07422ad8982d036870c91f84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Mon, 19 Sep 2022 11:42:43 +0000 Subject: [PATCH] Upload files from skel directory --- api/ovdashboard_api/dav_common.py | 46 ++++++++++++++++++++++-- api/ovdashboard_api/routers/v1/file.py | 8 +++-- api/ovdashboard_api/routers/v1/image.py | 10 ++++-- api/ovdashboard_api/routers/v1/text.py | 9 ++++- api/ovdashboard_api/routers/v1/ticker.py | 7 +++- 5 files changed, 72 insertions(+), 8 deletions(-) diff --git a/api/ovdashboard_api/dav_common.py b/api/ovdashboard_api/dav_common.py index 521faad..08e2f05 100644 --- a/api/ovdashboard_api/dav_common.py +++ b/api/ovdashboard_api/dav_common.py @@ -4,6 +4,8 @@ Definition of WebDAV and CalDAV clients. from functools import lru_cache from logging import getLogger +from os import path +from pathlib import Path from time import sleep from typing import Any, Iterator @@ -12,6 +14,7 @@ from caldav import Principal as CalDAVPrincipal from webdav3.client import Client as WebDAVclient from webdav3.client import Resource as WebDAVResource +from . import __file__ as OVD_INIT from .async_helpers import run_in_executor from .settings import SETTINGS @@ -67,7 +70,7 @@ def webdav_check() -> None: _logger.debug("WebDAV prefix directory found.") -def webdav_ensure_path(remote_path: str) -> None: +def webdav_ensure_path(remote_path: str) -> bool: remote_path = f"{SETTINGS.webdav_prefix}/{remote_path}" if _WEBDAV_CLIENT.check(remote_path): @@ -75,7 +78,7 @@ def webdav_ensure_path(remote_path: str) -> None: "WebDAV path %s found.", repr(remote_path), ) - return + return True _logger.info( "WebDAV path %s not found, creating ...", @@ -83,6 +86,45 @@ def webdav_ensure_path(remote_path: str) -> None: ) _WEBDAV_CLIENT.mkdir(remote_path) + return False + + +def get_skel_path(skel_file: str) -> Path: + skel_path = path.dirname(Path(OVD_INIT).absolute()) + return Path(skel_path).joinpath("skel", skel_file) + + +def webdav_upload_skel(remote_path: str, *skel_files: str) -> None: + remote_path = f"{SETTINGS.webdav_prefix}/{remote_path}" + + for skel_file in skel_files: + _logger.debug( + "Creating WebDAV file %s ...", + repr(skel_file), + ) + + _WEBDAV_CLIENT.upload_file( + f"{remote_path}/{skel_file}", + get_skel_path(skel_file), + ) + + +def webdav_ensure_files(remote_path: str, *file_names: str) -> None: + missing_files = ( + file_name + for file_name in file_names + if not _WEBDAV_CLIENT.check(path.join( + SETTINGS.webdav_prefix, + remote_path, + file_name, + )) + ) + + webdav_upload_skel( + remote_path, + *missing_files, + ) + @lru_cache(maxsize=SETTINGS.cache_size) def webdav_resource(remote_path: Any) -> WebDAVResource: diff --git a/api/ovdashboard_api/routers/v1/file.py b/api/ovdashboard_api/routers/v1/file.py index d64b2f7..a7837b1 100644 --- a/api/ovdashboard_api/routers/v1/file.py +++ b/api/ovdashboard_api/routers/v1/file.py @@ -15,7 +15,7 @@ from fastapi import APIRouter, Depends from fastapi.responses import StreamingResponse from magic import Magic -from ...dav_common import webdav_ensure_path +from ...dav_common import webdav_ensure_files, webdav_ensure_path from ...dav_file import DavFile from ._common import FileNameLister, PrefixFinder, PrefixUnique @@ -40,7 +40,11 @@ file_unique = PrefixUnique(file_finder) async def start_router() -> None: _logger.debug(f"{router.prefix} router starting.") - webdav_ensure_path(await file_lister.remote_path) + if not webdav_ensure_path(await file_lister.remote_path): + webdav_ensure_files( + await file_lister.remote_path, + "logo.svg", + ) @router.get( diff --git a/api/ovdashboard_api/routers/v1/image.py b/api/ovdashboard_api/routers/v1/image.py index fb10b5c..fc89d24 100644 --- a/api/ovdashboard_api/routers/v1/image.py +++ b/api/ovdashboard_api/routers/v1/image.py @@ -16,7 +16,7 @@ from fastapi.responses import StreamingResponse from PIL import Image from ...config import Config, ImageUIConfig -from ...dav_common import webdav_ensure_path +from ...dav_common import webdav_ensure_files, webdav_ensure_path from ...dav_file import DavFile from ._common import FileNameLister, PrefixFinder, PrefixUnique @@ -40,7 +40,13 @@ image_unique = PrefixUnique(image_finder) async def start_router() -> None: _logger.debug(f"{router.prefix} router starting.") - webdav_ensure_path(await image_lister.remote_path) + if not webdav_ensure_path(await image_lister.remote_path): + webdav_ensure_files( + await image_lister.remote_path, + "img1.jpg", + "img2.jpg", + "img3.jpg", + ) @router.get( diff --git a/api/ovdashboard_api/routers/v1/text.py b/api/ovdashboard_api/routers/v1/text.py index f841ee3..74253e1 100644 --- a/api/ovdashboard_api/routers/v1/text.py +++ b/api/ovdashboard_api/routers/v1/text.py @@ -14,7 +14,7 @@ from typing import Iterator from fastapi import APIRouter, Depends from markdown import markdown -from ...dav_common import webdav_ensure_path +from ...dav_common import webdav_ensure_files, webdav_ensure_path from ...dav_file import DavFile from ._common import FileNameLister, PrefixFinder, PrefixUnique @@ -40,6 +40,13 @@ async def start_router() -> None: webdav_ensure_path(await text_lister.remote_path) + webdav_ensure_files( + await text_lister.remote_path, + "message.txt", + "title.txt", + "ticker.txt", + ) + @router.get( "/list", diff --git a/api/ovdashboard_api/routers/v1/ticker.py b/api/ovdashboard_api/routers/v1/ticker.py index c70cfef..ac90230 100644 --- a/api/ovdashboard_api/routers/v1/ticker.py +++ b/api/ovdashboard_api/routers/v1/ticker.py @@ -13,7 +13,7 @@ from fastapi import APIRouter, Depends from markdown import markdown from ...config import Config, TickerUIConfig -from ...dav_common import webdav_ensure_path +from ...dav_common import webdav_ensure_files, webdav_ensure_path from ...dav_file import DavFile from .text import text_lister, text_unique @@ -28,6 +28,11 @@ async def start_router() -> None: webdav_ensure_path(await text_lister.remote_path) + webdav_ensure_files( + await text_lister.remote_path, + "ticker.txt", + ) + async def get_ticker_lines() -> Iterator[str]: cfg = await Config.get()