2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 22:44:49 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-12-17 21:50:59 +00:00
|
|
|
defmodule Pleroma.Plugs.InstanceStatic do
|
2020-05-01 19:15:43 +00:00
|
|
|
require Pleroma.Constants
|
|
|
|
|
2018-12-17 21:50:59 +00:00
|
|
|
@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
|
|
|
|
|
|
|
|
def file_path(path) do
|
|
|
|
instance_path =
|
|
|
|
Path.join(Pleroma.Config.get([:instance, :static_dir], "instance/static/"), path)
|
|
|
|
|
|
|
|
if File.exists?(instance_path) do
|
|
|
|
instance_path
|
|
|
|
else
|
|
|
|
Path.join(Application.app_dir(:pleroma, "priv/static/"), path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def init(opts) do
|
|
|
|
opts
|
|
|
|
|> Keyword.put(:from, "__unconfigured_instance_static_plug")
|
|
|
|
|> Keyword.put(:at, "/__unconfigured_instance_static_plug")
|
|
|
|
|> Plug.Static.init()
|
|
|
|
end
|
|
|
|
|
2020-05-01 19:15:43 +00:00
|
|
|
for only <- Pleroma.Constants.static_only_files() do
|
2018-12-17 21:50:59 +00:00
|
|
|
at = Plug.Router.Utils.split("/")
|
|
|
|
|
2019-02-06 19:19:39 +00:00
|
|
|
def call(%{request_path: "/" <> unquote(only) <> _} = conn, opts) do
|
2018-12-17 21:50:59 +00:00
|
|
|
call_static(
|
|
|
|
conn,
|
|
|
|
opts,
|
|
|
|
unquote(at),
|
|
|
|
Pleroma.Config.get([:instance, :static_dir], "instance/static")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(conn, _) do
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
|
|
|
|
defp call_static(conn, opts, at, from) do
|
|
|
|
opts =
|
|
|
|
opts
|
|
|
|
|> Map.put(:from, from)
|
|
|
|
|> Map.put(:at, at)
|
|
|
|
|
|
|
|
Plug.Static.call(conn, opts)
|
|
|
|
end
|
|
|
|
end
|