1 defmodule Pleroma.Emoji.Pack do
2 @derive {Jason.Encoder, only: [:files, :pack]}
9 @type t() :: %__MODULE__{
10 files: %{String.t() => Path.t()},
19 @spec create(String.t()) :: :ok | {:error, File.posix()} | {:error, :empty_values}
21 with :ok <- validate_not_empty([name]),
22 dir <- Path.join(emoji_path(), name),
23 :ok <- File.mkdir(dir) do
24 %__MODULE__{pack_file: Path.join(dir, "pack.json")}
29 @spec show(String.t()) :: {:ok, t()} | {:error, atom()}
31 with :ok <- validate_not_empty([name]),
32 {:ok, pack} <- load_pack(name) do
33 {:ok, validate_pack(pack)}
37 @spec delete(String.t()) ::
38 {:ok, [binary()]} | {:error, File.posix(), binary()} | {:error, :empty_values}
40 with :ok <- validate_not_empty([name]) do
47 @spec add_file(String.t(), String.t(), Path.t(), Plug.Upload.t() | String.t()) ::
48 {:ok, t()} | {:error, File.posix() | atom()}
49 def add_file(name, shortcode, filename, file) do
50 with :ok <- validate_not_empty([name, shortcode, filename]),
51 :ok <- validate_emoji_not_exists(shortcode),
52 {:ok, pack} <- load_pack(name),
53 :ok <- save_file(file, pack, filename),
54 {:ok, updated_pack} <- pack |> put_emoji(shortcode, filename) |> save_pack() do
60 @spec delete_file(String.t(), String.t()) ::
61 {:ok, t()} | {:error, File.posix() | atom()}
62 def delete_file(name, shortcode) do
63 with :ok <- validate_not_empty([name, shortcode]),
64 {:ok, pack} <- load_pack(name),
65 :ok <- remove_file(pack, shortcode),
66 {:ok, updated_pack} <- pack |> delete_emoji(shortcode) |> save_pack() do
72 @spec update_file(String.t(), String.t(), String.t(), String.t(), boolean()) ::
73 {:ok, t()} | {:error, File.posix() | atom()}
74 def update_file(name, shortcode, new_shortcode, new_filename, force) do
75 with :ok <- validate_not_empty([name, shortcode, new_shortcode, new_filename]),
76 {:ok, pack} <- load_pack(name),
77 {:ok, filename} <- get_filename(pack, shortcode),
78 :ok <- validate_emoji_not_exists(new_shortcode, force),
79 :ok <- rename_file(pack, filename, new_filename),
80 {:ok, updated_pack} <-
82 |> delete_emoji(shortcode)
83 |> put_emoji(new_shortcode, new_filename)
90 @spec import_from_filesystem() :: {:ok, [String.t()]} | {:error, File.posix() | atom()}
91 def import_from_filesystem do
92 emoji_path = emoji_path()
94 with {:ok, %{access: :read_write}} <- File.stat(emoji_path),
95 {:ok, results} <- File.ls(emoji_path) do
98 |> Enum.map(&Path.join(emoji_path, &1))
99 |> Enum.reject(fn path ->
100 File.dir?(path) and File.exists?(Path.join(path, "pack.json"))
102 |> Enum.map(&write_pack_contents/1)
103 |> Enum.reject(&is_nil/1)
107 {:ok, %{access: _}} -> {:error, :no_read_write}
112 @spec list_remote(String.t()) :: {:ok, map()} | {:error, atom()}
113 def list_remote(url) do
114 uri = url |> String.trim() |> URI.parse()
116 with :ok <- validate_shareable_packs_available(uri) do
118 |> URI.merge("/api/pleroma/emoji/packs")
123 @spec list_local() :: {:ok, map()}
125 with {:ok, results} <- list_packs_dir() do
128 |> Enum.map(fn name ->
129 case load_pack(name) do
134 |> Enum.reject(&is_nil/1)
135 |> Map.new(fn pack -> {pack.name, validate_pack(pack)} end)
141 @spec get_archive(String.t()) :: {:ok, binary()} | {:error, atom()}
142 def get_archive(name) do
143 with {:ok, pack} <- load_pack(name),
144 :ok <- validate_downloadable(pack) do
145 {:ok, fetch_archive(pack)}
149 @spec download(String.t(), String.t(), String.t()) :: :ok | {:error, atom()}
150 def download(name, url, as) do
151 uri = url |> String.trim() |> URI.parse()
153 with :ok <- validate_shareable_packs_available(uri),
154 {:ok, remote_pack} <- uri |> URI.merge("/api/pleroma/emoji/packs/#{name}") |> http_get(),
155 {:ok, %{sha: sha, url: url} = pack_info} <- fetch_pack_info(remote_pack, uri, name),
156 {:ok, archive} <- download_archive(url, sha),
157 pack <- copy_as(remote_pack, as || name),
158 {:ok, _} = unzip(archive, pack_info, remote_pack, pack) do
159 # Fallback can't contain a pack.json file, since that would cause the fallback-src-sha256
160 # in it to depend on itself
161 if pack_info[:fallback] do
169 @spec save_metadata(map(), t()) :: {:ok, t()} | {:error, File.posix()}
170 def save_metadata(metadata, %__MODULE__{} = pack) do
172 |> Map.put(:pack, metadata)
176 @spec update_metadata(String.t(), map()) :: {:ok, t()} | {:error, File.posix()}
177 def update_metadata(name, data) do
178 with {:ok, pack} <- load_pack(name) do
179 if fallback_sha_changed?(pack, data) do
180 update_sha_and_save_metadata(pack, data)
182 save_metadata(data, pack)
187 @spec load_pack(String.t()) :: {:ok, t()} | {:error, :not_found}
188 def load_pack(name) do
189 pack_file = Path.join([emoji_path(), name, "pack.json"])
191 if File.exists?(pack_file) do
196 |> Map.put(:pack_file, pack_file)
197 |> Map.put(:path, Path.dirname(pack_file))
198 |> Map.put(:name, name)
206 @spec emoji_path() :: Path.t()
208 [:instance, :static_dir]
209 |> Pleroma.Config.get!()
210 |> Path.join("emoji")
213 defp validate_emoji_not_exists(shortcode, force \\ false)
214 defp validate_emoji_not_exists(_shortcode, true), do: :ok
216 defp validate_emoji_not_exists(shortcode, _) do
217 case Emoji.get(shortcode) do
219 _ -> {:error, :already_exists}
223 defp write_pack_contents(path) do
225 files: files_from_path(path),
227 pack_file: Path.join(path, "pack.json")
230 case save_pack(pack) do
231 {:ok, _pack} -> Path.basename(path)
236 defp files_from_path(path) do
237 txt_path = Path.join(path, "emoji.txt")
239 if File.exists?(txt_path) do
240 # There's an emoji.txt file, it's likely from a pack installed by the pack manager.
241 # Make a pack.json file from the contents of that emoji.txt file
243 # FIXME: Copy-pasted from Pleroma.Emoji/load_from_file_stream/2
245 # Create a map of shortcodes to filenames from emoji.txt
248 |> String.split("\n")
249 |> Enum.map(&String.trim/1)
250 |> Enum.map(fn line ->
251 case String.split(line, ~r/,\s*/) do
252 # This matches both strings with and without tags
253 # and we don't care about tags here
255 file_dir_name = Path.dirname(file)
257 if String.ends_with?(path, file_dir_name) do
258 {name, Path.basename(file)}
267 |> Enum.reject(&is_nil/1)
270 # If there's no emoji.txt, assume all files
271 # that are of certain extensions from the config are emojis and import them all
272 pack_extensions = Pleroma.Config.get!([:emoji, :pack_extensions])
273 Emoji.Loader.make_shortcode_to_file_map(path, pack_extensions)
277 defp validate_pack(pack) do
279 if downloadable?(pack) do
280 archive = fetch_archive(pack)
281 archive_sha = :crypto.hash(:sha256, archive) |> Base.encode16()
284 |> Map.put("can-download", true)
285 |> Map.put("download-sha256", archive_sha)
287 Map.put(pack.pack, "can-download", false)
290 Map.put(pack, :pack, info)
293 defp downloadable?(pack) do
294 # If the pack is set as shared, check if it can be downloaded
295 # That means that when asked, the pack can be packed and sent to the remote
296 # Otherwise, they'd have to download it from external-src
297 pack.pack["share-files"] &&
298 Enum.all?(pack.files, fn {_, file} ->
299 File.exists?(Path.join(pack.path, file))
303 defp create_archive_and_cache(pack, hash) do
304 files = ['pack.json' | Enum.map(pack.files, fn {_, file} -> to_charlist(file) end)]
307 :zip.zip('#{pack.name}.zip', files, [:memory, cwd: to_charlist(pack.path)])
309 ttl_per_file = Pleroma.Config.get!([:emoji, :shared_pack_cache_seconds_per_file])
310 overall_ttl = :timer.seconds(ttl_per_file * Enum.count(files))
315 # if pack.json MD5 changes, the cache is not valid anymore
316 %{hash: hash, pack_data: result},
317 # Add a minute to cache time for every file in the pack
324 defp save_pack(pack) do
325 with {:ok, json} <- Jason.encode(pack, pretty: true),
326 :ok <- File.write(pack.pack_file, json) do
331 defp from_json(json) do
332 map = Jason.decode!(json)
334 struct(__MODULE__, %{files: map["files"], pack: map["pack"]})
337 defp validate_shareable_packs_available(uri) do
338 with {:ok, %{"links" => links}} <- uri |> URI.merge("/.well-known/nodeinfo") |> http_get(),
339 # Get the actual nodeinfo address and fetch it
340 {:ok, %{"metadata" => %{"features" => features}}} <-
341 links |> List.last() |> Map.get("href") |> http_get() do
342 if Enum.member?(features, "shareable_emoji_packs") do
345 {:error, :not_shareable}
350 defp validate_not_empty(list) do
351 if Enum.all?(list, fn i -> is_binary(i) and i != "" end) do
354 {:error, :empty_values}
358 defp save_file(file, pack, filename) do
359 file_path = Path.join(pack.path, filename)
360 create_subdirs(file_path)
363 %Plug.Upload{path: upload_path} ->
364 # Copy the uploaded file from the temporary directory
365 with {:ok, _} <- File.copy(upload_path, file_path), do: :ok
367 url when is_binary(url) ->
368 # Download and write the file
369 file_contents = Tesla.get!(url).body
370 File.write(file_path, file_contents)
374 defp put_emoji(pack, shortcode, filename) do
375 files = Map.put(pack.files, shortcode, filename)
376 %{pack | files: files}
379 defp delete_emoji(pack, shortcode) do
380 files = Map.delete(pack.files, shortcode)
381 %{pack | files: files}
384 defp rename_file(pack, filename, new_filename) do
385 old_path = Path.join(pack.path, filename)
386 new_path = Path.join(pack.path, new_filename)
387 create_subdirs(new_path)
389 with :ok <- File.rename(old_path, new_path) do
390 remove_dir_if_empty(old_path, filename)
394 defp create_subdirs(file_path) do
395 if String.contains?(file_path, "/") do
402 defp remove_file(pack, shortcode) do
403 with {:ok, filename} <- get_filename(pack, shortcode),
404 emoji <- Path.join(pack.path, filename),
405 :ok <- File.rm(emoji) do
406 remove_dir_if_empty(emoji, filename)
410 defp remove_dir_if_empty(emoji, filename) do
411 dir = Path.dirname(emoji)
413 if String.contains?(filename, "/") and File.ls!(dir) == [] do
420 defp get_filename(pack, shortcode) do
421 with %{^shortcode => filename} when is_binary(filename) <- pack.files,
422 true <- pack.path |> Path.join(filename) |> File.exists?() do
425 _ -> {:error, :doesnt_exist}
429 defp http_get(%URI{} = url), do: url |> to_string() |> http_get()
431 defp http_get(url) do
432 with {:ok, %{body: body}} <- url |> Pleroma.HTTP.get() do
437 defp list_packs_dir do
438 emoji_path = emoji_path()
439 # Create the directory first if it does not exist. This is probably the first request made
440 # with the API so it should be sufficient
441 with {:create_dir, :ok} <- {:create_dir, File.mkdir_p(emoji_path)},
442 {:ls, {:ok, results}} <- {:ls, File.ls(emoji_path)} do
445 {:create_dir, {:error, e}} -> {:error, :create_dir, e}
446 {:ls, {:error, e}} -> {:error, :ls, e}
450 defp validate_downloadable(pack) do
451 if downloadable?(pack), do: :ok, else: {:error, :cant_download}
454 defp copy_as(remote_pack, local_name) do
455 path = Path.join(emoji_path(), local_name)
460 files: remote_pack["files"],
461 pack_file: Path.join(path, "pack.json")
465 defp unzip(archive, pack_info, remote_pack, local_pack) do
466 with :ok <- File.mkdir_p!(local_pack.path) do
467 files = Enum.map(remote_pack["files"], fn {_, path} -> to_charlist(path) end)
468 # Fallback cannot contain a pack.json file
469 files = if pack_info[:fallback], do: files, else: ['pack.json' | files]
471 :zip.unzip(archive, cwd: to_charlist(local_pack.path), file_list: files)
475 defp fetch_pack_info(remote_pack, uri, name) do
476 case remote_pack["pack"] do
477 %{"share-files" => true, "can-download" => true, "download-sha256" => sha} ->
481 url: URI.merge(uri, "/api/pleroma/emoji/packs/#{name}/archive") |> to_string()
484 %{"fallback-src" => src, "fallback-src-sha256" => sha} when is_binary(src) ->
493 {:error, "The pack was not set as shared and there is no fallback src to download from"}
497 defp download_archive(url, sha) do
498 with {:ok, %{body: archive}} <- Tesla.get(url) do
499 if Base.decode16!(sha) == :crypto.hash(:sha256, archive) do
502 {:error, :invalid_checksum}
507 defp fetch_archive(pack) do
508 hash = :crypto.hash(:md5, File.read!(pack.pack_file))
510 case Cachex.get!(:emoji_packs_cache, pack.name) do
511 %{hash: ^hash, pack_data: archive} -> archive
512 _ -> create_archive_and_cache(pack, hash)
516 defp fallback_sha_changed?(pack, data) do
517 is_binary(data[:"fallback-src"]) and data[:"fallback-src"] != pack.pack["fallback-src"]
520 defp update_sha_and_save_metadata(pack, data) do
521 with {:ok, %{body: zip}} <- Tesla.get(data[:"fallback-src"]),
522 :ok <- validate_has_all_files(pack, zip) do
523 fallback_sha = :sha256 |> :crypto.hash(zip) |> Base.encode16()
526 |> Map.put("fallback-src-sha256", fallback_sha)
527 |> save_metadata(pack)
531 defp validate_has_all_files(pack, zip) do
532 with {:ok, f_list} <- :zip.unzip(zip, [:memory]) do
533 # Check if all files from the pack.json are in the archive
535 |> Enum.all?(fn {_, from_manifest} ->
536 List.keyfind(f_list, to_charlist(from_manifest), 0)
538 |> if(do: :ok, else: {:error, :incomplete})