WIP: Stop mangling filenames
authorrinpatch <rinpatch@sdf.org>
Tue, 12 Mar 2019 06:10:19 +0000 (09:10 +0300)
committerrinpatch <rinpatch@sdf.org>
Tue, 12 Mar 2019 06:10:19 +0000 (09:10 +0300)
lib/pleroma/plugs/uploaded_media.ex
lib/pleroma/upload.ex

index 13aa8641afcb2444d195ac87298d98fbb2c8f589..15f447ded319d97767592f0ffb17b999d40b1fac 100644 (file)
@@ -24,6 +24,16 @@ defmodule Pleroma.Plugs.UploadedMedia do
   end
 
   def call(%{request_path: <<"/", @path, "/", file::binary>>} = conn, opts) do
+    conn =
+      case fetch_query_params(conn) do
+        %{query_params: %{"name" => name}} = conn ->
+          conn
+          |> put_resp_header("Content-Disposition", "filename=\"#{name}\"")
+
+        conn ->
+          conn
+      end
+
     config = Pleroma.Config.get([Pleroma.Upload])
 
     with uploader <- Keyword.fetch!(config, :uploader),
index 1a97e9fdee156354fdb26e78bab931a2474e32d9..ae461d434532e099976d6c28f724181adc017a93 100644 (file)
@@ -70,7 +70,7 @@ defmodule Pleroma.Upload do
            %{
              "type" => "Link",
              "mediaType" => upload.content_type,
-             "href" => url_from_spec(opts.base_url, url_spec)
+             "href" => url_from_spec(opts.base_url, url_spec, upload.name)
            }
          ],
          "name" => Map.get(opts, :description) || upload.name
@@ -219,14 +219,12 @@ defmodule Pleroma.Upload do
     tmp_path
   end
 
-  defp url_from_spec(base_url, {:file, path}) do
-    path =
-      path
-      |> URI.encode(&char_unescaped?/1)
+  defp url_from_spec(base_url, {:file, path}, name) do
+    path = URI.encode(path, &char_unescaped?/1) <> "?name=#{URI.encode(name, &char_unescaped?/1)}"
 
     [base_url, "media", path]
     |> Path.join()
   end
 
-  defp url_from_spec(_base_url, {:url, url}), do: url
+  defp url_from_spec(_base_url, {:url, url}, _name), do: url
 end