91dfc77c34f28080bee5b09383868c4ab0272d27
[akkoma] / lib / pleroma / web / plugs / frontend_static.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 @frontend_cookie_name "preferred_frontend"
9
10 @moduledoc """
11 This is a shim to call `Plug.Static` but with runtime `from` configuration`. It dispatches to the different frontends.
12 """
13 @behaviour Plug
14
15 defp instance_static_path do
16 Pleroma.Config.get([:instance, :static_dir], "instance/static")
17 end
18
19 def file_path(path, frontend_type \\ :primary)
20
21 def file_path(path, frontend_type) when is_atom(frontend_type) do
22 if configuration = Pleroma.Config.get([:frontends, frontend_type]) do
23 Path.join([
24 instance_static_path(),
25 "frontends",
26 configuration["name"],
27 configuration["ref"],
28 path
29 ])
30 else
31 nil
32 end
33 end
34
35 def file_path(path, frontend_type) when is_binary(frontend_type) do
36 Path.join([
37 instance_static_path(),
38 "frontends",
39 frontend_type,
40 path
41 ])
42 end
43
44 def init(opts) do
45 opts
46 |> Keyword.put(:from, "__unconfigured_frontend_static_plug")
47 |> Plug.Static.init()
48 |> Map.put(:frontend_type, opts[:frontend_type])
49 |> Map.put(:if, Keyword.get(opts, :if, true))
50 end
51
52 def call(conn, opts) do
53 IO.inspect("OPTS: #{inspect(opts)}")
54 with false <- api_route?(conn.path_info),
55 false <- invalid_path?(conn.path_info),
56 true <- enabled?(opts[:if]),
57 fallback_frontend_type <- Map.get(opts, :frontend_type, :primary),
58 frontend_type <- preferred_or_fallback(conn, fallback_frontend_type),
59 path when not is_nil(path) <- file_path("", frontend_type) do
60 call_static(conn, opts, path)
61 else
62 _ ->
63 conn
64 end
65 end
66
67 def preferred_frontend(conn) do
68 %{req_cookies: cookies} =
69 conn
70 |> Plug.Conn.fetch_cookies()
71
72 Map.get(cookies, @frontend_cookie_name)
73 end
74
75 # Only override primary frontend
76 def preferred_or_fallback(conn, :primary) do
77 case preferred_frontend(conn) do
78 nil ->
79 :primary
80
81 frontend ->
82 frontend
83 end
84 end
85
86 def preferred_or_fallback(conn, fallback), do: fallback
87
88 defp enabled?(if_opt) when is_function(if_opt), do: if_opt.()
89 defp enabled?(true), do: true
90 defp enabled?(_), do: false
91
92 defp invalid_path?(list) do
93 invalid_path?(list, :binary.compile_pattern(["/", "\\", ":", "\0"]))
94 end
95
96 defp invalid_path?([h | _], _match) when h in [".", "..", ""], do: true
97 defp invalid_path?([h | t], match), do: String.contains?(h, match) or invalid_path?(t)
98 defp invalid_path?([], _match), do: false
99
100 defp api_route?([]), do: false
101
102 defp api_route?([h | t]) do
103 api_routes = Pleroma.Web.Router.get_api_routes()
104 if h in api_routes, do: true, else: api_route?(t)
105 end
106
107 defp call_static(conn, opts, from) do
108 opts = Map.put(opts, :from, from)
109 IO.inspect(opts, label: "opts")
110 Plug.Static.call(conn, opts)
111 end
112 end