diff options
author | Ekaterina Vaartis <vaartis@cock.li> | 2019-08-20 14:52:36 +0300 |
---|---|---|
committer | Ekaterina Vaartis <vaartis@cock.li> | 2019-09-19 00:16:33 +0300 |
commit | 8dbdd5c280d15fde4712989001d4ddee1cd37cff (patch) | |
tree | 5c9a4b4e5a65ddc657a909d8956dbdba08d89be6 /lib | |
parent | 16edfef12e6781971e2056a80a0ac38dcc254b1b (diff) | |
download | pleroma-8dbdd5c280d15fde4712989001d4ddee1cd37cff.tar.gz |
Allow uploading new emojis to packs from URLs
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/web/emoji_api/emoji_api_controller.ex | 57 |
1 files changed, 31 insertions, 26 deletions
diff --git a/lib/pleroma/web/emoji_api/emoji_api_controller.ex b/lib/pleroma/web/emoji_api/emoji_api_controller.ex index fdecbb700..87ae0e092 100644 --- a/lib/pleroma/web/emoji_api/emoji_api_controller.ex +++ b/lib/pleroma/web/emoji_api/emoji_api_controller.ex @@ -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, |