summaryrefslogtreecommitdiff
path: root/lib/pleroma/plugs/instance_static.ex
blob: f7c9df5f9d209a18c28722268677999857f01638 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Plugs.InstanceStatic do
  @moduledoc """
  This is a shim to call `Plug.Static` but with runtime `from` configuration.

  Mountpoints are defined directly in the module to avoid calling the configuration for every request including non-static ones.
  """
  @behaviour Plug

  alias Pleroma.Config

  def file_path(path) do
    instance_path = Path.join(Config.get([:instance, :static_dir], "instance/static/"), path)

    frontends_path =
      Config.get([:instance, :frontends_dir], "instance/frontends/")
      |> Path.join("pleroma-fe")
      |> Path.join(path)

    cond do
      File.exists?(instance_path) -> instance_path
      File.exists?(frontends_path) -> frontends_path
      true -> Path.join(Application.app_dir(:pleroma, "priv/static/"), path)
    end
  end

  @only ~w(index.html robots.txt static emoji packs sounds images instance favicon.png sw.js
  sw-pleroma.js fontello)

  def init(opts) do
    opts
    |> Keyword.put(:from, "__unconfigured_instance_static_plug")
    |> Keyword.put(:at, "/__unconfigured_instance_static_plug")
    |> Plug.Static.init()
  end

  for only <- @only do
    at = Plug.Router.Utils.split("/")

    def call(%{request_path: "/" <> unquote(only) <> _} = conn, opts) do
      call_static(
        conn,
        opts,
        unquote(at)
      )
    end
  end

  def call(conn, _) do
    conn
  end

  defp call_static(conn, opts, at) do
    static_dir = Config.get([:instance, :static_dir], "instance/static/")

    frontend_dir =
      Config.get([:instance, :frontends_dir], "instance/frontends/")
      |> Path.join("pleroma-fe")

    from =
      if File.exists?(Path.join(static_dir, conn.request_path)) do
        static_dir
      else
        frontend_dir
      end

    opts =
      opts
      |> Map.put(:from, from)
      |> Map.put(:at, at)

    Plug.Static.call(conn, opts)
  end
end