1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.EmojiFileController do
6 use Pleroma.Web, :controller
8 alias Pleroma.Emoji.Pack
9 alias Pleroma.Web.ApiSpec
11 plug(Pleroma.Web.ApiSpec.CastAndValidate)
14 Pleroma.Web.Plugs.OAuthScopesPlug,
15 %{scopes: ["admin:write"]}
23 defdelegate open_api_operation(action), to: ApiSpec.PleromaEmojiFileOperation
25 def create(%{body_params: params} = conn, %{name: pack_name}) do
26 filename = params[:filename] || get_filename(params[:file])
27 shortcode = params[:shortcode] || Path.basename(filename, Path.extname(filename))
29 with {:ok, pack} <- Pack.load_pack(pack_name),
30 {:ok, file} <- get_file(params[:file]),
31 {:ok, pack} <- Pack.add_file(pack, shortcode, filename, file) do
32 json(conn, pack.files)
34 {:error, :already_exists} ->
36 |> put_status(:conflict)
37 |> json(%{error: "An emoji with the \"#{shortcode}\" shortcode already exists"})
39 {:error, :empty_values} ->
41 |> put_status(:unprocessable_entity)
42 |> json(%{error: "pack name, shortcode or filename cannot be empty"})
44 {:error, _} = error ->
45 handle_error(conn, error, %{
47 message: "Unexpected error occurred while adding file to pack."
52 def update(%{body_params: %{shortcode: shortcode} = params} = conn, %{name: pack_name}) do
53 new_shortcode = params[:new_shortcode]
54 new_filename = params[:new_filename]
55 force = params[:force]
57 with {:ok, pack} <- Pack.load_pack(pack_name),
58 {:ok, pack} <- Pack.update_file(pack, shortcode, new_shortcode, new_filename, force) do
59 json(conn, pack.files)
61 {:error, :already_exists} ->
63 |> put_status(:conflict)
66 "New shortcode \"#{new_shortcode}\" is already used. If you want to override emoji use 'force' option"
69 {:error, :empty_values} ->
71 |> put_status(:unprocessable_entity)
72 |> json(%{error: "new_shortcode or new_filename cannot be empty"})
74 {:error, _} = error ->
75 handle_error(conn, error, %{
78 message: "Unexpected error occurred while updating."
83 def delete(conn, %{name: pack_name, shortcode: shortcode}) do
84 with {:ok, pack} <- Pack.load_pack(pack_name),
85 {:ok, pack} <- Pack.delete_file(pack, shortcode) do
86 json(conn, pack.files)
88 {:error, :empty_values} ->
90 |> put_status(:unprocessable_entity)
91 |> json(%{error: "pack name or shortcode cannot be empty"})
93 {:error, _} = error ->
94 handle_error(conn, error, %{
97 message: "Unexpected error occurred while deleting emoji file."
102 defp handle_error(conn, {:error, :doesnt_exist}, %{code: emoji_code}) do
104 |> put_status(:bad_request)
105 |> json(%{error: "Emoji \"#{emoji_code}\" does not exist"})
108 defp handle_error(conn, {:error, :enoent}, %{pack_name: pack_name}) do
110 |> put_status(:not_found)
111 |> json(%{error: "pack \"#{pack_name}\" is not found"})
114 defp handle_error(conn, {:error, error}, opts) do
117 Map.get(opts, :message, "Unexpected error occurred."),
118 Pleroma.Utils.posix_error_message(error)
124 |> put_status(:internal_server_error)
125 |> json(%{error: message})
128 defp get_filename(%Plug.Upload{filename: filename}), do: filename
129 defp get_filename(url) when is_binary(url), do: Path.basename(url)
131 def get_file(%Plug.Upload{} = file), do: {:ok, file}
133 def get_file(url) when is_binary(url) do
134 with {:ok, %Tesla.Env{body: body, status: code, headers: headers}}
135 when code in 200..299 <- Pleroma.HTTP.get(url) do
136 path = Plug.Upload.random_file!("emoji")
139 case List.keyfind(headers, "content-type", 0) do
140 {"content-type", value} -> value
144 File.write(path, body)
148 filename: Path.basename(url),
150 content_type: content_type