1 defmodule Pleroma.Web.PleromaAPI.EmojiPackController do
2 use Pleroma.Web, :controller
4 alias Pleroma.Emoji.Pack
6 plug(Pleroma.Web.ApiSpec.CastAndValidate)
9 Pleroma.Plugs.OAuthScopesPlug,
10 %{scopes: ["write"], admin: true}
12 :import_from_filesystem,
24 @skip_plugs [Pleroma.Plugs.OAuthScopesPlug, Pleroma.Plugs.ExpectPublicOrAuthenticatedCheckPlug]
25 plug(:skip_plug, @skip_plugs when action in [:archive, :show, :list])
27 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaEmojiPackOperation
29 def remote(conn, %{url: url}) do
30 with {:ok, packs} <- Pack.list_remote(url) do
33 {:error, :not_shareable} ->
35 |> put_status(:internal_server_error)
36 |> json(%{error: "The requested instance does not support sharing emoji packs"})
40 def index(conn, _params) do
42 [:instance, :static_dir]
43 |> Pleroma.Config.get!()
46 with {:ok, packs} <- Pack.list_local() do
49 {:error, :create_dir, e} ->
51 |> put_status(:internal_server_error)
52 |> json(%{error: "Failed to create the emoji pack directory at #{emoji_path}: #{e}"})
56 |> put_status(:internal_server_error)
58 error: "Failed to get the contents of the emoji pack directory at #{emoji_path}: #{e}"
63 def show(conn, %{name: name}) do
64 name = String.trim(name)
66 with {:ok, pack} <- Pack.show(name) do
69 {:error, :not_found} ->
71 |> put_status(:not_found)
72 |> json(%{error: "Pack #{name} does not exist"})
74 {:error, :empty_values} ->
76 |> put_status(:bad_request)
77 |> json(%{error: "pack name cannot be empty"})
81 def archive(conn, %{name: name}) do
82 with {:ok, archive} <- Pack.get_archive(name) do
83 send_download(conn, {:binary, archive}, filename: "#{name}.zip")
85 {:error, :cant_download} ->
87 |> put_status(:forbidden)
90 "Pack #{name} cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
93 {:error, :not_found} ->
95 |> put_status(:not_found)
96 |> json(%{error: "Pack #{name} does not exist"})
100 def download(%{body_params: %{url: url, name: name} = params} = conn, _) do
101 with {:ok, _pack} <- Pack.download(name, url, params[:as]) do
104 {:error, :not_shareable} ->
106 |> put_status(:internal_server_error)
107 |> json(%{error: "The requested instance does not support sharing emoji packs"})
109 {:error, :invalid_checksum} ->
111 |> put_status(:internal_server_error)
112 |> json(%{error: "SHA256 for the pack doesn't match the one sent by the server"})
116 |> put_status(:internal_server_error)
121 def create(conn, %{name: name}) do
122 name = String.trim(name)
124 with {:ok, _pack} <- Pack.create(name) do
129 |> put_status(:conflict)
130 |> json(%{error: "A pack named \"#{name}\" already exists"})
132 {:error, :empty_values} ->
134 |> put_status(:bad_request)
135 |> json(%{error: "pack name cannot be empty"})
140 :internal_server_error,
141 "Unexpected error occurred while creating pack."
146 def delete(conn, %{name: name}) do
147 name = String.trim(name)
149 with {:ok, deleted} when deleted != [] <- Pack.delete(name) do
154 |> put_status(:not_found)
155 |> json(%{error: "Pack #{name} does not exist"})
157 {:error, :empty_values} ->
159 |> put_status(:bad_request)
160 |> json(%{error: "pack name cannot be empty"})
164 |> put_status(:internal_server_error)
165 |> json(%{error: "Couldn't delete the pack #{name}"})
169 def update(%{body_params: %{metadata: metadata}} = conn, %{name: name}) do
170 with {:ok, pack} <- Pack.update_metadata(name, metadata) do
171 json(conn, pack.pack)
173 {:error, :incomplete} ->
175 |> put_status(:bad_request)
176 |> json(%{error: "The fallback archive does not have all files specified in pack.json"})
181 :internal_server_error,
182 "Unexpected error occurred while updating pack metadata."
187 def add_file(%{body_params: params} = conn, %{name: name}) do
188 filename = params[:filename] || get_filename(params[:file])
189 shortcode = params[:shortcode] || Path.basename(filename, Path.extname(filename))
191 with {:ok, pack} <- Pack.add_file(name, shortcode, filename, params[:file]) do
192 json(conn, pack.files)
194 {:error, :already_exists} ->
196 |> put_status(:conflict)
197 |> json(%{error: "An emoji with the \"#{shortcode}\" shortcode already exists"})
199 {:error, :not_found} ->
201 |> put_status(:bad_request)
202 |> json(%{error: "pack \"#{name}\" is not found"})
204 {:error, :empty_values} ->
206 |> put_status(:bad_request)
207 |> json(%{error: "pack name, shortcode or filename cannot be empty"})
212 :internal_server_error,
213 "Unexpected error occurred while adding file to pack."
218 def update_file(%{body_params: %{shortcode: shortcode} = params} = conn, %{name: name}) do
219 new_shortcode = params[:new_shortcode]
220 new_filename = params[:new_filename]
221 force = params[:force]
223 with {:ok, pack} <- Pack.update_file(name, shortcode, new_shortcode, new_filename, force) do
224 json(conn, pack.files)
226 {:error, :doesnt_exist} ->
228 |> put_status(:bad_request)
229 |> json(%{error: "Emoji \"#{shortcode}\" does not exist"})
231 {:error, :already_exists} ->
233 |> put_status(:conflict)
236 "New shortcode \"#{new_shortcode}\" is already used. If you want to override emoji use 'force' option"
239 {:error, :not_found} ->
241 |> put_status(:bad_request)
242 |> json(%{error: "pack \"#{name}\" is not found"})
244 {:error, :empty_values} ->
246 |> put_status(:bad_request)
247 |> json(%{error: "new_shortcode or new_filename cannot be empty"})
252 :internal_server_error,
253 "Unexpected error occurred while updating file in pack."
258 def delete_file(conn, %{name: name, shortcode: shortcode}) do
259 with {:ok, pack} <- Pack.delete_file(name, shortcode) do
260 json(conn, pack.files)
262 {:error, :doesnt_exist} ->
264 |> put_status(:bad_request)
265 |> json(%{error: "Emoji \"#{shortcode}\" does not exist"})
267 {:error, :not_found} ->
269 |> put_status(:bad_request)
270 |> json(%{error: "pack \"#{name}\" is not found"})
272 {:error, :empty_values} ->
274 |> put_status(:bad_request)
275 |> json(%{error: "pack name or shortcode cannot be empty"})
280 :internal_server_error,
281 "Unexpected error occurred while removing file from pack."
286 def import_from_filesystem(conn, _params) do
287 with {:ok, names} <- Pack.import_from_filesystem() do
290 {:error, :no_read_write} ->
292 |> put_status(:internal_server_error)
293 |> json(%{error: "Error: emoji pack directory must be writable"})
297 |> put_status(:internal_server_error)
298 |> json(%{error: "Error accessing emoji pack directory"})
302 defp get_filename(%Plug.Upload{filename: filename}), do: filename
303 defp get_filename(url) when is_binary(url), do: Path.basename(url)