logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

frontend_static.ex (1943B)


  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. defmodule Pleroma.Web.Plugs.FrontendStatic do
  5. require Pleroma.Constants
  6. @moduledoc """
  7. This is a shim to call `Plug.Static` but with runtime `from` configuration`. It dispatches to the different frontends.
  8. """
  9. @behaviour Plug
  10. def file_path(path, frontend_type \\ :primary) do
  11. if configuration = Pleroma.Config.get([:frontends, frontend_type]) do
  12. instance_static_path = Pleroma.Config.get([:instance, :static_dir], "instance/static")
  13. Path.join([
  14. instance_static_path,
  15. "frontends",
  16. configuration["name"],
  17. configuration["ref"],
  18. path
  19. ])
  20. else
  21. nil
  22. end
  23. end
  24. def init(opts) do
  25. opts
  26. |> Keyword.put(:from, "__unconfigured_frontend_static_plug")
  27. |> Plug.Static.init()
  28. |> Map.put(:frontend_type, opts[:frontend_type])
  29. end
  30. def call(conn, opts) do
  31. with false <- api_route?(conn.path_info),
  32. false <- invalid_path?(conn.path_info),
  33. frontend_type <- Map.get(opts, :frontend_type, :primary),
  34. path when not is_nil(path) <- file_path("", frontend_type) do
  35. call_static(conn, opts, path)
  36. else
  37. _ ->
  38. conn
  39. end
  40. end
  41. defp invalid_path?(list) do
  42. invalid_path?(list, :binary.compile_pattern(["/", "\\", ":", "\0"]))
  43. end
  44. defp invalid_path?([h | _], _match) when h in [".", "..", ""], do: true
  45. defp invalid_path?([h | t], match), do: String.contains?(h, match) or invalid_path?(t)
  46. defp invalid_path?([], _match), do: false
  47. defp api_route?([]), do: false
  48. defp api_route?([h | t]) do
  49. api_routes = Pleroma.Web.Router.get_api_routes()
  50. if h in api_routes, do: true, else: api_route?(t)
  51. end
  52. defp call_static(conn, opts, from) do
  53. opts = Map.put(opts, :from, from)
  54. Plug.Static.call(conn, opts)
  55. end
  56. end