Add an endpoint for emoji pack metadata updating

This commit is contained in:
Ekaterina Vaartis 2019-08-15 19:55:58 +03:00
parent adf31d596e
commit bcc0bfd0c5
2 changed files with 49 additions and 1 deletions

View file

@ -196,7 +196,7 @@ def download_from(conn, %{"instance_address" => address, "pack_name" => name} =
if pinfo[:fallback] do
pack_file_path = Path.join(pack_dir, "pack.json")
File.write!(pack_file_path, Jason.encode!(full_pack))
File.write!(pack_file_path, Jason.encode!(full_pack, pretty: true))
end
conn |> text("ok")
@ -222,4 +222,51 @@ def delete(conn, %{"name" => name}) do
conn |> put_status(:internal_server_error) |> text("Couldn't delete the pack #{name}")
end
end
def update_metadata(conn, %{"name" => name, "new_data" => new_data}) do
pack_dir = Path.join(@emoji_dir_path, name)
pack_file_p = Path.join(pack_dir, "pack.json")
full_pack = Jason.decode!(File.read!(pack_file_p))
new_data =
if not is_nil(new_data["fallback-src"]) and is_nil(new_data["fallback-src-sha256"]) do
pack_arch = Tesla.get!(new_data["fallback-src"]).body
{:ok, flist} = :zip.unzip(pack_arch, [:memory])
# Check if all files from the pack.json are in the archive
has_all_files =
Enum.all?(full_pack["files"], fn {_, from_manifest} ->
Enum.find(flist, fn {from_archive, _} ->
to_string(from_archive) == from_manifest
end)
end)
unless has_all_files do
{:error,
conn
|> put_status(:bad_request)
|> text("The fallback archive does not have all files specified in pack.json")}
else
fallback_sha = :crypto.hash(:sha256, pack_arch) |> Base.encode16()
{:ok, new_data |> Map.put("fallback-src-sha256", fallback_sha)}
end
else
{:ok, new_data}
end
case new_data do
{:ok, new_data} ->
full_pack = Map.put(full_pack, "pack", new_data)
File.write!(pack_file_p, Jason.encode!(full_pack, pretty: true))
# Send new data back with fallback sha filled
conn |> json(new_data)
{:error, e} ->
e
end
end
end

View file

@ -218,6 +218,7 @@ defmodule Pleroma.Web.Router do
# Modifying packs
pipe_through([:admin_api, :oauth_write])
post("/update_metadata/:name", EmojiAPIController, :update_metadata)
delete("/delete/:name", EmojiAPIController, :delete)
post("/download_from", EmojiAPIController, :download_from)
end