Correct mimetype on bad uploads.
authoreal <eal@waifu.club>
Thu, 9 Nov 2017 12:11:37 +0000 (14:11 +0200)
committereal <eal@waifu.club>
Thu, 9 Nov 2017 13:01:59 +0000 (15:01 +0200)
lib/pleroma/upload.ex
test/upload_test.exs

index 2717377a37f4f6d1c1379fb80f6e225563742601..d5723f5de1cd054d60d093f409e665723fdfefb9 100644 (file)
@@ -8,11 +8,19 @@ defmodule Pleroma.Upload do
     result_file = Path.join(upload_folder, file.filename)
     File.cp!(file.path, result_file)
 
+    # fix content type on some image uploads
+    matches = Regex.named_captures(~r/\.(?<ext>(jpg|jpeg|png|gif))$/i, file.filename)
+    content_type = if file.content_type == "application/octet-stream" and matches do
+      if matches["ext"] == "jpg", do: "image/jpeg", else: "image/#{matches["ext"]}"
+    else
+      file.content_type
+    end
+
     %{
       "type" => "Image",
       "url" => [%{
         "type" => "Link",
-        "mediaType" => file.content_type,
+        "mediaType" => content_type,
         "href" => url_for(Path.join(uuid, :cow_uri.urlencode(file.filename)))
       }],
       "name" => file.filename,
index 71041e83c4ab0d55913f17ce66c13ef8622858ea..f90c4d7137e3686c48b7ad939172e73b8c67481f 100644 (file)
@@ -9,5 +9,17 @@ defmodule Pleroma.UploadTest do
       assert data["name"] == "an [image.jpg"
       assert List.first(data["url"])["href"] == "http://localhost:4001/media/#{data["uuid"]}/an%20%5Bimage.jpg"
     end
+
+    test "fixes an incorrect content type" do
+      file = %Plug.Upload{content_type: "application/octet-stream", path: Path.absname("test/fixtures/image.jpg"), filename: "an [image.jpg"}
+      data = Upload.store(file)
+      assert hd(data["url"])["mediaType"] == "image/jpeg"
+    end
+
+    test "does not modify a valid content type" do
+      file = %Plug.Upload{content_type: "image/png", path: Path.absname("test/fixtures/image.jpg"), filename: "an [image.jpg"}
+      data = Upload.store(file)
+      assert hd(data["url"])["mediaType"] == "image/png"
+    end
   end
 end