Remove vapidPublicKey from Nodeinfo
[akkoma] / lib / pleroma / plugs / uploaded_media.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Plugs.UploadedMedia do
6 @moduledoc """
7 """
8
9 import Plug.Conn
10 import Pleroma.Web.Gettext
11 require Logger
12
13 @behaviour Plug
14 # no slashes
15 @path "media"
16
17 @default_cache_control_header "public, max-age=1209600"
18
19 def init(_opts) do
20 static_plug_opts =
21 [
22 headers: %{"cache-control" => @default_cache_control_header},
23 cache_control_for_etags: @default_cache_control_header
24 ]
25 |> Keyword.put(:from, "__unconfigured_media_plug")
26 |> Keyword.put(:at, "/__unconfigured_media_plug")
27 |> Plug.Static.init()
28
29 %{static_plug_opts: static_plug_opts}
30 end
31
32 def call(%{request_path: <<"/", @path, "/", file::binary>>} = conn, opts) do
33 conn =
34 case fetch_query_params(conn) do
35 %{query_params: %{"name" => name}} = conn ->
36 name = String.replace(name, "\"", "\\\"")
37
38 conn
39 |> put_resp_header("content-disposition", "filename=\"#{name}\"")
40
41 conn ->
42 conn
43 end
44
45 config = Pleroma.Config.get(Pleroma.Upload)
46
47 with uploader <- Keyword.fetch!(config, :uploader),
48 proxy_remote = Keyword.get(config, :proxy_remote, false),
49 {:ok, get_method} <- uploader.get_file(file) do
50 get_media(conn, get_method, proxy_remote, opts)
51 else
52 _ ->
53 conn
54 |> send_resp(:internal_server_error, dgettext("errors", "Failed"))
55 |> halt()
56 end
57 end
58
59 def call(conn, _opts), do: conn
60
61 defp get_media(conn, {:static_dir, directory}, _, opts) do
62 static_opts =
63 Map.get(opts, :static_plug_opts)
64 |> Map.put(:at, [@path])
65 |> Map.put(:from, directory)
66
67 conn = Plug.Static.call(conn, static_opts)
68
69 if conn.halted do
70 conn
71 else
72 conn
73 |> send_resp(:not_found, dgettext("errors", "Not found"))
74 |> halt()
75 end
76 end
77
78 defp get_media(conn, {:url, url}, true, _) do
79 conn
80 |> Pleroma.ReverseProxy.call(url, Pleroma.Config.get([Pleroma.Upload, :proxy_opts], []))
81 end
82
83 defp get_media(conn, {:url, url}, _, _) do
84 conn
85 |> Phoenix.Controller.redirect(external: url)
86 |> halt()
87 end
88
89 defp get_media(conn, unknown, _, _) do
90 Logger.error("#{__MODULE__}: Unknown get startegy: #{inspect(unknown)}")
91
92 conn
93 |> send_resp(:internal_server_error, dgettext("errors", "Internal Error"))
94 |> halt()
95 end
96 end