1d68614568fb967b479ee4d41de3c0c5b6c1dd06
[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 with false <- api_route?(conn.path_info),
54 false <- invalid_path?(conn.path_info),
55 true <- enabled?(opts[:if]),
56 fallback_frontend_type <- Map.get(opts, :frontend_type, :primary),
57 frontend_type <- preferred_or_fallback(conn, fallback_frontend_type),
58 path when not is_nil(path) <- file_path("", frontend_type) do
59 call_static(conn, opts, path)
60 else
61 _ ->
62 conn
63 end
64 end
65
66 def preferred_frontend(conn) do
67 %{req_cookies: cookies} =
68 conn
69 |> Plug.Conn.fetch_cookies()
70
71 Map.get(cookies, @frontend_cookie_name)
72 end
73
74 # Only override primary frontend
75 def preferred_or_fallback(conn, :primary) do
76 case preferred_frontend(conn) do
77 nil ->
78 :primary
79
80 frontend ->
81 frontend
82 end
83 end
84
85 def preferred_or_fallback(_conn, fallback), do: fallback
86
87 defp enabled?(if_opt) when is_function(if_opt), do: if_opt.()
88 defp enabled?(true), do: true
89 defp enabled?(_), do: false
90
91 defp invalid_path?(list) do
92 invalid_path?(list, :binary.compile_pattern(["/", "\\", ":", "\0"]))
93 end
94
95 defp invalid_path?([h | _], _match) when h in [".", "..", ""], do: true
96 defp invalid_path?([h | t], match), do: String.contains?(h, match) or invalid_path?(t)
97 defp invalid_path?([], _match), do: false
98
99 defp api_route?([]), do: false
100
101 defp api_route?([h | t]) do
102 api_routes = Pleroma.Web.Router.get_api_routes()
103 if h in api_routes, do: true, else: api_route?(t)
104 end
105
106 defp call_static(conn, opts, from) do
107 opts = Map.put(opts, :from, from)
108 Plug.Static.call(conn, opts)
109 end
110 end