This commit is contained in:
retoor 2025-05-09 02:08:43 +02:00
parent 165dda3210
commit ac570d036c
2 changed files with 37 additions and 7 deletions
src/snek

View File

@ -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)

View File

@ -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():