1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Plugs.UploadedMedia do
10 import Pleroma.Web.Gettext
20 |> Keyword.put(:from, "__unconfigured_media_plug")
21 |> Keyword.put(:at, "/__unconfigured_media_plug")
24 %{static_plug_opts: static_plug_opts}
27 def call(%{request_path: <<"/", @path, "/", file::binary>>} = conn, opts) do
29 case fetch_query_params(conn) do
30 %{query_params: %{"name" => name}} = conn ->
31 name = String.replace(name, "\"", "\\\"")
34 |> put_resp_header("content-disposition", "filename=\"#{name}\"")
40 config = Pleroma.Config.get(Pleroma.Upload)
42 with uploader <- Keyword.fetch!(config, :uploader),
43 proxy_remote = Keyword.get(config, :proxy_remote, false),
44 {:ok, get_method} <- uploader.get_file(file) do
45 get_media(conn, get_method, proxy_remote, opts)
49 |> send_resp(:internal_server_error, dgettext("errors", "Failed"))
54 def call(conn, _opts), do: conn
56 defp get_media(conn, {:static_dir, directory}, _, opts) do
58 Map.get(opts, :static_plug_opts)
59 |> Map.put(:at, [@path])
60 |> Map.put(:from, directory)
62 conn = Plug.Static.call(conn, static_opts)
68 |> send_resp(:not_found, dgettext("errors", "Not found"))
73 defp get_media(conn, {:url, url}, true, _) do
75 |> Pleroma.ReverseProxy.call(url, Pleroma.Config.get([Pleroma.Upload, :proxy_opts], []))
78 defp get_media(conn, {:url, url}, _, _) do
80 |> Phoenix.Controller.redirect(external: url)
84 defp get_media(conn, unknown, _, _) do
85 Logger.error("#{__MODULE__}: Unknown get startegy: #{inspect(unknown)}")
88 |> send_resp(:internal_server_error, dgettext("errors", "Internal Error"))