Allow uploading new emojis to packs from URLs

This commit is contained in:
Ekaterina Vaartis 2019-08-20 14:52:36 +03:00
parent 16edfef12e
commit 8dbdd5c280
2 changed files with 69 additions and 30 deletions

View File

@ -288,14 +288,14 @@ def update_file(
case action do case action do
"add" -> "add" ->
unless Map.has_key?(full_pack["files"], shortcode) do 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 = filename =
if Map.has_key?(params, "filename") do if Map.has_key?(params, "filename") do
params["filename"] params["filename"]
else else
filename case params["file"] do
%Plug.Upload{filename: filename} -> filename
url when is_binary(url) -> Path.basename(url)
end
end end
unless String.trim(shortcode) |> String.length() == 0 or unless String.trim(shortcode) |> String.length() == 0 or
@ -307,9 +307,17 @@ def update_file(
File.mkdir_p!(Path.dirname(file_path)) File.mkdir_p!(Path.dirname(file_path))
end end
case params["file"] do
%Plug.Upload{path: upload_path} ->
# Copy the uploaded file from the temporary directory # Copy the uploaded file from the temporary directory
File.copy!(upload_path, file_path) File.copy!(upload_path, file_path)
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) updated_full_pack = put_in(full_pack, ["files", shortcode], filename)
{:ok, updated_full_pack} {:ok, updated_full_pack}
@ -319,9 +327,6 @@ def update_file(
|> put_status(:bad_request) |> put_status(:bad_request)
|> text("shortcode or filename cannot be empty")} |> text("shortcode or filename cannot be empty")}
end end
else
_ -> {:error, conn |> put_status(:bad_request) |> text("\"file\" not provided")}
end
else else
{:error, {:error,
conn conn

View File

@ -244,6 +244,7 @@ test "updating pack files" do
on_exit(fn -> on_exit(fn ->
File.write!(pack_file, original_content) 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")
File.rm_rf!("#{@emoji_dir_path}/test_pack/dir_2") File.rm_rf!("#{@emoji_dir_path}/test_pack/dir_2")
end) end)
@ -296,5 +297,38 @@ test "updating pack files" do
|> json_response(200) == %{"blank" => "blank.png"} |> json_response(200) == %{"blank" => "blank.png"}
refute File.exists?("#{@emoji_dir_path}/test_pack/dir_2/") 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
end end