logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

frontend_static.ex (1336B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.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. frontend_type = Map.get(opts, :frontend_type, :primary)
  32. path = file_path("", frontend_type)
  33. if path do
  34. conn
  35. |> call_static(opts, path)
  36. else
  37. conn
  38. end
  39. end
  40. defp call_static(conn, opts, from) do
  41. opts =
  42. opts
  43. |> Map.put(:from, from)
  44. Plug.Static.call(conn, opts)
  45. end
  46. end