2019-05-22 12:12:11 +00:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/plugs/uploaded_media.ex
|
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
defmodule Mobilizon.Web.Plugs.UploadedMedia do
|
2019-05-22 12:12:11 +00:00
|
|
|
@moduledoc """
|
|
|
|
Serves uploaded media files
|
|
|
|
"""
|
|
|
|
|
2019-09-07 22:05:54 +00:00
|
|
|
@behaviour Plug
|
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
import Plug.Conn
|
2019-09-07 22:05:54 +00:00
|
|
|
|
|
|
|
alias Mobilizon.Config
|
|
|
|
|
2020-01-28 18:18:33 +00:00
|
|
|
alias Mobilizon.Web.{ReverseProxy, Upload}
|
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
# no slashes
|
|
|
|
@path "media"
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec init(any()) :: map()
|
2019-05-22 12:12:11 +00:00
|
|
|
def init(_opts) do
|
|
|
|
static_plug_opts =
|
|
|
|
[]
|
|
|
|
|> Keyword.put(:from, "__unconfigured_media_plug")
|
|
|
|
|> Keyword.put(:at, "/__unconfigured_media_plug")
|
|
|
|
|> Plug.Static.init()
|
|
|
|
|
|
|
|
%{static_plug_opts: static_plug_opts}
|
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec call(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
2019-05-22 12:12:11 +00:00
|
|
|
def call(%{request_path: <<"/", @path, "/", file::binary>>} = conn, opts) do
|
|
|
|
conn =
|
|
|
|
case fetch_query_params(conn) do
|
|
|
|
%{query_params: %{"name" => name}} = conn ->
|
|
|
|
name = String.replace(name, "\"", "\\\"")
|
|
|
|
|
2020-01-22 23:55:07 +00:00
|
|
|
put_resp_header(conn, "content-disposition", "filename=\"#{name}\"")
|
2019-05-22 12:12:11 +00:00
|
|
|
|
|
|
|
conn ->
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
|
2020-01-28 18:18:33 +00:00
|
|
|
config = Config.get([Upload])
|
2019-05-22 12:12:11 +00:00
|
|
|
|
2021-09-10 09:27:59 +00:00
|
|
|
uploader = Keyword.fetch!(config, :uploader)
|
|
|
|
proxy_remote = Keyword.get(config, :proxy_remote, false)
|
|
|
|
|
|
|
|
case uploader.get_file(file) do
|
|
|
|
{:ok, get_method} ->
|
|
|
|
get_media(conn, get_method, proxy_remote, opts)
|
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
_ ->
|
|
|
|
conn
|
|
|
|
|> send_resp(500, "Failed")
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(conn, _opts), do: conn
|
|
|
|
|
2021-09-10 09:27:59 +00:00
|
|
|
@spec get_media(
|
|
|
|
Plug.Conn.t(),
|
|
|
|
{:static_dir, String.t()} | {:url, String.t()} | any(),
|
|
|
|
boolean,
|
|
|
|
any()
|
|
|
|
) :: Plug.Conn.t()
|
2019-05-22 12:12:11 +00:00
|
|
|
defp get_media(conn, {:static_dir, directory}, _, opts) do
|
|
|
|
static_opts =
|
2019-09-22 11:41:24 +00:00
|
|
|
opts
|
|
|
|
|> Map.get(:static_plug_opts)
|
2019-05-22 12:12:11 +00:00
|
|
|
|> Map.put(:at, [@path])
|
|
|
|
|> Map.put(:from, directory)
|
|
|
|
|
|
|
|
conn = Plug.Static.call(conn, static_opts)
|
|
|
|
|
|
|
|
if conn.halted do
|
|
|
|
conn
|
|
|
|
else
|
|
|
|
conn
|
2020-11-26 10:41:13 +00:00
|
|
|
|> delete_resp_header("content-disposition")
|
|
|
|
|> put_status(404)
|
|
|
|
|> Phoenix.Controller.put_view(Mobilizon.Web.ErrorView)
|
|
|
|
|> Phoenix.Controller.render("404.html")
|
2019-05-22 12:12:11 +00:00
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_media(conn, {:url, url}, true, _) do
|
2020-01-28 18:18:33 +00:00
|
|
|
ReverseProxy.call(conn, url, Config.get([Mobilizon.Upload, :proxy_opts], []))
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp get_media(conn, {:url, url}, _, _) do
|
|
|
|
conn
|
|
|
|
|> Phoenix.Controller.redirect(external: url)
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_media(conn, unknown, _, _) do
|
|
|
|
Logger.error("#{__MODULE__}: Unknown get startegy: #{inspect(unknown)}")
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> send_resp(500, "Internal Error")
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|