1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Emoji.Pack do
6 @derive {Jason.Encoder, only: [:files, :pack, :files_count]}
14 @type t() :: %__MODULE__{
15 files: %{String.t() => Path.t()},
16 files_count: non_neg_integer(),
24 alias Pleroma.Emoji.Pack
26 @spec create(String.t()) :: {:ok, t()} | {:error, File.posix()} | {:error, :empty_values}
28 with :ok <- validate_not_empty([name]),
29 dir <- Path.join(emoji_path(), name),
30 :ok <- File.mkdir(dir) do
31 %__MODULE__{pack_file: Path.join(dir, "pack.json")}
36 defp paginate(entities, 1, page_size), do: Enum.take(entities, page_size)
38 defp paginate(entities, page, page_size) do
40 |> Enum.chunk_every(page_size)
44 @spec show(keyword()) :: {:ok, t()} | {:error, atom()}
48 with :ok <- validate_not_empty([name]),
49 {:ok, pack} <- load_pack(name) do
54 |> paginate(opts[:page], opts[:page_size])
56 pack = Map.put(pack, :files, Map.take(pack.files, shortcodes))
58 {:ok, validate_pack(pack)}
62 @spec delete(String.t()) ::
63 {:ok, [binary()]} | {:error, File.posix(), binary()} | {:error, :empty_values}
65 with :ok <- validate_not_empty([name]) do
72 @spec unpack_zip_emojies(list(tuple())) :: list(map())
73 defp unpack_zip_emojies(zip_files) do
74 Enum.reduce(zip_files, [], fn
75 {_, path, s, _, _, _}, acc when elem(s, 2) == :regular ->
77 filename <- Path.basename(path),
78 shortcode <- Path.basename(filename, Path.extname(filename)),
79 false <- Emoji.exist?(shortcode)
81 [%{path: path, filename: path, shortcode: shortcode} | acc]
91 @spec add_file(t(), String.t(), Path.t(), Plug.Upload.t()) ::
93 | {:error, File.posix() | atom()}
94 def add_file(%Pack{} = pack, _, _, %Plug.Upload{content_type: "application/zip"} = file) do
95 with {:ok, zip_files} <- :zip.table(to_charlist(file.path)),
96 [_ | _] = emojies <- unpack_zip_emojies(zip_files),
97 {:ok, tmp_dir} <- Pleroma.Utils.tmp_dir("emoji") do
101 to_charlist(file.path),
102 [{:file_list, Enum.map(emojies, & &1[:path])}, {:cwd, tmp_dir}]
106 Enum.map_reduce(emojies, pack, fn item, emoji_pack ->
107 emoji_file = %Plug.Upload{
108 filename: item[:filename],
109 path: Path.join(tmp_dir, item[:path])
112 {:ok, updated_pack} =
116 to_string(item[:filename]),
130 {:error, _} = error ->
138 def add_file(%Pack{} = pack, shortcode, filename, %Plug.Upload{} = file) do
139 with :ok <- validate_not_empty([shortcode, filename]),
140 :ok <- validate_emoji_not_exists(shortcode),
141 {:ok, updated_pack} <- do_add_file(pack, shortcode, filename, file) do
147 defp do_add_file(pack, shortcode, filename, file) do
148 with :ok <- save_file(file, pack, filename) do
150 |> put_emoji(shortcode, filename)
155 @spec delete_file(t(), String.t()) ::
156 {:ok, t()} | {:error, File.posix() | atom()}
157 def delete_file(%Pack{} = pack, shortcode) do
158 with :ok <- validate_not_empty([shortcode]),
159 :ok <- remove_file(pack, shortcode),
160 {:ok, updated_pack} <- pack |> delete_emoji(shortcode) |> save_pack() do
166 @spec update_file(t(), String.t(), String.t(), String.t(), boolean()) ::
167 {:ok, t()} | {:error, File.posix() | atom()}
168 def update_file(%Pack{} = pack, shortcode, new_shortcode, new_filename, force) do
169 with :ok <- validate_not_empty([shortcode, new_shortcode, new_filename]),
170 {:ok, filename} <- get_filename(pack, shortcode),
171 :ok <- validate_emoji_not_exists(new_shortcode, force),
172 :ok <- rename_file(pack, filename, new_filename),
173 {:ok, updated_pack} <-
175 |> delete_emoji(shortcode)
176 |> put_emoji(new_shortcode, new_filename)
183 @spec import_from_filesystem() :: {:ok, [String.t()]} | {:error, File.posix() | atom()}
184 def import_from_filesystem do
185 emoji_path = emoji_path()
187 with {:ok, %{access: :read_write}} <- File.stat(emoji_path),
188 {:ok, results} <- File.ls(emoji_path) do
191 |> Enum.map(&Path.join(emoji_path, &1))
192 |> Enum.reject(fn path ->
193 File.dir?(path) and File.exists?(Path.join(path, "pack.json"))
195 |> Enum.map(&write_pack_contents/1)
196 |> Enum.reject(&is_nil/1)
200 {:ok, %{access: _}} -> {:error, :no_read_write}
205 @spec list_remote(keyword()) :: {:ok, map()} | {:error, atom()}
206 def list_remote(opts) do
207 uri = opts[:url] |> String.trim() |> URI.parse()
209 with :ok <- validate_shareable_packs_available(uri) do
211 |> URI.merge("/api/pleroma/emoji/packs?page=#{opts[:page]}&page_size=#{opts[:page_size]}")
216 @spec list_local(keyword()) :: {:ok, map(), non_neg_integer()}
217 def list_local(opts) do
218 with {:ok, results} <- list_packs_dir() do
221 |> Enum.map(fn name ->
222 case load_pack(name) do
227 |> Enum.reject(&is_nil/1)
231 |> paginate(opts[:page], opts[:page_size])
232 |> Map.new(fn pack -> {pack.name, validate_pack(pack)} end)
234 {:ok, packs, length(all_packs)}
238 @spec get_archive(String.t()) :: {:ok, binary()} | {:error, atom()}
239 def get_archive(name) do
240 with {:ok, pack} <- load_pack(name),
241 :ok <- validate_downloadable(pack) do
242 {:ok, fetch_archive(pack)}
246 @spec download(String.t(), String.t(), String.t()) :: {:ok, t()} | {:error, atom()}
247 def download(name, url, as) do
248 uri = url |> String.trim() |> URI.parse()
250 with :ok <- validate_shareable_packs_available(uri),
251 {:ok, remote_pack} <-
252 uri |> URI.merge("/api/pleroma/emoji/pack?name=#{name}") |> http_get(),
253 {:ok, %{sha: sha, url: url} = pack_info} <- fetch_pack_info(remote_pack, uri, name),
254 {:ok, archive} <- download_archive(url, sha),
255 pack <- copy_as(remote_pack, as || name),
256 {:ok, _} = unzip(archive, pack_info, remote_pack, pack) do
257 # Fallback can't contain a pack.json file, since that would cause the fallback-src-sha256
258 # in it to depend on itself
259 if pack_info[:fallback] do
267 @spec save_metadata(map(), t()) :: {:ok, t()} | {:error, File.posix()}
268 def save_metadata(metadata, %__MODULE__{} = pack) do
270 |> Map.put(:pack, metadata)
274 @spec update_metadata(String.t(), map()) :: {:ok, t()} | {:error, File.posix()}
275 def update_metadata(name, data) do
276 with {:ok, pack} <- load_pack(name) do
277 if fallback_sha_changed?(pack, data) do
278 update_sha_and_save_metadata(pack, data)
280 save_metadata(data, pack)
285 @spec load_pack(String.t()) :: {:ok, t()} | {:error, :not_found}
286 def load_pack(name) do
287 pack_file = Path.join([emoji_path(), name, "pack.json"])
289 if File.exists?(pack_file) do
294 |> Map.put(:pack_file, pack_file)
295 |> Map.put(:path, Path.dirname(pack_file))
296 |> Map.put(:name, name)
303 {:ok, Map.put(pack, :files_count, files_count)}
309 @spec emoji_path() :: Path.t()
311 [:instance, :static_dir]
312 |> Pleroma.Config.get!()
313 |> Path.join("emoji")
316 defp validate_emoji_not_exists(shortcode, force \\ false)
317 defp validate_emoji_not_exists(_shortcode, true), do: :ok
319 defp validate_emoji_not_exists(shortcode, _) do
320 if Emoji.exist?(shortcode) do
321 {:error, :already_exists}
327 defp write_pack_contents(path) do
329 files: files_from_path(path),
331 pack_file: Path.join(path, "pack.json")
334 case save_pack(pack) do
335 {:ok, _pack} -> Path.basename(path)
340 defp files_from_path(path) do
341 txt_path = Path.join(path, "emoji.txt")
343 if File.exists?(txt_path) do
344 # There's an emoji.txt file, it's likely from a pack installed by the pack manager.
345 # Make a pack.json file from the contents of that emoji.txt file
347 # FIXME: Copy-pasted from Pleroma.Emoji/load_from_file_stream/2
349 # Create a map of shortcodes to filenames from emoji.txt
352 |> String.split("\n")
353 |> Enum.map(&String.trim/1)
354 |> Enum.map(fn line ->
355 case String.split(line, ~r/,\s*/) do
356 # This matches both strings with and without tags
357 # and we don't care about tags here
359 file_dir_name = Path.dirname(file)
361 if String.ends_with?(path, file_dir_name) do
362 {name, Path.basename(file)}
371 |> Enum.reject(&is_nil/1)
374 # If there's no emoji.txt, assume all files
375 # that are of certain extensions from the config are emojis and import them all
376 pack_extensions = Pleroma.Config.get!([:emoji, :pack_extensions])
377 Emoji.Loader.make_shortcode_to_file_map(path, pack_extensions)
381 defp validate_pack(pack) do
383 if downloadable?(pack) do
384 archive = fetch_archive(pack)
385 archive_sha = :crypto.hash(:sha256, archive) |> Base.encode16()
388 |> Map.put("can-download", true)
389 |> Map.put("download-sha256", archive_sha)
391 Map.put(pack.pack, "can-download", false)
394 Map.put(pack, :pack, info)
397 defp downloadable?(pack) do
398 # If the pack is set as shared, check if it can be downloaded
399 # That means that when asked, the pack can be packed and sent to the remote
400 # Otherwise, they'd have to download it from external-src
401 pack.pack["share-files"] &&
402 Enum.all?(pack.files, fn {_, file} ->
409 defp create_archive_and_cache(pack, hash) do
410 files = ['pack.json' | Enum.map(pack.files, fn {_, file} -> to_charlist(file) end)]
413 :zip.zip('#{pack.name}.zip', files, [:memory, cwd: to_charlist(pack.path)])
415 ttl_per_file = Pleroma.Config.get!([:emoji, :shared_pack_cache_seconds_per_file])
416 overall_ttl = :timer.seconds(ttl_per_file * Enum.count(files))
421 # if pack.json MD5 changes, the cache is not valid anymore
422 %{hash: hash, pack_data: result},
423 # Add a minute to cache time for every file in the pack
430 defp save_pack(pack) do
431 with {:ok, json} <- Jason.encode(pack, pretty: true),
432 :ok <- File.write(pack.pack_file, json) do
437 defp from_json(json) do
438 map = Jason.decode!(json)
440 struct(__MODULE__, %{files: map["files"], pack: map["pack"]})
443 defp validate_shareable_packs_available(uri) do
444 with {:ok, %{"links" => links}} <- uri |> URI.merge("/.well-known/nodeinfo") |> http_get(),
445 # Get the actual nodeinfo address and fetch it
446 {:ok, %{"metadata" => %{"features" => features}}} <-
447 links |> List.last() |> Map.get("href") |> http_get() do
448 if Enum.member?(features, "shareable_emoji_packs") do
451 {:error, :not_shareable}
456 defp validate_not_empty(list) do
457 if Enum.all?(list, fn i -> is_binary(i) and i != "" end) do
460 {:error, :empty_values}
464 defp save_file(%Plug.Upload{path: upload_path}, pack, filename) do
465 file_path = Path.join(pack.path, filename)
466 create_subdirs(file_path)
468 with {:ok, _} <- File.copy(upload_path, file_path) do
473 defp put_emoji(pack, shortcode, filename) do
474 files = Map.put(pack.files, shortcode, filename)
475 %{pack | files: files, files_count: length(Map.keys(files))}
478 defp delete_emoji(pack, shortcode) do
479 files = Map.delete(pack.files, shortcode)
480 %{pack | files: files}
483 defp rename_file(pack, filename, new_filename) do
484 old_path = Path.join(pack.path, filename)
485 new_path = Path.join(pack.path, new_filename)
486 create_subdirs(new_path)
488 with :ok <- File.rename(old_path, new_path) do
489 remove_dir_if_empty(old_path, filename)
493 defp create_subdirs(file_path) do
494 if String.contains?(file_path, "/") do
501 defp remove_file(pack, shortcode) do
502 with {:ok, filename} <- get_filename(pack, shortcode),
503 emoji <- Path.join(pack.path, filename),
504 :ok <- File.rm(emoji) do
505 remove_dir_if_empty(emoji, filename)
509 defp remove_dir_if_empty(emoji, filename) do
510 dir = Path.dirname(emoji)
512 if String.contains?(filename, "/") and File.ls!(dir) == [] do
519 defp get_filename(pack, shortcode) do
520 with %{^shortcode => filename} when is_binary(filename) <- pack.files,
521 true <- pack.path |> Path.join(filename) |> File.exists?() do
524 _ -> {:error, :doesnt_exist}
528 defp http_get(%URI{} = url), do: url |> to_string() |> http_get()
530 defp http_get(url) do
531 with {:ok, %{body: body}} <- Pleroma.HTTP.get(url, [], pool: :default) do
536 defp list_packs_dir do
537 emoji_path = emoji_path()
538 # Create the directory first if it does not exist. This is probably the first request made
539 # with the API so it should be sufficient
540 with {:create_dir, :ok} <- {:create_dir, File.mkdir_p(emoji_path)},
541 {:ls, {:ok, results}} <- {:ls, File.ls(emoji_path)} do
542 {:ok, Enum.sort(results)}
544 {:create_dir, {:error, e}} -> {:error, :create_dir, e}
545 {:ls, {:error, e}} -> {:error, :ls, e}
549 defp validate_downloadable(pack) do
550 if downloadable?(pack), do: :ok, else: {:error, :cant_download}
553 defp copy_as(remote_pack, local_name) do
554 path = Path.join(emoji_path(), local_name)
559 files: remote_pack["files"],
560 pack_file: Path.join(path, "pack.json")
564 defp unzip(archive, pack_info, remote_pack, local_pack) do
565 with :ok <- File.mkdir_p!(local_pack.path) do
566 files = Enum.map(remote_pack["files"], fn {_, path} -> to_charlist(path) end)
567 # Fallback cannot contain a pack.json file
568 files = if pack_info[:fallback], do: files, else: ['pack.json' | files]
570 :zip.unzip(archive, cwd: to_charlist(local_pack.path), file_list: files)
574 defp fetch_pack_info(remote_pack, uri, name) do
575 case remote_pack["pack"] do
576 %{"share-files" => true, "can-download" => true, "download-sha256" => sha} ->
580 url: URI.merge(uri, "/api/pleroma/emoji/packs/archive?name=#{name}") |> to_string()
583 %{"fallback-src" => src, "fallback-src-sha256" => sha} when is_binary(src) ->
592 {:error, "The pack was not set as shared and there is no fallback src to download from"}
596 defp download_archive(url, sha) do
597 with {:ok, %{body: archive}} <- Pleroma.HTTP.get(url) do
598 if Base.decode16!(sha) == :crypto.hash(:sha256, archive) do
601 {:error, :invalid_checksum}
606 defp fetch_archive(pack) do
607 hash = :crypto.hash(:md5, File.read!(pack.pack_file))
609 case Cachex.get!(:emoji_packs_cache, pack.name) do
610 %{hash: ^hash, pack_data: archive} -> archive
611 _ -> create_archive_and_cache(pack, hash)
615 defp fallback_sha_changed?(pack, data) do
616 is_binary(data[:"fallback-src"]) and data[:"fallback-src"] != pack.pack["fallback-src"]
619 defp update_sha_and_save_metadata(pack, data) do
620 with {:ok, %{body: zip}} <- Pleroma.HTTP.get(data[:"fallback-src"]),
621 :ok <- validate_has_all_files(pack, zip) do
622 fallback_sha = :sha256 |> :crypto.hash(zip) |> Base.encode16()
625 |> Map.put("fallback-src-sha256", fallback_sha)
626 |> save_metadata(pack)
630 defp validate_has_all_files(pack, zip) do
631 with {:ok, f_list} <- :zip.unzip(zip, [:memory]) do
632 # Check if all files from the pack.json are in the archive
634 |> Enum.all?(fn {_, from_manifest} ->
635 List.keyfind(f_list, to_charlist(from_manifest), 0)
637 |> if(do: :ok, else: {:error, :incomplete})