aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/plugs/instance_static.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pleroma/plugs/instance_static.ex')
-rw-r--r--lib/pleroma/plugs/instance_static.ex75
1 files changed, 47 insertions, 28 deletions
diff --git a/lib/pleroma/plugs/instance_static.ex b/lib/pleroma/plugs/instance_static.ex
index 927fa2663..41d2bc923 100644
--- a/lib/pleroma/plugs/instance_static.ex
+++ b/lib/pleroma/plugs/instance_static.ex
@@ -7,22 +7,28 @@ defmodule Pleroma.Plugs.InstanceStatic do
This is a shim to call `Plug.Static` but with runtime `from` configuration.
Mountpoints are defined directly in the module to avoid calling the configuration for every request including non-static ones.
+
+ Files in FE bundles can override files in priv/static, and files in
+ instance/static can override files in FE bundles:
+ instance/static > FE bundles > priv/static
"""
@behaviour Plug
- def file_path(path) do
- instance_path =
- Path.join(Pleroma.Config.get([:instance, :static_dir], "instance/static/"), path)
-
- if File.exists?(instance_path) do
- instance_path
- else
- Path.join(Application.app_dir(:pleroma, "priv/static/"), path)
- end
- end
+ # list of paths to be looked up in intance/static
+ @instance_overridable_paths ~w(robots.txt emoji sounds images instance favicon.png)
- @only ~w(index.html robots.txt static emoji packs sounds images instance favicon.png sw.js
- sw-pleroma.js)
+ # both pleroma/{STATIC_PATH} and pleroma/admin/{STATIC_PATH} can be requested
+ @fe_prefixed_paths ~w(pleroma/admin pleroma)
+ @fe_paths [
+ # mastodon
+ "packs",
+ "sw.js",
+ # primary frontend
+ "static",
+ "index.html",
+ "sw-pleroma.js",
+ "static-fe.css"
+ ]
def init(opts) do
opts
@@ -31,29 +37,42 @@ defmodule Pleroma.Plugs.InstanceStatic do
|> Plug.Static.init()
end
- for only <- @only do
- at = Plug.Router.Utils.split("/")
+ for path <- @fe_prefixed_paths do
+ def call(%{request_path: "/" <> unquote(path) <> _} = conn, opts) do
+ fe_path = get_fe_path(conn)
+ opts = %{opts | at: Plug.Router.Utils.split(unquote(path)), from: fe_path}
- def call(%{request_path: "/" <> unquote(only) <> _} = conn, opts) do
- call_static(
- conn,
- opts,
- unquote(at),
- Pleroma.Config.get([:instance, :static_dir], "instance/static")
- )
+ Plug.Static.call(conn, opts)
end
end
- def call(conn, _) do
- conn
+ for path <- @fe_paths do
+ def call(%{request_path: "/" <> unquote(path) <> _} = conn, opts) do
+ fe_path = get_fe_path(conn)
+ opts = %{opts | at: [], from: fe_path}
+
+ with ^conn <- call_instance_static(conn, opts) do
+ Plug.Static.call(conn, opts)
+ end
+ end
end
- defp call_static(conn, opts, at, from) do
- opts =
- opts
- |> Map.put(:from, from)
- |> Map.put(:at, at)
+ for path <- @instance_overridable_paths do
+ def call(%{request_path: "/" <> unquote(path) <> _} = conn, opts) do
+ call_instance_static(conn, opts)
+ end
+ end
+
+ def call(conn, _), do: conn
+ defp call_instance_static(conn, opts) do
+ instance_static_path = Pleroma.Config.get([:instance, :static_dir], "instance/static")
+ opts = %{opts | at: [], from: instance_static_path}
Plug.Static.call(conn, opts)
end
+
+ defp get_fe_path(%{private: %{frontend: %{config: conf}}}) do
+ instance_static_path = Pleroma.Config.get([:instance, :static_dir], "instance/static")
+ Path.join([instance_static_path, "frontends", conf["name"], conf["ref"]])
+ end
end