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
17 @default_cache_control_header "public, max-age=1209600"
22 headers: %{"cache-control" => @default_cache_control_header},
23 cache_control_for_etags: @default_cache_control_header
25 |> Keyword.put(:from, "__unconfigured_media_plug")
26 |> Keyword.put(:at, "/__unconfigured_media_plug")
29 %{static_plug_opts: static_plug_opts}
32 def call(%{request_path: <<"/", @path, "/", file::binary>>} = conn, opts) do
34 case fetch_query_params(conn) do
35 %{query_params: %{"name" => name}} = conn ->
36 name = String.replace(name, "\"", "\\\"")
39 |> put_resp_header("content-disposition", "filename=\"#{name}\"")
44 |> merge_resp_headers([{"content-security-policy", "sandbox"}])
46 config = Pleroma.Config.get(Pleroma.Upload)
48 with uploader <- Keyword.fetch!(config, :uploader),
49 proxy_remote = Keyword.get(config, :proxy_remote, false),
50 {:ok, get_method} <- uploader.get_file(file) do
51 get_media(conn, get_method, proxy_remote, opts)
55 |> send_resp(:internal_server_error, dgettext("errors", "Failed"))
60 def call(conn, _opts), do: conn
62 defp get_media(conn, {:static_dir, directory}, _, opts) do
64 Map.get(opts, :static_plug_opts)
65 |> Map.put(:at, [@path])
66 |> Map.put(:from, directory)
68 conn = Plug.Static.call(conn, static_opts)
74 |> send_resp(:not_found, dgettext("errors", "Not found"))
79 defp get_media(conn, {:url, url}, true, _) do
81 |> Pleroma.ReverseProxy.call(url, Pleroma.Config.get([Pleroma.Upload, :proxy_opts], []))
84 defp get_media(conn, {:url, url}, _, _) do
86 |> Phoenix.Controller.redirect(external: url)
90 defp get_media(conn, unknown, _, _) do
91 Logger.error("#{__MODULE__}: Unknown get startegy: #{inspect(unknown)}")
94 |> send_resp(:internal_server_error, dgettext("errors", "Internal Error"))