2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-24 01:03:10 -06:00
|
|
|
defmodule Pleroma.Web.Plugs.InstanceStatic do
|
2024-03-06 16:00:25 -07:00
|
|
|
import Plug.Conn
|
|
|
|
|
2020-05-01 13:15:43 -06:00
|
|
|
require Pleroma.Constants
|
|
|
|
|
2024-03-06 16:00:25 -07:00
|
|
|
alias Pleroma.Web.Plugs.Utils
|
|
|
|
|
2018-12-17 14:50:59 -07: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
|
|
|
|
|
2023-03-12 17:59:10 -06:00
|
|
|
def file_path(path, frontend_type \\ :primary) do
|
2018-12-17 14:50:59 -07:00
|
|
|
instance_path =
|
|
|
|
Path.join(Pleroma.Config.get([:instance, :static_dir], "instance/static/"), path)
|
|
|
|
|
2023-03-12 17:59:10 -06:00
|
|
|
frontend_path = Pleroma.Web.Plugs.FrontendStatic.file_path(path, frontend_type)
|
2020-07-28 09:35:16 -06:00
|
|
|
|
|
|
|
(File.exists?(instance_path) && instance_path) ||
|
|
|
|
(frontend_path && File.exists?(frontend_path) && frontend_path) ||
|
2018-12-17 14:50:59 -07:00
|
|
|
Path.join(Application.app_dir(:pleroma, "priv/static/"), path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def init(opts) do
|
|
|
|
opts
|
|
|
|
|> Keyword.put(:from, "__unconfigured_instance_static_plug")
|
|
|
|
|> Plug.Static.init()
|
|
|
|
end
|
|
|
|
|
2020-05-01 13:15:43 -06:00
|
|
|
for only <- Pleroma.Constants.static_only_files() do
|
2019-02-06 12:19:39 -07:00
|
|
|
def call(%{request_path: "/" <> unquote(only) <> _} = conn, opts) do
|
2018-12-17 14:50:59 -07:00
|
|
|
call_static(
|
|
|
|
conn,
|
|
|
|
opts,
|
|
|
|
Pleroma.Config.get([:instance, :static_dir], "instance/static")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(conn, _) do
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
|
2024-03-06 16:00:25 -07:00
|
|
|
defp set_static_content_type(conn, "/emoji/" <> _ = request_path) do
|
|
|
|
real_mime = MIME.from_path(request_path)
|
|
|
|
safe_mime = Utils.get_safe_mime_type(%{allowed_mime_types: ["image"]}, real_mime)
|
|
|
|
|
|
|
|
put_resp_header(conn, "content-type", safe_mime)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp set_static_content_type(conn, request_path) do
|
|
|
|
put_resp_header(conn, "content-type", MIME.from_path(request_path))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp call_static(%{request_path: request_path} = conn, opts, from) do
|
2018-12-17 14:50:59 -07:00
|
|
|
opts =
|
|
|
|
opts
|
|
|
|
|> Map.put(:from, from)
|
2024-03-06 16:00:25 -07:00
|
|
|
|> Map.put(:set_content_type, false)
|
2018-12-17 14:50:59 -07:00
|
|
|
|
2024-03-06 16:00:25 -07:00
|
|
|
conn
|
|
|
|
|> set_static_content_type(request_path)
|
|
|
|
|> Pleroma.Web.Plugs.StaticNoCT.call(opts)
|
2018-12-17 14:50:59 -07:00
|
|
|
end
|
|
|
|
end
|