2020-07-28 15:35:16 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2020-07-28 15:35:16 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-02 07:29:36 +00:00
|
|
|
defmodule Pleroma.Web.Plugs.FrontendStatic do
|
2020-07-28 15:35:16 +00:00
|
|
|
require Pleroma.Constants
|
|
|
|
|
|
|
|
@moduledoc """
|
|
|
|
This is a shim to call `Plug.Static` but with runtime `from` configuration`. It dispatches to the different frontends.
|
|
|
|
"""
|
|
|
|
@behaviour Plug
|
|
|
|
|
|
|
|
def file_path(path, frontend_type \\ :primary) do
|
|
|
|
if configuration = Pleroma.Config.get([:frontends, frontend_type]) do
|
|
|
|
instance_static_path = Pleroma.Config.get([:instance, :static_dir], "instance/static")
|
|
|
|
|
|
|
|
Path.join([
|
|
|
|
instance_static_path,
|
|
|
|
"frontends",
|
|
|
|
configuration["name"],
|
|
|
|
configuration["ref"],
|
|
|
|
path
|
|
|
|
])
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def init(opts) do
|
|
|
|
opts
|
|
|
|
|> Keyword.put(:from, "__unconfigured_frontend_static_plug")
|
|
|
|
|> Plug.Static.init()
|
2020-07-29 11:02:48 +00:00
|
|
|
|> Map.put(:frontend_type, opts[:frontend_type])
|
2020-07-28 15:35:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(conn, opts) do
|
2021-02-24 21:23:45 +00:00
|
|
|
with false <- api_route?(conn.path_info),
|
|
|
|
false <- invalid_path?(conn.path_info),
|
2020-10-27 19:58:55 +00:00
|
|
|
frontend_type <- Map.get(opts, :frontend_type, :primary),
|
|
|
|
path when not is_nil(path) <- file_path("", frontend_type) do
|
|
|
|
call_static(conn, opts, path)
|
2020-07-28 15:35:16 +00:00
|
|
|
else
|
2020-10-27 19:58:55 +00:00
|
|
|
_ ->
|
|
|
|
conn
|
2020-07-28 15:35:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-27 19:58:55 +00:00
|
|
|
defp invalid_path?(list) do
|
|
|
|
invalid_path?(list, :binary.compile_pattern(["/", "\\", ":", "\0"]))
|
|
|
|
end
|
2020-07-28 15:35:16 +00:00
|
|
|
|
2020-10-27 19:58:55 +00:00
|
|
|
defp invalid_path?([h | _], _match) when h in [".", "..", ""], do: true
|
|
|
|
defp invalid_path?([h | t], match), do: String.contains?(h, match) or invalid_path?(t)
|
|
|
|
defp invalid_path?([], _match), do: false
|
|
|
|
|
2021-02-25 16:07:29 +00:00
|
|
|
defp api_route?([]), do: false
|
2021-02-24 21:23:45 +00:00
|
|
|
|
2021-05-21 22:37:34 +00:00
|
|
|
defp api_route?([h | t]) do
|
|
|
|
api_routes = Pleroma.Web.Router.get_api_routes()
|
|
|
|
if h in api_routes, do: true, else: api_route?(t)
|
|
|
|
end
|
|
|
|
|
2020-10-27 19:58:55 +00:00
|
|
|
defp call_static(conn, opts, from) do
|
|
|
|
opts = Map.put(opts, :from, from)
|
2020-07-28 15:35:16 +00:00
|
|
|
Plug.Static.call(conn, opts)
|
|
|
|
end
|
|
|
|
end
|