summaryrefslogtreecommitdiff
path: root/lib/pleroma/web/plugs/frontend_static.ex
blob: 03fd510431e882ab2c17bcb3cd01560d6dd687b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.Plugs.FrontendStatic do
  require Pleroma.Constants

  @moduledoc """
  This is a shim to call `Plug.Static` but with runtime `from` configuration`. It dispatches to the different frontends.
  """
  @behaviour Plug

  @api_routes Pleroma.Web.get_api_routes()

  def file_path(path, frontend_type \\ :primary) do
    if configuration = Pleroma.Config.get([:frontends, frontend_type]) do
      instance_static_path = Pleroma.Config.get([:instance, :static_dir], "instance/static")

      Path.join([
        instance_static_path,
        "frontends",
        configuration["name"],
        configuration["ref"],
        path
      ])
    else
      nil
    end
  end

  def init(opts) do
    opts
    |> Keyword.put(:from, "__unconfigured_frontend_static_plug")
    |> Plug.Static.init()
    |> Map.put(:frontend_type, opts[:frontend_type])
  end

  def call(conn, opts) do
    with false <- api_route?(conn.path_info),
         false <- invalid_path?(conn.path_info),
         frontend_type <- Map.get(opts, :frontend_type, :primary),
         path when not is_nil(path) <- file_path("", frontend_type) do
      call_static(conn, opts, path)
    else
      _ ->
        conn
    end
  end

  defp invalid_path?(list) do
    invalid_path?(list, :binary.compile_pattern(["/", "\\", ":", "\0"]))
  end

  defp invalid_path?([h | _], _match) when h in [".", "..", ""], do: true
  defp invalid_path?([h | t], match), do: String.contains?(h, match) or invalid_path?(t)
  defp invalid_path?([], _match), do: false

  defp api_route?(list) when is_list(list) and length(list) > 0 do
    List.first(list) in @api_routes
  end

  defp api_route?(_), do: false

  defp call_static(conn, opts, from) do
    opts = Map.put(opts, :from, from)
    Plug.Static.call(conn, opts)
  end
end