1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.FrontendStatic do
6 require Pleroma.Constants
9 This is a shim to call `Plug.Static` but with runtime `from` configuration`. It dispatches to the different frontends.
13 def file_path(path, frontend_type \\ :primary) do
14 if configuration = Pleroma.Config.get([:frontends, frontend_type]) do
15 instance_static_path = Pleroma.Config.get([:instance, :static_dir], "instance/static")
20 configuration["name"],
31 |> Keyword.put(:from, "__unconfigured_frontend_static_plug")
33 |> Map.put(:frontend_type, opts[:frontend_type])
36 def call(conn, opts) do
37 with false <- invalid_path?(conn.path_info),
38 frontend_type <- Map.get(opts, :frontend_type, :primary),
39 path when not is_nil(path) <- file_path("", frontend_type) do
40 call_static(conn, opts, path)
47 defp invalid_path?(list) do
48 invalid_path?(list, :binary.compile_pattern(["/", "\\", ":", "\0"]))
51 defp invalid_path?([h | _], _match) when h in [".", "..", ""], do: true
52 defp invalid_path?([h | t], match), do: String.contains?(h, match) or invalid_path?(t)
53 defp invalid_path?([], _match), do: false
55 defp call_static(conn, opts, from) do
56 opts = Map.put(opts, :from, from)
57 Plug.Static.call(conn, opts)