From f677e27282cddcd9544e33a0f5a7577b93c84bb7 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Wed, 16 Jun 2021 17:02:34 +0200 Subject: [PATCH] Attempt to log more and find correct paths --- pubspace.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/pubspace.py b/pubspace.py index 07175f0..4ba9b53 100644 --- a/pubspace.py +++ b/pubspace.py @@ -1,5 +1,6 @@ import logging from os import environ +from os.path import basename import owncloud from fastapi import FastAPI, Request @@ -43,8 +44,11 @@ nextcloud.login(NEXTCLOUD_USER, NEXTCLOUD_APP_PASSWORD) def create_share(fpath): - if not nextcloud.is_shared(fpath): - info = nextcloud.share_file_with_link(fpath) + fname = basname(fpath) + fpaths = nextcloud.list("/", depth="infinity") + matching = [f.path for f in fpaths if fname in f.path][0] + if not nextcloud.is_shared(matching): + info = nextcloud.share_file_with_link(matching) return info.get_link() return None @@ -53,15 +57,23 @@ def create_share(fpath): async def home(request: Request): try: payload = await request.json() - request.app.state.log.info(f"Received: {payload}") - file = payload["file"] - link = create_share(file) - if link: - request.app.state.log.info(f"Shared {file} on {link}") - else: - request.app.state.log.info(f"{file} already shared or failure!") except Exception: - return {} + request.app.state.log.info("Unable to parse request, bailing out") + return {"detail": "unknown request"} + + request.app.state.log.info(f"Received: {payload}") + + try: + file = payload["rel_path"] + link = create_share(file) + except Exception: + request.app.state.log.info(f"Failed to share {file}" + return {"detail": "failed to create share"} + + if link: + request.app.state.log.info(f"Shared {file} on {link}") + else: + request.app.state.log.info(f"{file} already shared or failure!") @app.get("/healthz")