MediaProxy: parse filename from content-disposition for non-whitelisted types
authorrinpatch <rinpatch@sdf.org>
Thu, 14 Mar 2019 22:36:29 +0000 (01:36 +0300)
committerrinpatch <rinpatch@sdf.org>
Thu, 14 Mar 2019 22:36:29 +0000 (01:36 +0300)
lib/pleroma/reverse_proxy.ex

index 6298b92f4e4c12ceb4b17feebfde431ee5a747e1..39ede8619a77c974432025151458e35dcedb1263 100644 (file)
@@ -311,7 +311,25 @@ defmodule Pleroma.ReverseProxy do
       end
 
     if attachment? do
-      disposition = "attachment; filename=" <> Keyword.get(opts, :attachment_name, "attachment")
+      name =
+        try do
+          {{"content-disposition", content_disposition_string}, _} =
+            List.keytake(headers, "content-disposition", 0)
+
+          [name] =
+            Regex.run(
+              ~r/filename=\"(.*)\"/u,
+              content_disposition_string || "",
+              capture: :all_but_first
+            )
+
+          name
+        rescue
+          MatchError -> Keyword.get(opts, :attachment_name, "attachment")
+        end
+
+      disposition = "attachment; filename=" <> name
+
       List.keystore(headers, "content-disposition", 0, {"content-disposition", disposition})
     else
       headers