Swap TOML for YAML to get YAML generation for packs from fallbacks
[akkoma] / lib / pleroma / web / emoji_api / emoji_api_controller.ex
1 defmodule Pleroma.Web.EmojiAPI.EmojiAPIController do
2 use Pleroma.Web, :controller
3
4 def reload(conn, _params) do
5 Pleroma.Emoji.reload()
6
7 conn |> json("ok")
8 end
9
10 @emoji_dir_path Path.join(
11 Pleroma.Config.get!([:instance, :static_dir]),
12 "emoji"
13 )
14
15 def list_packs(conn, _params) do
16 pack_infos =
17 case File.ls(@emoji_dir_path) do
18 {:error, _} ->
19 %{}
20
21 {:ok, results} ->
22 results
23 |> Enum.filter(fn file ->
24 dir_path = Path.join(@emoji_dir_path, file)
25 # Filter to only use the pack.yml packs
26 File.dir?(dir_path) and File.exists?(Path.join(dir_path, "pack.yml"))
27 end)
28 |> Enum.map(fn pack_name ->
29 pack_path = Path.join(@emoji_dir_path, pack_name)
30 pack_file = Path.join(pack_path, "pack.yml")
31
32 {pack_name, RelaxYaml.Decoder.read_from_file(pack_file)}
33 end)
34 # Transform into a map of pack-name => pack-data
35 # Check if all the files are in place and can be sent
36 |> Enum.map(fn {name, pack} ->
37 pack_path = Path.join(@emoji_dir_path, name)
38
39 archive_for_sha = make_archive(name, pack, pack_path)
40 archive_sha = :crypto.hash(:sha256, archive_for_sha) |> Base.encode16()
41
42 {name,
43 pack
44 |> put_in(["pack", "can-download"], can_download?(pack, pack_path))
45 |> put_in(["pack", "download-sha256"], archive_sha)}
46 end)
47 |> Enum.into(%{})
48 end
49
50 conn |> json(pack_infos)
51 end
52
53 defp can_download?(pack, pack_path) do
54 # If the pack is set as shared, check if it can be downloaded
55 # That means that when asked, the pack can be packed and sent to the remote
56 # Otherwise, they'd have to download it from external-src
57 pack["pack"]["share-files"] and
58 Enum.all?(pack["files"], fn {_, path} ->
59 File.exists?(Path.join(pack_path, path))
60 end)
61 end
62
63 defp make_archive(name, pack, pack_dir) do
64 files =
65 ['pack.yml'] ++
66 (pack["files"] |> Enum.map(fn {_, path} -> to_charlist(path) end))
67
68 {:ok, {_, zip_result}} = :zip.zip('#{name}.zip', files, [:memory, cwd: to_charlist(pack_dir)])
69
70 zip_result
71 end
72
73 def download_shared(conn, %{"name" => name}) do
74 pack_dir = Path.join(@emoji_dir_path, name)
75 pack_yaml = Path.join(pack_dir, "pack.yml")
76
77 if File.exists?(pack_yaml) do
78 pack = RelaxYaml.Decoder.read_from_file(pack_yaml)
79
80 if can_download?(pack, pack_dir) do
81 zip_result = make_archive(name, pack, pack_dir)
82
83 conn
84 |> send_download({:binary, zip_result}, filename: "#{name}.zip")
85 else
86 {:error,
87 conn
88 |> put_status(:forbidden)
89 |> json("Pack #{name} cannot be downloaded from this instance, either pack sharing\
90 was disabled for this pack or some files are missing")}
91 end
92 else
93 {:error,
94 conn
95 |> put_status(:not_found)
96 |> json("Pack #{name} does not exist")}
97 end
98 end
99
100 def download_from(conn, %{"instance_address" => address, "pack_name" => name} = data) do
101 list_uri = "#{address}/api/pleroma/emoji/packs/list"
102
103 list = Tesla.get!(list_uri).body |> Jason.decode!()
104 full_pack = list[name]
105 pfiles = full_pack["files"]
106 pack = full_pack["pack"]
107
108 pack_info_res =
109 cond do
110 pack["share-files"] && pack["can-download"] ->
111 {:ok,
112 %{
113 sha: pack["download-sha256"],
114 uri: "#{address}/api/pleroma/emoji/packs/download_shared/#{name}"
115 }}
116
117 pack["fallback-src"] ->
118 {:ok,
119 %{
120 sha: pack["fallback-src-sha256"],
121 uri: pack["fallback-src"],
122 fallback: true
123 }}
124
125 true ->
126 {:error, "The pack was not set as shared and the is no fallback url to download from"}
127 end
128
129 case pack_info_res do
130 {:ok, %{sha: sha, uri: uri} = pinfo} ->
131 sha = Base.decode16!(sha)
132 emoji_archive = Tesla.get!(uri).body
133
134 got_sha = :crypto.hash(:sha256, emoji_archive)
135
136 if got_sha == sha do
137 local_name = data["as"] || name
138 pack_dir = Path.join(@emoji_dir_path, local_name)
139 File.mkdir_p!(pack_dir)
140
141 files =
142 ['pack.yml'] ++
143 (pfiles |> Enum.map(fn {_, path} -> to_charlist(path) end))
144
145 {:ok, _} = :zip.unzip(emoji_archive, cwd: to_charlist(pack_dir), file_list: files)
146
147 # Fallback URL might not contain a pack.yml file. Put on we have if there's none
148 if pinfo[:fallback] do
149 yaml_path = Path.join(pack_dir, "pack.yml")
150
151 unless File.exists?(yaml_path) do
152 File.write!(yaml_path, RelaxYaml.Encoder.encode(full_pack, []))
153 end
154 end
155
156 conn |> text("ok")
157 else
158 conn
159 |> put_status(:internal_server_error)
160 |> text("SHA256 for the pack doesn't match the one sent by the server")
161 end
162
163 {:error, e} ->
164 conn |> put_status(:internal_server_error) |> text(e)
165 end
166 end
167 end