diff options
author | href <href@random.sh> | 2018-12-17 22:50:59 +0100 |
---|---|---|
committer | href <href@random.sh> | 2018-12-17 22:50:59 +0100 |
commit | b1860fe85af1d32de937f466ba65d03614952e31 (patch) | |
tree | 9b1239247d38a2aaa60627fc9bd4db44ed67d136 /lib/pleroma/plugs/instance_static.ex | |
parent | 3879500c87829a5cf1377ca7ccdb7bf92c75367f (diff) | |
download | pleroma-b1860fe85af1d32de937f466ba65d03614952e31.tar.gz |
Instance/Static runtime plug
This allows to set-up an arbitrary directory which overrides most of the
static files: index.html static/ emoji/ packs/ sounds/ images/ instance/
favicon.png.
If the files are not present in the directory, the bundled ones in
priv/static will be used.
Diffstat (limited to 'lib/pleroma/plugs/instance_static.ex')
-rw-r--r-- | lib/pleroma/plugs/instance_static.ex | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/pleroma/plugs/instance_static.ex b/lib/pleroma/plugs/instance_static.ex new file mode 100644 index 000000000..46ee77e11 --- /dev/null +++ b/lib/pleroma/plugs/instance_static.ex @@ -0,0 +1,54 @@ +defmodule Pleroma.Plugs.InstanceStatic do + @moduledoc """ + 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. + """ + @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 + + @only ~w(index.html static emoji packs sounds images instance favicon.png) + + def init(opts) do + opts + |> Keyword.put(:from, "__unconfigured_instance_static_plug") + |> Keyword.put(:at, "/__unconfigured_instance_static_plug") + |> Plug.Static.init() + end + + for only <- @only do + at = Plug.Router.Utils.split("/") + + def call(conn = %{request_path: "/" <> unquote(only) <> _}, opts) do + call_static( + conn, + opts, + unquote(at), + Pleroma.Config.get([:instance, :static_dir], "instance/static") + ) + end + end + + def call(conn, _) do + conn + end + + defp call_static(conn, opts, at, from) do + opts = + opts + |> Map.put(:from, from) + |> Map.put(:at, at) + + Plug.Static.call(conn, opts) + end +end |