From b1860fe85af1d32de937f466ba65d03614952e31 Mon Sep 17 00:00:00 2001 From: href Date: Mon, 17 Dec 2018 22:50:59 +0100 Subject: 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. --- lib/pleroma/plugs/instance_static.ex | 54 +++++++++++++++++++++++++++ lib/pleroma/web/endpoint.ex | 4 ++ lib/pleroma/web/ostatus/ostatus_controller.ex | 2 +- lib/pleroma/web/router.ex | 2 +- 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 lib/pleroma/plugs/instance_static.ex (limited to 'lib') 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 diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex index e52667c72..d79f61b2e 100644 --- a/lib/pleroma/web/endpoint.ex +++ b/lib/pleroma/web/endpoint.ex @@ -12,6 +12,10 @@ defmodule Pleroma.Web.Endpoint do plug(Pleroma.Plugs.UploadedMedia) + # InstanceStatic needs to be before Plug.Static to be able to override shipped-static files + # If you're adding new paths to `only:` you'll need to configure them in InstanceStatic as well + plug(Pleroma.Plugs.InstanceStatic, at: "/") + plug( Plug.Static, at: "/", diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index 9dfcf0f95..6005eadb2 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -136,7 +136,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do "html" -> conn |> put_resp_content_type("text/html") - |> send_file(200, Application.app_dir(:pleroma, "priv/static/index.html")) + |> send_file(200, Pleroma.Plugs.InstanceStatic.file_path("index.html")) _ -> represent_activity(conn, format, activity, user) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 60342cfb4..dd1985d6e 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -459,7 +459,7 @@ defmodule Fallback.RedirectController do def redirector(conn, _params) do conn |> put_resp_content_type("text/html") - |> send_file(200, Application.app_dir(:pleroma, "priv/static/index.html")) + |> send_file(200, Pleroma.Plugs.InstanceStatic.file_path("index.html")) end def registration_page(conn, params) do -- cgit v1.2.3