1 defmodule Pleroma.Web.EmojiAPI.EmojiAPIController do
2 use Pleroma.Web, :controller
6 def reload(conn, _params) do
12 @emoji_dir_path Path.join(
13 Pleroma.Config.get!([:instance, :static_dir]),
17 @cache_seconds_per_file Pleroma.Config.get!([:emoji, :shared_pack_cache_seconds_per_file])
19 def list_packs(conn, _params) do
21 case File.ls(@emoji_dir_path) do
27 |> Enum.filter(fn file ->
28 dir_path = Path.join(@emoji_dir_path, file)
29 # Filter to only use the pack.yml packs
30 File.dir?(dir_path) and File.exists?(Path.join(dir_path, "pack.yml"))
32 |> Enum.map(fn pack_name ->
33 pack_path = Path.join(@emoji_dir_path, pack_name)
34 pack_file = Path.join(pack_path, "pack.yml")
36 {pack_name, RelaxYaml.Decoder.read_from_file(pack_file)}
38 # Transform into a map of pack-name => pack-data
39 # Check if all the files are in place and can be sent
40 |> Enum.map(fn {name, pack} ->
41 pack_path = Path.join(@emoji_dir_path, name)
43 if can_download?(pack, pack_path) do
44 archive_for_sha = make_archive(name, pack, pack_path)
45 archive_sha = :crypto.hash(:sha256, archive_for_sha) |> Base.encode16()
49 |> put_in(["pack", "can-download"], true)
50 |> put_in(["pack", "download-sha256"], archive_sha)}
54 |> put_in(["pack", "can-download"], false)}
60 conn |> json(pack_infos)
63 defp can_download?(pack, pack_path) do
64 # If the pack is set as shared, check if it can be downloaded
65 # That means that when asked, the pack can be packed and sent to the remote
66 # Otherwise, they'd have to download it from external-src
67 pack["pack"]["share-files"] and
68 Enum.all?(pack["files"], fn {_, path} ->
69 File.exists?(Path.join(pack_path, path))
73 defp create_archive_and_cache(name, pack, pack_dir, md5) do
76 (pack["files"] |> Enum.map(fn {_, path} -> to_charlist(path) end))
78 {:ok, {_, zip_result}} = :zip.zip('#{name}.zip', files, [:memory, cwd: to_charlist(pack_dir)])
80 cache_ms = :timer.seconds(@cache_seconds_per_file * Enum.count(files))
85 # if pack.yml MD5 changes, the cache is not valid anymore
86 %{pack_yml_md5: md5, pack_data: zip_result},
87 # Add a minute to cache time for every file in the pack
91 Logger.debug("Create an archive for the '#{name}' emoji pack, \
92 keeping it in cache for #{div(cache_ms, 1000)}s")
97 defp make_archive(name, pack, pack_dir) do
98 # Having a different pack.yml md5 invalidates cache
99 pack_yml_md5 = :crypto.hash(:md5, File.read!(Path.join(pack_dir, "pack.yml")))
101 maybe_cached_pack = Cachex.get!(:emoji_packs_cache, name)
104 if is_nil(maybe_cached_pack) do
105 create_archive_and_cache(name, pack, pack_dir, pack_yml_md5)
107 if maybe_cached_pack[:pack_yml_md5] == pack_yml_md5 do
108 Logger.debug("Using cache for the '#{name}' shared emoji pack")
110 maybe_cached_pack[:pack_data]
112 create_archive_and_cache(name, pack, pack_dir, pack_yml_md5)
119 def download_shared(conn, %{"name" => name}) do
120 pack_dir = Path.join(@emoji_dir_path, name)
121 pack_yaml = Path.join(pack_dir, "pack.yml")
123 if File.exists?(pack_yaml) do
124 pack = RelaxYaml.Decoder.read_from_file(pack_yaml)
126 if can_download?(pack, pack_dir) do
127 zip_result = make_archive(name, pack, pack_dir)
130 |> send_download({:binary, zip_result}, filename: "#{name}.zip")
134 |> put_status(:forbidden)
135 |> text("Pack #{name} cannot be downloaded from this instance, either pack sharing\
136 was disabled for this pack or some files are missing")}
141 |> put_status(:not_found)
142 |> text("Pack #{name} does not exist")}
146 def download_from(conn, %{"instance_address" => address, "pack_name" => name} = data) do
147 list_uri = "#{address}/api/pleroma/emoji/packs/list"
149 list = Tesla.get!(list_uri).body |> Jason.decode!()
150 full_pack = list[name]
151 pfiles = full_pack["files"]
152 pack = full_pack["pack"]
156 pack["share-files"] && pack["can-download"] ->
159 sha: pack["download-sha256"],
160 uri: "#{address}/api/pleroma/emoji/packs/download_shared/#{name}"
163 pack["fallback-src"] ->
166 sha: pack["fallback-src-sha256"],
167 uri: pack["fallback-src"],
172 {:error, "The pack was not set as shared and there is no fallback src to download from"}
175 case pack_info_res do
176 {:ok, %{sha: sha, uri: uri} = pinfo} ->
177 sha = Base.decode16!(sha)
178 emoji_archive = Tesla.get!(uri).body
180 got_sha = :crypto.hash(:sha256, emoji_archive)
183 local_name = data["as"] || name
184 pack_dir = Path.join(@emoji_dir_path, local_name)
185 File.mkdir_p!(pack_dir)
189 (pfiles |> Enum.map(fn {_, path} -> to_charlist(path) end))
191 {:ok, _} = :zip.unzip(emoji_archive, cwd: to_charlist(pack_dir), file_list: files)
193 # Fallback URL might not contain a pack.yml file. Put on we have if there's none
194 if pinfo[:fallback] do
195 yaml_path = Path.join(pack_dir, "pack.yml")
197 unless File.exists?(yaml_path) do
198 File.write!(yaml_path, RelaxYaml.Encoder.encode(full_pack, []))
205 |> put_status(:internal_server_error)
206 |> text("SHA256 for the pack doesn't match the one sent by the server")
210 conn |> put_status(:internal_server_error) |> text(e)
214 def delete(conn, %{"name" => name}) do
215 pack_dir = Path.join(@emoji_dir_path, name)
217 case File.rm_rf(pack_dir) do
222 conn |> put_status(:internal_server_error) |> text("Couldn't delete the pack #{name}")