- def render(self, filename="/", hubapp=None):
- if not hubapp:
- hubapp = self.context["hubapp"]
- elif isinstance(hubapp, str):
- hubapp = self.context["hubapp"].app.get_hubapp_by_name(hubapp)
- return hubapp.uri_from(Path(filename))
-
-
-def scramble(secret, value):
- if isinstance(value, str):
- value = value.encode()
- if isinstance(secret, str):
- secret = secret.encode()
- return urlsafe_b64encode(
- pbkdf2_hmac("sha512", value, secret, 221100)
- ).rstrip(b"=").decode("ascii")
+ def render(
+ self, filename: str = "", hubapp: str | TreeFileApp | None = None
+ ):
+ """
+ If filename starts with '/', interpret the path as relative to hubapp.base_dir,
+ otherwise assume the path is relative to the current file.
+ """
+ static_file = self.context["static_file"]
+ h = static_file.hubapp
+ if isinstance(hubapp, str):
+ h = h.app.hubapps[hubapp]
+ elif isinstance(hubapp, TreeFileApp):
+ h = hubapp
+ del hubapp
+ if filename.startswith("/") or h != static_file.hubapp:
+ path = h.base_dir / filename.lstrip("/")
+ else:
+ path = static_file.path.parent / filename
+ return h.uri(path)