4f4e84bfe57ffe681770411bd995f0b5a636e58a
[akkoma] / lib / pleroma / emoji / pack.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Emoji.Pack do
6 @derive {Jason.Encoder, only: [:files, :pack, :files_count]}
7 defstruct files: %{},
8 files_count: 0,
9 pack_file: nil,
10 path: nil,
11 pack: %{},
12 name: nil
13
14 @type t() :: %__MODULE__{
15 files: %{String.t() => Path.t()},
16 files_count: non_neg_integer(),
17 pack_file: Path.t(),
18 path: Path.t(),
19 pack: map(),
20 name: String.t()
21 }
22
23 alias Pleroma.Emoji
24 alias Pleroma.Emoji.Pack
25 alias Pleroma.Utils
26
27 @spec create(String.t()) :: {:ok, t()} | {:error, File.posix()} | {:error, :empty_values}
28 def create(name) do
29 with :ok <- validate_not_empty([name]),
30 dir <- Path.join(emoji_path(), name),
31 :ok <- File.mkdir(dir) do
32 save_pack(%__MODULE__{pack_file: Path.join(dir, "pack.json")})
33 end
34 end
35
36 defp paginate(entities, 1, page_size), do: Enum.take(entities, page_size)
37
38 defp paginate(entities, page, page_size) do
39 entities
40 |> Enum.chunk_every(page_size)
41 |> Enum.at(page - 1)
42 end
43
44 @spec show(keyword()) :: {:ok, t()} | {:error, atom()}
45 def show(opts) do
46 name = opts[:name]
47
48 with :ok <- validate_not_empty([name]),
49 {:ok, pack} <- load_pack(name) do
50 shortcodes =
51 pack.files
52 |> Map.keys()
53 |> Enum.sort()
54 |> paginate(opts[:page], opts[:page_size])
55
56 pack = Map.put(pack, :files, Map.take(pack.files, shortcodes))
57
58 {:ok, validate_pack(pack)}
59 end
60 end
61
62 @spec delete(String.t()) ::
63 {:ok, [binary()]} | {:error, File.posix(), binary()} | {:error, :empty_values}
64 def delete(name) do
65 with :ok <- validate_not_empty([name]) do
66 emoji_path()
67 |> Path.join(name)
68 |> File.rm_rf()
69 end
70 end
71
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 ->
76 with(
77 filename <- Path.basename(path),
78 shortcode <- Path.basename(filename, Path.extname(filename)),
79 false <- Emoji.exist?(shortcode)
80 ) do
81 [%{path: path, filename: path, shortcode: shortcode} | acc]
82 else
83 _ -> acc
84 end
85
86 _, acc ->
87 acc
88 end)
89 end
90
91 @spec add_file(t(), String.t(), Path.t(), Plug.Upload.t()) ::
92 {:ok, 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} <- Utils.tmp_dir("emoji") do
98 try do
99 {:ok, _emoji_files} =
100 :zip.unzip(
101 to_charlist(file.path),
102 [{:file_list, Enum.map(emojies, & &1[:path])}, {:cwd, tmp_dir}]
103 )
104
105 {_, updated_pack} =
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])
110 }
111
112 {:ok, updated_pack} =
113 do_add_file(
114 emoji_pack,
115 item[:shortcode],
116 to_string(item[:filename]),
117 emoji_file
118 )
119
120 {item, updated_pack}
121 end)
122
123 Emoji.reload()
124
125 {:ok, updated_pack}
126 after
127 File.rm_rf(tmp_dir)
128 end
129 else
130 {:error, _} = error ->
131 error
132
133 _ ->
134 {:ok, pack}
135 end
136 end
137
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
142 Emoji.reload()
143 {:ok, updated_pack}
144 end
145 end
146
147 defp do_add_file(pack, shortcode, filename, file) do
148 with :ok <- save_file(file, pack, filename) do
149 pack
150 |> put_emoji(shortcode, filename)
151 |> save_pack()
152 end
153 end
154
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
161 Emoji.reload()
162 {:ok, updated_pack}
163 end
164 end
165
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} <-
174 pack
175 |> delete_emoji(shortcode)
176 |> put_emoji(new_shortcode, new_filename)
177 |> save_pack() do
178 Emoji.reload()
179 {:ok, updated_pack}
180 end
181 end
182
183 @spec import_from_filesystem() :: {:ok, [String.t()]} | {:error, File.posix() | atom()}
184 def import_from_filesystem do
185 emoji_path = emoji_path()
186
187 with {:ok, %{access: :read_write}} <- File.stat(emoji_path),
188 {:ok, results} <- File.ls(emoji_path) do
189 names =
190 results
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"))
194 end)
195 |> Enum.map(&write_pack_contents/1)
196 |> Enum.reject(&is_nil/1)
197
198 {:ok, names}
199 else
200 {:ok, %{access: _}} -> {:error, :no_read_write}
201 e -> e
202 end
203 end
204
205 @spec list_remote(keyword()) :: {:ok, map()} | {:error, atom()}
206 def list_remote(opts) do
207 uri = opts[:url] |> String.trim() |> URI.parse()
208
209 with :ok <- validate_shareable_packs_available(uri) do
210 uri
211 |> URI.merge("/api/pleroma/emoji/packs?page=#{opts[:page]}&page_size=#{opts[:page_size]}")
212 |> http_get()
213 end
214 end
215
216 @spec list_local(keyword()) :: {:ok, map(), non_neg_integer()}
217 def list_local(opts) do
218 with {:ok, results} <- list_packs_dir() do
219 all_packs =
220 results
221 |> Enum.map(fn name ->
222 case load_pack(name) do
223 {:ok, pack} -> pack
224 _ -> nil
225 end
226 end)
227 |> Enum.reject(&is_nil/1)
228
229 packs =
230 all_packs
231 |> paginate(opts[:page], opts[:page_size])
232 |> Map.new(fn pack -> {pack.name, validate_pack(pack)} end)
233
234 {:ok, packs, length(all_packs)}
235 end
236 end
237
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)}
243 end
244 end
245
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()
249
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
260 save_pack(pack)
261 else
262 {:ok, pack}
263 end
264 end
265 end
266
267 @spec save_metadata(map(), t()) :: {:ok, t()} | {:error, File.posix()}
268 def save_metadata(metadata, %__MODULE__{} = pack) do
269 pack
270 |> Map.put(:pack, metadata)
271 |> save_pack()
272 end
273
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)
279 else
280 save_metadata(data, pack)
281 end
282 end
283 end
284
285 @spec load_pack(String.t()) :: {:ok, t()} | {:error, :file.posix()}
286 def load_pack(name) do
287 pack_file = Path.join([emoji_path(), name, "pack.json"])
288
289 with {:ok, _} <- File.stat(pack_file),
290 {:ok, pack_data} <- File.read(pack_file) do
291 pack =
292 from_json(
293 pack_data,
294 %{
295 pack_file: pack_file,
296 path: Path.dirname(pack_file),
297 name: name
298 }
299 )
300
301 files_count =
302 pack.files
303 |> Map.keys()
304 |> length()
305
306 {:ok, Map.put(pack, :files_count, files_count)}
307 end
308 end
309
310 @spec emoji_path() :: Path.t()
311 defp emoji_path do
312 [:instance, :static_dir]
313 |> Pleroma.Config.get!()
314 |> Path.join("emoji")
315 end
316
317 defp validate_emoji_not_exists(shortcode, force \\ false)
318 defp validate_emoji_not_exists(_shortcode, true), do: :ok
319
320 defp validate_emoji_not_exists(shortcode, _) do
321 if Emoji.exist?(shortcode) do
322 {:error, :already_exists}
323 else
324 :ok
325 end
326 end
327
328 defp write_pack_contents(path) do
329 pack = %__MODULE__{
330 files: files_from_path(path),
331 path: path,
332 pack_file: Path.join(path, "pack.json")
333 }
334
335 case save_pack(pack) do
336 {:ok, _pack} -> Path.basename(path)
337 _ -> nil
338 end
339 end
340
341 defp files_from_path(path) do
342 txt_path = Path.join(path, "emoji.txt")
343
344 if File.exists?(txt_path) do
345 # There's an emoji.txt file, it's likely from a pack installed by the pack manager.
346 # Make a pack.json file from the contents of that emoji.txt file
347
348 # FIXME: Copy-pasted from Pleroma.Emoji/load_from_file_stream/2
349
350 # Create a map of shortcodes to filenames from emoji.txt
351 txt_path
352 |> File.read!()
353 |> String.split("\n")
354 |> Enum.map(&String.trim/1)
355 |> Enum.map(fn line ->
356 case String.split(line, ~r/,\s*/) do
357 # This matches both strings with and without tags
358 # and we don't care about tags here
359 [name, file | _] ->
360 file_dir_name = Path.dirname(file)
361
362 if String.ends_with?(path, file_dir_name) do
363 {name, Path.basename(file)}
364 else
365 {name, file}
366 end
367
368 _ ->
369 nil
370 end
371 end)
372 |> Enum.reject(&is_nil/1)
373 |> Map.new()
374 else
375 # If there's no emoji.txt, assume all files
376 # that are of certain extensions from the config are emojis and import them all
377 pack_extensions = Pleroma.Config.get!([:emoji, :pack_extensions])
378 Emoji.Loader.make_shortcode_to_file_map(path, pack_extensions)
379 end
380 end
381
382 defp validate_pack(pack) do
383 info =
384 if downloadable?(pack) do
385 archive = fetch_archive(pack)
386 archive_sha = :crypto.hash(:sha256, archive) |> Base.encode16()
387
388 pack.pack
389 |> Map.put("can-download", true)
390 |> Map.put("download-sha256", archive_sha)
391 else
392 Map.put(pack.pack, "can-download", false)
393 end
394
395 Map.put(pack, :pack, info)
396 end
397
398 defp downloadable?(pack) do
399 # If the pack is set as shared, check if it can be downloaded
400 # That means that when asked, the pack can be packed and sent to the remote
401 # Otherwise, they'd have to download it from external-src
402 pack.pack["share-files"] &&
403 Enum.all?(pack.files, fn {_, file} ->
404 pack.path
405 |> Path.join(file)
406 |> File.exists?()
407 end)
408 end
409
410 defp create_archive_and_cache(pack, hash) do
411 files = ['pack.json' | Enum.map(pack.files, fn {_, file} -> to_charlist(file) end)]
412
413 {:ok, {_, result}} =
414 :zip.zip('#{pack.name}.zip', files, [:memory, cwd: to_charlist(pack.path)])
415
416 ttl_per_file = Pleroma.Config.get!([:emoji, :shared_pack_cache_seconds_per_file])
417 overall_ttl = :timer.seconds(ttl_per_file * Enum.count(files))
418
419 Cachex.put!(
420 :emoji_packs_cache,
421 pack.name,
422 # if pack.json MD5 changes, the cache is not valid anymore
423 %{hash: hash, pack_data: result},
424 # Add a minute to cache time for every file in the pack
425 ttl: overall_ttl
426 )
427
428 result
429 end
430
431 defp save_pack(pack) do
432 with {:ok, json} <- Jason.encode(pack, pretty: true),
433 :ok <- File.write(pack.pack_file, json) do
434 {:ok, pack}
435 end
436 end
437
438 defp from_json(json, attrs) do
439 map = Jason.decode!(json)
440
441 pack_attrs =
442 attrs
443 |> Map.merge(%{
444 files: map["files"],
445 pack: map["pack"]
446 })
447
448 struct(__MODULE__, pack_attrs)
449 end
450
451 defp validate_shareable_packs_available(uri) do
452 with {:ok, %{"links" => links}} <- uri |> URI.merge("/.well-known/nodeinfo") |> http_get(),
453 # Get the actual nodeinfo address and fetch it
454 {:ok, %{"metadata" => %{"features" => features}}} <-
455 links |> List.last() |> Map.get("href") |> http_get() do
456 if Enum.member?(features, "shareable_emoji_packs") do
457 :ok
458 else
459 {:error, :not_shareable}
460 end
461 end
462 end
463
464 defp validate_not_empty(list) do
465 if Enum.all?(list, fn i -> is_binary(i) and i != "" end) do
466 :ok
467 else
468 {:error, :empty_values}
469 end
470 end
471
472 defp save_file(%Plug.Upload{path: upload_path}, pack, filename) do
473 file_path = Path.join(pack.path, filename)
474 create_subdirs(file_path)
475
476 with {:ok, _} <- File.copy(upload_path, file_path) do
477 :ok
478 end
479 end
480
481 defp put_emoji(pack, shortcode, filename) do
482 files = Map.put(pack.files, shortcode, filename)
483 %{pack | files: files, files_count: length(Map.keys(files))}
484 end
485
486 defp delete_emoji(pack, shortcode) do
487 files = Map.delete(pack.files, shortcode)
488 %{pack | files: files}
489 end
490
491 defp rename_file(pack, filename, new_filename) do
492 old_path = Path.join(pack.path, filename)
493 new_path = Path.join(pack.path, new_filename)
494 create_subdirs(new_path)
495
496 with :ok <- File.rename(old_path, new_path) do
497 remove_dir_if_empty(old_path, filename)
498 end
499 end
500
501 defp create_subdirs(file_path) do
502 with true <- String.contains?(file_path, "/"),
503 path <- Path.dirname(file_path),
504 false <- File.exists?(path) do
505 File.mkdir_p!(path)
506 end
507 end
508
509 defp remove_file(pack, shortcode) do
510 with {:ok, filename} <- get_filename(pack, shortcode),
511 emoji <- Path.join(pack.path, filename),
512 :ok <- File.rm(emoji) do
513 remove_dir_if_empty(emoji, filename)
514 end
515 end
516
517 defp remove_dir_if_empty(emoji, filename) do
518 dir = Path.dirname(emoji)
519
520 if String.contains?(filename, "/") and File.ls!(dir) == [] do
521 File.rmdir!(dir)
522 else
523 :ok
524 end
525 end
526
527 defp get_filename(pack, shortcode) do
528 with %{^shortcode => filename} when is_binary(filename) <- pack.files,
529 file_path <- Path.join(pack.path, filename),
530 {:ok, _} <- File.stat(file_path) do
531 {:ok, filename}
532 else
533 {:error, _} = error ->
534 error
535
536 _ ->
537 {:error, :doesnt_exist}
538 end
539 end
540
541 defp http_get(%URI{} = url), do: url |> to_string() |> http_get()
542
543 defp http_get(url) do
544 with {:ok, %{body: body}} <- Pleroma.HTTP.get(url, [], pool: :default) do
545 Jason.decode(body)
546 end
547 end
548
549 defp list_packs_dir do
550 emoji_path = emoji_path()
551 # Create the directory first if it does not exist. This is probably the first request made
552 # with the API so it should be sufficient
553 with {:create_dir, :ok} <- {:create_dir, File.mkdir_p(emoji_path)},
554 {:ls, {:ok, results}} <- {:ls, File.ls(emoji_path)} do
555 {:ok, Enum.sort(results)}
556 else
557 {:create_dir, {:error, e}} -> {:error, :create_dir, e}
558 {:ls, {:error, e}} -> {:error, :ls, e}
559 end
560 end
561
562 defp validate_downloadable(pack) do
563 if downloadable?(pack), do: :ok, else: {:error, :cant_download}
564 end
565
566 defp copy_as(remote_pack, local_name) do
567 path = Path.join(emoji_path(), local_name)
568
569 %__MODULE__{
570 name: local_name,
571 path: path,
572 files: remote_pack["files"],
573 pack_file: Path.join(path, "pack.json")
574 }
575 end
576
577 defp unzip(archive, pack_info, remote_pack, local_pack) do
578 with :ok <- File.mkdir_p!(local_pack.path) do
579 files = Enum.map(remote_pack["files"], fn {_, path} -> to_charlist(path) end)
580 # Fallback cannot contain a pack.json file
581 files = if pack_info[:fallback], do: files, else: ['pack.json' | files]
582
583 :zip.unzip(archive, cwd: to_charlist(local_pack.path), file_list: files)
584 end
585 end
586
587 defp fetch_pack_info(remote_pack, uri, name) do
588 case remote_pack["pack"] do
589 %{"share-files" => true, "can-download" => true, "download-sha256" => sha} ->
590 {:ok,
591 %{
592 sha: sha,
593 url: URI.merge(uri, "/api/pleroma/emoji/packs/archive?name=#{name}") |> to_string()
594 }}
595
596 %{"fallback-src" => src, "fallback-src-sha256" => sha} when is_binary(src) ->
597 {:ok,
598 %{
599 sha: sha,
600 url: src,
601 fallback: true
602 }}
603
604 _ ->
605 {:error, "The pack was not set as shared and there is no fallback src to download from"}
606 end
607 end
608
609 defp download_archive(url, sha) do
610 with {:ok, %{body: archive}} <- Pleroma.HTTP.get(url) do
611 if Base.decode16!(sha) == :crypto.hash(:sha256, archive) do
612 {:ok, archive}
613 else
614 {:error, :invalid_checksum}
615 end
616 end
617 end
618
619 defp fetch_archive(pack) do
620 hash = :crypto.hash(:md5, File.read!(pack.pack_file))
621
622 case Cachex.get!(:emoji_packs_cache, pack.name) do
623 %{hash: ^hash, pack_data: archive} -> archive
624 _ -> create_archive_and_cache(pack, hash)
625 end
626 end
627
628 defp fallback_sha_changed?(pack, data) do
629 is_binary(data[:"fallback-src"]) and data[:"fallback-src"] != pack.pack["fallback-src"]
630 end
631
632 defp update_sha_and_save_metadata(pack, data) do
633 with {:ok, %{body: zip}} <- Pleroma.HTTP.get(data[:"fallback-src"]),
634 :ok <- validate_has_all_files(pack, zip) do
635 fallback_sha = :sha256 |> :crypto.hash(zip) |> Base.encode16()
636
637 data
638 |> Map.put("fallback-src-sha256", fallback_sha)
639 |> save_metadata(pack)
640 end
641 end
642
643 defp validate_has_all_files(pack, zip) do
644 with {:ok, f_list} <- :zip.unzip(zip, [:memory]) do
645 # Check if all files from the pack.json are in the archive
646 pack.files
647 |> Enum.all?(fn {_, from_manifest} ->
648 List.keyfind(f_list, to_charlist(from_manifest), 0)
649 end)
650 |> if(do: :ok, else: {:error, :incomplete})
651 end
652 end
653 end