forked from AkkomaGang/akkoma
Split list_packs
This commit is contained in:
parent
f251225cae
commit
b8a214b0ab
1 changed files with 39 additions and 36 deletions
|
@ -23,47 +23,49 @@ def reload(conn, _params) do
|
||||||
a map of "pack directory name" to pack.json contents.
|
a map of "pack directory name" to pack.json contents.
|
||||||
"""
|
"""
|
||||||
def list_packs(conn, _params) do
|
def list_packs(conn, _params) do
|
||||||
|
with {:ok, results} <- File.ls(@emoji_dir_path) do
|
||||||
pack_infos =
|
pack_infos =
|
||||||
case File.ls(@emoji_dir_path) do
|
|
||||||
{:error, _} ->
|
|
||||||
%{}
|
|
||||||
|
|
||||||
{:ok, results} ->
|
|
||||||
results
|
results
|
||||||
|> Enum.filter(fn file ->
|
|> Enum.filter(&has_pack_json?/1)
|
||||||
|
|> Enum.map(&load_pack/1)
|
||||||
|
# Check if all the files are in place and can be sent
|
||||||
|
|> Enum.map(&validate_pack/1)
|
||||||
|
# Transform into a map of pack-name => pack-data
|
||||||
|
|> Enum.into(%{})
|
||||||
|
|
||||||
|
json(conn, pack_infos)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp has_pack_json?(file) do
|
||||||
dir_path = Path.join(@emoji_dir_path, file)
|
dir_path = Path.join(@emoji_dir_path, file)
|
||||||
# Filter to only use the pack.json packs
|
# Filter to only use the pack.json packs
|
||||||
File.dir?(dir_path) and File.exists?(Path.join(dir_path, "pack.json"))
|
File.dir?(dir_path) and File.exists?(Path.join(dir_path, "pack.json"))
|
||||||
end)
|
end
|
||||||
|> Enum.map(fn pack_name ->
|
|
||||||
|
defp load_pack(pack_name) do
|
||||||
pack_path = Path.join(@emoji_dir_path, pack_name)
|
pack_path = Path.join(@emoji_dir_path, pack_name)
|
||||||
pack_file = Path.join(pack_path, "pack.json")
|
pack_file = Path.join(pack_path, "pack.json")
|
||||||
|
|
||||||
{pack_name, Jason.decode!(File.read!(pack_file))}
|
{pack_name, Jason.decode!(File.read!(pack_file))}
|
||||||
end)
|
end
|
||||||
# Transform into a map of pack-name => pack-data
|
|
||||||
# Check if all the files are in place and can be sent
|
defp validate_pack({name, pack}) do
|
||||||
|> Enum.map(fn {name, pack} ->
|
|
||||||
pack_path = Path.join(@emoji_dir_path, name)
|
pack_path = Path.join(@emoji_dir_path, name)
|
||||||
|
|
||||||
if can_download?(pack, pack_path) do
|
if can_download?(pack, pack_path) do
|
||||||
archive_for_sha = make_archive(name, pack, pack_path)
|
archive_for_sha = make_archive(name, pack, pack_path)
|
||||||
archive_sha = :crypto.hash(:sha256, archive_for_sha) |> Base.encode16()
|
archive_sha = :crypto.hash(:sha256, archive_for_sha) |> Base.encode16()
|
||||||
|
|
||||||
{name,
|
pack =
|
||||||
pack
|
pack
|
||||||
|> put_in(["pack", "can-download"], true)
|
|> put_in(["pack", "can-download"], true)
|
||||||
|> put_in(["pack", "download-sha256"], archive_sha)}
|
|> put_in(["pack", "download-sha256"], archive_sha)
|
||||||
else
|
|
||||||
{name,
|
|
||||||
pack
|
|
||||||
|> put_in(["pack", "can-download"], false)}
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|> Enum.into(%{})
|
|
||||||
end
|
|
||||||
|
|
||||||
conn |> json(pack_infos)
|
{name, pack}
|
||||||
|
else
|
||||||
|
{name, put_in(pack, ["pack", "can-download"], false)}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp can_download?(pack, pack_path) do
|
defp can_download?(pack, pack_path) do
|
||||||
|
@ -159,6 +161,7 @@ def download_from(conn, %{"instance_address" => address, "pack_name" => name} =
|
||||||
|> Map.get(:body)
|
|> Map.get(:body)
|
||||||
|> Jason.decode!()
|
|> Jason.decode!()
|
||||||
|> Map.get(name)
|
|> Map.get(name)
|
||||||
|
|
||||||
pfiles = full_pack["files"]
|
pfiles = full_pack["files"]
|
||||||
|
|
||||||
pack_info_res =
|
pack_info_res =
|
||||||
|
|
Loading…
Reference in a new issue