Allow uploading new emojis to packs from URLs
authorEkaterina Vaartis <vaartis@cock.li>
Tue, 20 Aug 2019 11:52:36 +0000 (14:52 +0300)
committerEkaterina Vaartis <vaartis@cock.li>
Wed, 18 Sep 2019 21:16:33 +0000 (00:16 +0300)
lib/pleroma/web/emoji_api/emoji_api_controller.ex
test/web/emoji_api_controller_test.exs

index fdecbb700c3e9376cdda8d298d9574d452083b84..87ae0e092ea969dc63c4a69f6e3412e74998f524 100644 (file)
@@ -288,39 +288,44 @@ keeping it in cache for #{div(cache_ms, 1000)}s")
       case action do
         "add" ->
           unless Map.has_key?(full_pack["files"], shortcode) do
-            with %{"file" => %Plug.Upload{filename: filename, path: upload_path}} <- params do
-              # If there was a file name provided with the request, use it, otherwise just use the
-              # uploaded file name
-              filename =
-                if Map.has_key?(params, "filename") do
-                  params["filename"]
-                else
-                  filename
+            filename =
+              if Map.has_key?(params, "filename") do
+                params["filename"]
+              else
+                case params["file"] do
+                  %Plug.Upload{filename: filename} -> filename
+                  url when is_binary(url) -> Path.basename(url)
                 end
+              end
 
-              unless String.trim(shortcode) |> String.length() == 0 or
-                       String.trim(filename) |> String.length() == 0 do
-                file_path = Path.join(pack_dir, filename)
+            unless String.trim(shortcode) |> String.length() == 0 or
+                     String.trim(filename) |> String.length() == 0 do
+              file_path = Path.join(pack_dir, filename)
 
-                # If the name contains directories, create them
-                if String.contains?(file_path, "/") do
-                  File.mkdir_p!(Path.dirname(file_path))
-                end
-
-                # Copy the uploaded file from the temporary directory
-                File.copy!(upload_path, file_path)
+              # If the name contains directories, create them
+              if String.contains?(file_path, "/") do
+                File.mkdir_p!(Path.dirname(file_path))
+              end
 
-                updated_full_pack = put_in(full_pack, ["files", shortcode], filename)
+              case params["file"] do
+                %Plug.Upload{path: upload_path} ->
+                  # Copy the uploaded file from the temporary directory
+                  File.copy!(upload_path, file_path)
 
-                {:ok, updated_full_pack}
-              else
-                {:error,
-                 conn
-                 |> put_status(:bad_request)
-                 |> text("shortcode or filename cannot be empty")}
+                url when is_binary(url) ->
+                  # Download and write the file
+                  file_contents = Tesla.get!(url).body
+                  File.write!(file_path, file_contents)
               end
+
+              updated_full_pack = put_in(full_pack, ["files", shortcode], filename)
+
+              {:ok, updated_full_pack}
             else
-              _ -> {:error, conn |> put_status(:bad_request) |> text("\"file\" not provided")}
+              {:error,
+               conn
+               |> put_status(:bad_request)
+               |> text("shortcode or filename cannot be empty")}
             end
           else
             {:error,
index 6d3603da569fb1a63464ecf1b1dbcc8ef1f7e6f7..c1aece69140fb838b597e34095b4e71a8643491e 100644 (file)
@@ -244,6 +244,7 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIControllerTest do
     on_exit(fn ->
       File.write!(pack_file, original_content)
 
+      File.rm_rf!("#{@emoji_dir_path}/test_pack/blank_url.png")
       File.rm_rf!("#{@emoji_dir_path}/test_pack/dir")
       File.rm_rf!("#{@emoji_dir_path}/test_pack/dir_2")
     end)
@@ -296,5 +297,38 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIControllerTest do
            |> json_response(200) == %{"blank" => "blank.png"}
 
     refute File.exists?("#{@emoji_dir_path}/test_pack/dir_2/")
+
+    mock(fn
+      %{
+        method: :get,
+        url: "https://test-blank/blank_url.png"
+      } ->
+        text(File.read!("#{@emoji_dir_path}/test_pack/blank.png"))
+    end)
+
+    # The name should be inferred from the URL ending
+    from_url = %{
+      "action" => "add",
+      "shortcode" => "blank_url",
+      "file" => "https://test-blank/blank_url.png"
+    }
+
+    assert conn
+           |> post(emoji_api_path(conn, :update_file, "test_pack"), from_url)
+           |> json_response(200) == %{
+             "blank" => "blank.png",
+             "blank_url" => "blank_url.png"
+           }
+
+    assert File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
+
+    assert conn
+           |> post(emoji_api_path(conn, :update_file, "test_pack"), %{
+             "action" => "remove",
+             "shortcode" => "blank_url"
+           })
+           |> json_response(200) == %{"blank" => "blank.png"}
+
+    refute File.exists?("#{@emoji_dir_path}/test_pack/blank_url.png")
   end
 end