From ac570d036c26b6c7ff5abac169fdf622c10827ad Mon Sep 17 00:00:00 2001 From: retoor Date: Fri, 9 May 2025 02:08:43 +0200 Subject: [PATCH] Update. --- src/snek/app.py | 36 +++++++++++++++++++++++++++++------- src/snek/service/user.py | 8 ++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/snek/app.py b/src/snek/app.py index ece5b7b..32d83e9 100644 --- a/src/snek/app.py +++ b/src/snek/app.py @@ -75,6 +75,7 @@ class Application(BaseApplication): web.normalize_path_middleware(merge_slashes=True), ] self.template_path = pathlib.Path(__file__).parent.joinpath("templates") + self.static_path = pathlib.Path(__file__).parent.joinpath("static") super().__init__( middlewares=middlewares, template_path=self.template_path, *args, **kwargs ) @@ -136,12 +137,7 @@ class Application(BaseApplication): def setup_router(self): self.router.add_get("/", IndexView) - self.router.add_static( - "/", - pathlib.Path(__file__).parent.joinpath("static"), - name="static", - show_index=True, - ) + self.router.add_get("/static/{file_path:.*}", self.static_handler) self.router.add_view("/profiler.html", profiler_handler) self.router.add_view("/about.html", AboutHTMLView) self.router.add_view("/about.md", AboutMDView) @@ -257,11 +253,37 @@ class Application(BaseApplication): return rendered + + async def static_handler(request): + file_name = request.match_info.get('filename', '') + + paths = [] + + + uid = self.request.session.get("uid") + if uid: + user_static_path = await self.services.user.get_static_path(uid) + if user_static_path: + paths.append(user_template_path) + + for admin_uid in self.services.user.get_admin_uids(): + user_static_path = await self.services.user.get_static_path(admin_uid) + if user_static_path: + paths.append(user_static_path) + + paths.append(self.static_path) + + for path in paths: + if pathlib.Path(path).joinpath(file_name).exists(): + return web.FileResponse(pathlib.Path(path).joinpath(file_name)) + return web.HTTPNotFound() + async def get_user_template_loader(self, uid=None): template_paths = [] for admin_uid in self.services.user.get_admin_uids(): user_template_path = await self.services.user.get_template_path(admin_uid) - template_paths.append(user_template_path) + if user_template_path: + template_paths.append(user_template_path) if uid: user_template_path = await self.services.user.get_template_path(uid) diff --git a/src/snek/service/user.py b/src/snek/service/user.py index c0734dc..fb66ddb 100644 --- a/src/snek/service/user.py +++ b/src/snek/service/user.py @@ -42,6 +42,14 @@ class UserService(BaseService): def get_admin_uids(self): return self.mapper.get_admin_uids() + async def get_static_path(self, user_uid): + path = pathlib.Path(f"./drive/{user_uid}/snek/static") + if not path.exists(): + return None + return path + + + async def get_template_path(self, user_uid): path = pathlib.Path(f"./drive/{user_uid}/snek/templates") if not path.exists():