update adminfe
[akkoma] / lib / pleroma / emoji / pack.ex
1 defmodule Pleroma.Emoji.Pack do
2 @derive {Jason.Encoder, only: [:files, :pack]}
3 defstruct files: %{},
4 pack_file: nil,
5 path: nil,
6 pack: %{},
7 name: nil
8
9 @type t() :: %__MODULE__{
10 files: %{String.t() => Path.t()},
11 pack_file: Path.t(),
12 path: Path.t(),
13 pack: map(),
14 name: String.t()
15 }
16
17 alias Pleroma.Emoji
18
19 @spec create(String.t()) :: :ok | {:error, File.posix()} | {:error, :empty_values}
20 def create(name) do
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")}
25 |> save_pack()
26 end
27 end
28
29 @spec show(String.t()) :: {:ok, t()} | {:error, atom()}
30 def show(name) do
31 with :ok <- validate_not_empty([name]),
32 {:ok, pack} <- load_pack(name) do
33 {:ok, validate_pack(pack)}
34 end
35 end
36
37 @spec delete(String.t()) ::
38 {:ok, [binary()]} | {:error, File.posix(), binary()} | {:error, :empty_values}
39 def delete(name) do
40 with :ok <- validate_not_empty([name]) do
41 emoji_path()
42 |> Path.join(name)
43 |> File.rm_rf()
44 end
45 end
46
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
55 Emoji.reload()
56 {:ok, updated_pack}
57 end
58 end
59
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
67 Emoji.reload()
68 {:ok, updated_pack}
69 end
70 end
71
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} <-
81 pack
82 |> delete_emoji(shortcode)
83 |> put_emoji(new_shortcode, new_filename)
84 |> save_pack() do
85 Emoji.reload()
86 {:ok, updated_pack}
87 end
88 end
89
90 @spec import_from_filesystem() :: {:ok, [String.t()]} | {:error, File.posix() | atom()}
91 def import_from_filesystem do
92 emoji_path = emoji_path()
93
94 with {:ok, %{access: :read_write}} <- File.stat(emoji_path),
95 {:ok, results} <- File.ls(emoji_path) do
96 names =
97 results
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"))
101 end)
102 |> Enum.map(&write_pack_contents/1)
103 |> Enum.reject(&is_nil/1)
104
105 {:ok, names}
106 else
107 {:ok, %{access: _}} -> {:error, :no_read_write}
108 e -> e
109 end
110 end
111
112 @spec list_remote(String.t()) :: {:ok, map()} | {:error, atom()}
113 def list_remote(url) do
114 uri = url |> String.trim() |> URI.parse()
115
116 with :ok <- validate_shareable_packs_available(uri) do
117 uri
118 |> URI.merge("/api/pleroma/emoji/packs")
119 |> http_get()
120 end
121 end
122
123 @spec list_local() :: {:ok, map()}
124 def list_local do
125 with {:ok, results} <- list_packs_dir() do
126 packs =
127 results
128 |> Enum.map(fn name ->
129 case load_pack(name) do
130 {:ok, pack} -> pack
131 _ -> nil
132 end
133 end)
134 |> Enum.reject(&is_nil/1)
135 |> Map.new(fn pack -> {pack.name, validate_pack(pack)} end)
136
137 {:ok, packs}
138 end
139 end
140
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)}
146 end
147 end
148
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()
152
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
162 save_pack(pack)
163 else
164 {:ok, pack}
165 end
166 end
167 end
168
169 @spec save_metadata(map(), t()) :: {:ok, t()} | {:error, File.posix()}
170 def save_metadata(metadata, %__MODULE__{} = pack) do
171 pack
172 |> Map.put(:pack, metadata)
173 |> save_pack()
174 end
175
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)
181 else
182 save_metadata(data, pack)
183 end
184 end
185 end
186
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"])
190
191 if File.exists?(pack_file) do
192 pack =
193 pack_file
194 |> File.read!()
195 |> from_json()
196 |> Map.put(:pack_file, pack_file)
197 |> Map.put(:path, Path.dirname(pack_file))
198 |> Map.put(:name, name)
199
200 {:ok, pack}
201 else
202 {:error, :not_found}
203 end
204 end
205
206 @spec emoji_path() :: Path.t()
207 defp emoji_path do
208 [:instance, :static_dir]
209 |> Pleroma.Config.get!()
210 |> Path.join("emoji")
211 end
212
213 defp validate_emoji_not_exists(shortcode, force \\ false)
214 defp validate_emoji_not_exists(_shortcode, true), do: :ok
215
216 defp validate_emoji_not_exists(shortcode, _) do
217 case Emoji.get(shortcode) do
218 nil -> :ok
219 _ -> {:error, :already_exists}
220 end
221 end
222
223 defp write_pack_contents(path) do
224 pack = %__MODULE__{
225 files: files_from_path(path),
226 path: path,
227 pack_file: Path.join(path, "pack.json")
228 }
229
230 case save_pack(pack) do
231 {:ok, _pack} -> Path.basename(path)
232 _ -> nil
233 end
234 end
235
236 defp files_from_path(path) do
237 txt_path = Path.join(path, "emoji.txt")
238
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
242
243 # FIXME: Copy-pasted from Pleroma.Emoji/load_from_file_stream/2
244
245 # Create a map of shortcodes to filenames from emoji.txt
246 txt_path
247 |> File.read!()
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
254 [name, file | _] ->
255 file_dir_name = Path.dirname(file)
256
257 if String.ends_with?(path, file_dir_name) do
258 {name, Path.basename(file)}
259 else
260 {name, file}
261 end
262
263 _ ->
264 nil
265 end
266 end)
267 |> Enum.reject(&is_nil/1)
268 |> Map.new()
269 else
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)
274 end
275 end
276
277 defp validate_pack(pack) do
278 info =
279 if downloadable?(pack) do
280 archive = fetch_archive(pack)
281 archive_sha = :crypto.hash(:sha256, archive) |> Base.encode16()
282
283 pack.pack
284 |> Map.put("can-download", true)
285 |> Map.put("download-sha256", archive_sha)
286 else
287 Map.put(pack.pack, "can-download", false)
288 end
289
290 Map.put(pack, :pack, info)
291 end
292
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))
300 end)
301 end
302
303 defp create_archive_and_cache(pack, hash) do
304 files = ['pack.json' | Enum.map(pack.files, fn {_, file} -> to_charlist(file) end)]
305
306 {:ok, {_, result}} =
307 :zip.zip('#{pack.name}.zip', files, [:memory, cwd: to_charlist(pack.path)])
308
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))
311
312 Cachex.put!(
313 :emoji_packs_cache,
314 pack.name,
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
318 ttl: overall_ttl
319 )
320
321 result
322 end
323
324 defp save_pack(pack) do
325 with {:ok, json} <- Jason.encode(pack, pretty: true),
326 :ok <- File.write(pack.pack_file, json) do
327 {:ok, pack}
328 end
329 end
330
331 defp from_json(json) do
332 map = Jason.decode!(json)
333
334 struct(__MODULE__, %{files: map["files"], pack: map["pack"]})
335 end
336
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
343 :ok
344 else
345 {:error, :not_shareable}
346 end
347 end
348 end
349
350 defp validate_not_empty(list) do
351 if Enum.all?(list, fn i -> is_binary(i) and i != "" end) do
352 :ok
353 else
354 {:error, :empty_values}
355 end
356 end
357
358 defp save_file(file, pack, filename) do
359 file_path = Path.join(pack.path, filename)
360 create_subdirs(file_path)
361
362 case file do
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
366
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)
371 end
372 end
373
374 defp put_emoji(pack, shortcode, filename) do
375 files = Map.put(pack.files, shortcode, filename)
376 %{pack | files: files}
377 end
378
379 defp delete_emoji(pack, shortcode) do
380 files = Map.delete(pack.files, shortcode)
381 %{pack | files: files}
382 end
383
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)
388
389 with :ok <- File.rename(old_path, new_path) do
390 remove_dir_if_empty(old_path, filename)
391 end
392 end
393
394 defp create_subdirs(file_path) do
395 if String.contains?(file_path, "/") do
396 file_path
397 |> Path.dirname()
398 |> File.mkdir_p!()
399 end
400 end
401
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)
407 end
408 end
409
410 defp remove_dir_if_empty(emoji, filename) do
411 dir = Path.dirname(emoji)
412
413 if String.contains?(filename, "/") and File.ls!(dir) == [] do
414 File.rmdir!(dir)
415 else
416 :ok
417 end
418 end
419
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
423 {:ok, filename}
424 else
425 _ -> {:error, :doesnt_exist}
426 end
427 end
428
429 defp http_get(%URI{} = url), do: url |> to_string() |> http_get()
430
431 defp http_get(url) do
432 with {:ok, %{body: body}} <- url |> Pleroma.HTTP.get() do
433 Jason.decode(body)
434 end
435 end
436
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
443 {:ok, results}
444 else
445 {:create_dir, {:error, e}} -> {:error, :create_dir, e}
446 {:ls, {:error, e}} -> {:error, :ls, e}
447 end
448 end
449
450 defp validate_downloadable(pack) do
451 if downloadable?(pack), do: :ok, else: {:error, :cant_download}
452 end
453
454 defp copy_as(remote_pack, local_name) do
455 path = Path.join(emoji_path(), local_name)
456
457 %__MODULE__{
458 name: local_name,
459 path: path,
460 files: remote_pack["files"],
461 pack_file: Path.join(path, "pack.json")
462 }
463 end
464
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]
470
471 :zip.unzip(archive, cwd: to_charlist(local_pack.path), file_list: files)
472 end
473 end
474
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} ->
478 {:ok,
479 %{
480 sha: sha,
481 url: URI.merge(uri, "/api/pleroma/emoji/packs/#{name}/archive") |> to_string()
482 }}
483
484 %{"fallback-src" => src, "fallback-src-sha256" => sha} when is_binary(src) ->
485 {:ok,
486 %{
487 sha: sha,
488 url: src,
489 fallback: true
490 }}
491
492 _ ->
493 {:error, "The pack was not set as shared and there is no fallback src to download from"}
494 end
495 end
496
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
500 {:ok, archive}
501 else
502 {:error, :invalid_checksum}
503 end
504 end
505 end
506
507 defp fetch_archive(pack) do
508 hash = :crypto.hash(:md5, File.read!(pack.pack_file))
509
510 case Cachex.get!(:emoji_packs_cache, pack.name) do
511 %{hash: ^hash, pack_data: archive} -> archive
512 _ -> create_archive_and_cache(pack, hash)
513 end
514 end
515
516 defp fallback_sha_changed?(pack, data) do
517 is_binary(data[:"fallback-src"]) and data[:"fallback-src"] != pack.pack["fallback-src"]
518 end
519
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()
524
525 data
526 |> Map.put("fallback-src-sha256", fallback_sha)
527 |> save_metadata(pack)
528 end
529 end
530
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
534 pack.files
535 |> Enum.all?(fn {_, from_manifest} ->
536 List.keyfind(f_list, to_charlist(from_manifest), 0)
537 end)
538 |> if(do: :ok, else: {:error, :incomplete})
539 end
540 end
541 end