Only find SHA256 for packs that are shared
[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 if can_download?(pack, pack_path) do
40 archive_for_sha = make_archive(name, pack, pack_path)
41 archive_sha = :crypto.hash(:sha256, archive_for_sha) |> Base.encode16()
42
43 {name,
44 pack
45 |> put_in(["pack", "can-download"], true)
46 |> put_in(["pack", "download-sha256"], archive_sha)}
47 else
48 {name,
49 pack
50 |> put_in(["pack", "can-download"], false)}
51 end
52 end)
53 |> Enum.into(%{})
54 end
55
56 conn |> json(pack_infos)
57 end
58
59 defp can_download?(pack, pack_path) do
60 # If the pack is set as shared, check if it can be downloaded
61 # That means that when asked, the pack can be packed and sent to the remote
62 # Otherwise, they'd have to download it from external-src
63 pack["pack"]["share-files"] and
64 Enum.all?(pack["files"], fn {_, path} ->
65 File.exists?(Path.join(pack_path, path))
66 end)
67 end
68
69 defp make_archive(name, pack, pack_dir) do
70 files =
71 ['pack.yml'] ++
72 (pack["files"] |> Enum.map(fn {_, path} -> to_charlist(path) end))
73
74 {:ok, {_, zip_result}} = :zip.zip('#{name}.zip', files, [:memory, cwd: to_charlist(pack_dir)])
75
76 zip_result
77 end
78
79 def download_shared(conn, %{"name" => name}) do
80 pack_dir = Path.join(@emoji_dir_path, name)
81 pack_yaml = Path.join(pack_dir, "pack.yml")
82
83 if File.exists?(pack_yaml) do
84 pack = RelaxYaml.Decoder.read_from_file(pack_yaml)
85
86 if can_download?(pack, pack_dir) do
87 zip_result = make_archive(name, pack, pack_dir)
88
89 conn
90 |> send_download({:binary, zip_result}, filename: "#{name}.zip")
91 else
92 {:error,
93 conn
94 |> put_status(:forbidden)
95 |> json("Pack #{name} cannot be downloaded from this instance, either pack sharing\
96 was disabled for this pack or some files are missing")}
97 end
98 else
99 {:error,
100 conn
101 |> put_status(:not_found)
102 |> json("Pack #{name} does not exist")}
103 end
104 end
105
106 def download_from(conn, %{"instance_address" => address, "pack_name" => name} = data) do
107 list_uri = "#{address}/api/pleroma/emoji/packs/list"
108
109 list = Tesla.get!(list_uri).body |> Jason.decode!()
110 full_pack = list[name]
111 pfiles = full_pack["files"]
112 pack = full_pack["pack"]
113
114 pack_info_res =
115 cond do
116 pack["share-files"] && pack["can-download"] ->
117 {:ok,
118 %{
119 sha: pack["download-sha256"],
120 uri: "#{address}/api/pleroma/emoji/packs/download_shared/#{name}"
121 }}
122
123 pack["fallback-src"] ->
124 {:ok,
125 %{
126 sha: pack["fallback-src-sha256"],
127 uri: pack["fallback-src"],
128 fallback: true
129 }}
130
131 true ->
132 {:error, "The pack was not set as shared and the is no fallback url to download from"}
133 end
134
135 case pack_info_res do
136 {:ok, %{sha: sha, uri: uri} = pinfo} ->
137 sha = Base.decode16!(sha)
138 emoji_archive = Tesla.get!(uri).body
139
140 got_sha = :crypto.hash(:sha256, emoji_archive)
141
142 if got_sha == sha do
143 local_name = data["as"] || name
144 pack_dir = Path.join(@emoji_dir_path, local_name)
145 File.mkdir_p!(pack_dir)
146
147 files =
148 ['pack.yml'] ++
149 (pfiles |> Enum.map(fn {_, path} -> to_charlist(path) end))
150
151 {:ok, _} = :zip.unzip(emoji_archive, cwd: to_charlist(pack_dir), file_list: files)
152
153 # Fallback URL might not contain a pack.yml file. Put on we have if there's none
154 if pinfo[:fallback] do
155 yaml_path = Path.join(pack_dir, "pack.yml")
156
157 unless File.exists?(yaml_path) do
158 File.write!(yaml_path, RelaxYaml.Encoder.encode(full_pack, []))
159 end
160 end
161
162 conn |> text("ok")
163 else
164 conn
165 |> put_status(:internal_server_error)
166 |> text("SHA256 for the pack doesn't match the one sent by the server")
167 end
168
169 {:error, e} ->
170 conn |> put_status(:internal_server_error) |> text(e)
171 end
172 end
173 end