move to 2.5.5
[anni] / lib / pleroma / web / plugs / frontend_static.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Plugs.FrontendStatic do
6   require Pleroma.Constants
7
8   @moduledoc """
9   This is a shim to call `Plug.Static` but with runtime `from` configuration`. It dispatches to the different frontends.
10   """
11   @behaviour Plug
12
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")
16
17       Path.join([
18         instance_static_path,
19         "frontends",
20         configuration["name"],
21         configuration["ref"],
22         path
23       ])
24     else
25       nil
26     end
27   end
28
29   def init(opts) do
30     opts
31     |> Keyword.put(:from, "__unconfigured_frontend_static_plug")
32     |> Plug.Static.init()
33     |> Map.put(:frontend_type, opts[:frontend_type])
34   end
35
36   def call(conn, opts) do
37     with false <- api_route?(conn.path_info),
38          false <- invalid_path?(conn.path_info),
39          frontend_type <- Map.get(opts, :frontend_type, :primary),
40          path when not is_nil(path) <- file_path("", frontend_type) do
41       call_static(conn, opts, path)
42     else
43       _ ->
44         conn
45     end
46   end
47
48   defp invalid_path?(list) do
49     invalid_path?(list, :binary.compile_pattern(["/", "\\", ":", "\0"]))
50   end
51
52   defp invalid_path?([h | _], _match) when h in [".", "..", ""], do: true
53   defp invalid_path?([h | t], match), do: String.contains?(h, match) or invalid_path?(t)
54   defp invalid_path?([], _match), do: false
55
56   defp api_route?([]), do: false
57
58   defp api_route?([h | t]) do
59     api_routes = Pleroma.Web.Router.get_api_routes()
60     if h in api_routes, do: true, else: api_route?(t)
61   end
62
63   defp call_static(conn, opts, from) do
64     opts = Map.put(opts, :from, from)
65     Plug.Static.call(conn, opts)
66   end
67 end