1 defmodule Pleroma.Web.EmojiAPI.EmojiAPIController do
2 use Pleroma.Web, :controller
4 def reload(conn, _params) do
10 @emoji_dir_path Path.join(
11 Pleroma.Config.get!([:instance, :static_dir]),
15 def list_packs(conn, _params) do
17 case File.ls(@emoji_dir_path) do
23 |> Enum.filter(fn file ->
24 dir_path = Path.join(@emoji_dir_path, file)
25 # Filter to only use the pack.toml packs
26 File.dir?(dir_path) and File.exists?(Path.join(dir_path, "pack.toml"))
28 |> Enum.map(fn pack_name ->
29 pack_path = Path.join(@emoji_dir_path, pack_name)
30 pack_file = Path.join(pack_path, "pack.toml")
32 {pack_name, Toml.decode_file!(pack_file)}
34 # Transform into a map of pack-name => pack-data
35 # Check if all the files are in place and can be sent
36 |> Enum.map(fn {name, pack} ->
37 pack_path = Path.join(@emoji_dir_path, name)
39 archive_for_sha = make_archive(name, pack, pack_path)
40 archive_sha = :crypto.hash(:sha256, archive_for_sha) |> Base.encode16()
44 |> put_in(["pack", "can-download"], can_download?(pack, pack_path))
45 |> put_in(["pack", "download-sha256"], archive_sha)}
50 conn |> json(pack_infos)
53 defp can_download?(pack, pack_path) do
54 # If the pack is set as shared, check if it can be downloaded
55 # That means that when asked, the pack can be packed and sent to the remote
56 # Otherwise, they'd have to download it from external-src
57 pack["pack"]["share-files"] and
58 Enum.all?(pack["files"], fn {_, path} ->
59 File.exists?(Path.join(pack_path, path))
63 defp make_archive(name, pack, pack_dir) do
66 (pack["files"] |> Enum.map(fn {_, path} -> to_charlist(path) end))
68 {:ok, {_, zip_result}} = :zip.zip('#{name}.zip', files, [:memory, cwd: to_charlist(pack_dir)])
73 def download_shared(conn, %{"name" => name}) do
74 pack_dir = Path.join(@emoji_dir_path, name)
75 pack_toml = Path.join(pack_dir, "pack.toml")
77 if File.exists?(pack_toml) do
78 pack = Toml.decode_file!(pack_toml)
80 if can_download?(pack, pack_dir) do
81 zip_result = make_archive(name, pack, pack_dir)
84 |> send_download({:binary, zip_result}, filename: "#{name}.zip")
88 |> put_status(:forbidden)
89 |> json("Pack #{name} cannot be downloaded from this instance, either pack sharing\
90 was disabled for this pack or some files are missing")}
95 |> put_status(:not_found)
96 |> json("Pack #{name} does not exist")}
100 def download_from(conn, %{"instance_address" => address, "pack_name" => name} = data) do
101 list_uri = "#{address}/api/pleroma/emoji/packs/list"
103 list = Tesla.get!(list_uri).body |> Jason.decode!()
104 full_pack = list[name]
105 pfiles = full_pack["files"]
106 pack = full_pack["pack"]
110 pack["share-files"] && pack["can-download"] ->
113 sha: pack["download-sha256"],
114 uri: "#{address}/api/pleroma/emoji/packs/download_shared/#{name}"
117 pack["fallback-src"] ->
120 sha: pack["fallback-src-sha256"],
121 uri: pack["fallback-src"],
126 {:error, "The pack was not set as shared and the is no fallback url to download from"}
129 case pack_info_res do
130 {:ok, %{sha: sha, uri: uri} = pinfo} ->
131 sha = Base.decode16!(sha)
132 emoji_archive = Tesla.get!(uri).body
134 got_sha = :crypto.hash(:sha256, emoji_archive)
137 local_name = data["as"] || name
138 pack_dir = Path.join(@emoji_dir_path, local_name)
139 File.mkdir_p!(pack_dir)
143 (pfiles |> Enum.map(fn {_, path} -> to_charlist(path) end))
145 {:ok, _} = :zip.unzip(emoji_archive, cwd: to_charlist(pack_dir), file_list: files)
147 # Fallback URL might not contain a pack.toml file, if that happens - fail (for now)
148 # FIXME: there seems to be a lack of any kind of encoders besides JSON.
150 if pinfo[:fallback] do
151 toml_path = Path.join(pack_dir, "pack.toml")
153 unless File.exists?(toml_path) do
155 |> put_status(:internal_server_error)
156 |> text("No pack.toml in falblack source")
160 if not is_nil(erres), do: erres, else: conn |> text("ok")
163 |> put_status(:internal_server_error)
164 |> text("SHA256 for the pack doesn't match the one sent by the server")
168 conn |> put_status(:internal_server_error) |> text(e)