logo

pleroma

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

instance_static.ex (1473B)


  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.InstanceStatic do
  5. require Pleroma.Constants
  6. @moduledoc """
  7. This is a shim to call `Plug.Static` but with runtime `from` configuration.
  8. Mountpoints are defined directly in the module to avoid calling the configuration for every request including non-static ones.
  9. """
  10. @behaviour Plug
  11. def file_path(path) do
  12. instance_path =
  13. Path.join(Pleroma.Config.get([:instance, :static_dir], "instance/static/"), path)
  14. frontend_path = Pleroma.Web.Plugs.FrontendStatic.file_path(path, :primary)
  15. (File.exists?(instance_path) && instance_path) ||
  16. (frontend_path && File.exists?(frontend_path) && frontend_path) ||
  17. Path.join(Application.app_dir(:pleroma, "priv/static/"), path)
  18. end
  19. def init(opts) do
  20. opts
  21. |> Keyword.put(:from, "__unconfigured_instance_static_plug")
  22. |> Plug.Static.init()
  23. end
  24. for only <- Pleroma.Constants.static_only_files() do
  25. def call(%{request_path: "/" <> unquote(only) <> _} = conn, opts) do
  26. call_static(
  27. conn,
  28. opts,
  29. Pleroma.Config.get([:instance, :static_dir], "instance/static")
  30. )
  31. end
  32. end
  33. def call(conn, _) do
  34. conn
  35. end
  36. defp call_static(conn, opts, from) do
  37. opts =
  38. opts
  39. |> Map.put(:from, from)
  40. Plug.Static.call(conn, opts)
  41. end
  42. end