diff options
author | rinpatch <rinpatch@sdf.org> | 2019-04-17 12:22:32 +0300 |
---|---|---|
committer | rinpatch <rinpatch@sdf.org> | 2019-04-17 12:22:32 +0300 |
commit | 627e5a0a4992cc19fc65a7e93a09c470c8e2bf33 (patch) | |
tree | 0f38b475e8554863a1cbbd7750c19d4cd1336eb1 /lib/pleroma/plugs/instance_static.ex | |
parent | d6ab701a14f7c9fb4d59953648c425e04725fc62 (diff) | |
parent | 73df3046e014ae16e03f16a9c82921652cefcb54 (diff) | |
download | pleroma-627e5a0a4992cc19fc65a7e93a09c470c8e2bf33.tar.gz |
Merge branch 'develop' into feature/database-compaction
Diffstat (limited to 'lib/pleroma/plugs/instance_static.ex')
-rw-r--r-- | lib/pleroma/plugs/instance_static.ex | 59 |
1 files changed, 59 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..a64f1ea80 --- /dev/null +++ b/lib/pleroma/plugs/instance_static.ex @@ -0,0 +1,59 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +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 robots.txt static emoji packs sounds images instance favicon.png sw.js + sw-pleroma.js) + + 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(%{request_path: "/" <> unquote(only) <> _} = conn, 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 |