1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.FrontendStatic do
6 require Pleroma.Constants
8 @frontend_cookie_name "preferred_frontend"
11 This is a shim to call `Plug.Static` but with runtime `from` configuration`. It dispatches to the different frontends.
15 defp instance_static_path do
16 Pleroma.Config.get([:instance, :static_dir], "instance/static")
19 def file_path(path, frontend_type \\ :primary)
21 def file_path(path, frontend_type) when is_atom(frontend_type) do
22 if configuration = Pleroma.Config.get([:frontends, frontend_type]) do
24 instance_static_path(),
26 configuration["name"],
35 def file_path(path, frontend_type) when is_binary(frontend_type) do
37 instance_static_path(),
46 |> Keyword.put(:from, "__unconfigured_frontend_static_plug")
48 |> Map.put(:frontend_type, opts[:frontend_type])
49 |> Map.put(:if, Keyword.get(opts, :if, true))
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)
66 def preferred_frontend(conn) do
67 %{req_cookies: cookies} =
69 |> Plug.Conn.fetch_cookies()
71 Map.get(cookies, @frontend_cookie_name)
74 # Only override primary frontend
75 def preferred_or_fallback(conn, :primary) do
76 case preferred_frontend(conn) do
85 def preferred_or_fallback(_conn, fallback), do: fallback
87 defp enabled?(if_opt) when is_function(if_opt), do: if_opt.()
88 defp enabled?(true), do: true
89 defp enabled?(_), do: false
91 defp invalid_path?(list) do
92 invalid_path?(list, :binary.compile_pattern(["/", "\\", ":", "\0"]))
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
99 defp api_route?([]), do: false
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)
106 defp call_static(conn, opts, from) do
107 opts = Map.put(opts, :from, from)
108 Plug.Static.call(conn, opts)