diff options
Diffstat (limited to 'lib/pleroma/plugs/instance_static.ex')
-rw-r--r-- | lib/pleroma/plugs/instance_static.ex | 72 |
1 files changed, 47 insertions, 25 deletions
diff --git a/lib/pleroma/plugs/instance_static.ex b/lib/pleroma/plugs/instance_static.ex index 7516f75c3..93e6528ea 100644 --- a/lib/pleroma/plugs/instance_static.ex +++ b/lib/pleroma/plugs/instance_static.ex @@ -9,19 +9,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) + # list of paths to be looked up in intance/static + @instance_overridable_paths ~w(robots.txt emoji sounds images instance favicon.png) - if File.exists?(instance_path) do - instance_path - else - Path.join(Application.app_dir(:pleroma, "priv/static/"), path) - end - end + # 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 @@ -30,29 +39,42 @@ defmodule Pleroma.Plugs.InstanceStatic do |> Plug.Static.init() end - for only <- Pleroma.Constants.static_only_files() do - at = Plug.Router.Utils.split("/") + # for only <- Pleroma.Constants.static_only_files() do + 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} + Plug.Static.call(conn, opts) + end + end - def call(%{request_path: "/" <> unquote(only) <> _} = conn, opts) do - call_static( - conn, - opts, - unquote(at), - Pleroma.Config.get([:instance, :static_dir], "instance/static") - ) + 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 - def call(conn, _) do - conn + for path <- @instance_overridable_paths do + def call(%{request_path: "/" <> unquote(path) <> _} = conn, opts) do + call_instance_static(conn, opts) + end end - defp call_static(conn, opts, at, from) do - opts = - opts - |> Map.put(:from, from) - |> Map.put(:at, at) + 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 |