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, replace_params: false)
14 Pleroma.Web.Plugs.OAuthScopesPlug,
15 %{scopes: ["admin:write"]}
23 defdelegate open_api_operation(action), to: ApiSpec.PleromaEmojiFileOperation
26 %{private: %{open_api_spex: %{body_params: params, params: %{name: pack_name}}}} = conn,
29 filename = params[:filename] || get_filename(params[:file])
30 shortcode = params[:shortcode] || Path.basename(filename, Path.extname(filename))
32 with {:ok, pack} <- Pack.load_pack(pack_name),
33 {:ok, file} <- get_file(params[:file]),
34 {:ok, pack} <- Pack.add_file(pack, shortcode, filename, file) do
35 json(conn, pack.files)
37 {:error, :already_exists} ->
39 |> put_status(:conflict)
40 |> json(%{error: "An emoji with the \"#{shortcode}\" shortcode already exists"})
42 {:error, :empty_values} ->
44 |> put_status(:unprocessable_entity)
45 |> json(%{error: "pack name, shortcode or filename cannot be empty"})
47 {:error, _} = error ->
48 handle_error(conn, error, %{
50 message: "Unexpected error occurred while adding file to pack."
59 body_params: %{shortcode: shortcode} = params,
60 params: %{name: pack_name}
66 new_shortcode = params[:new_shortcode]
67 new_filename = params[:new_filename]
68 force = params[:force]
70 with {:ok, pack} <- Pack.load_pack(pack_name),
71 {:ok, pack} <- Pack.update_file(pack, shortcode, new_shortcode, new_filename, force) do
72 json(conn, pack.files)
74 {:error, :already_exists} ->
76 |> put_status(:conflict)
79 "New shortcode \"#{new_shortcode}\" is already used. If you want to override emoji use 'force' option"
82 {:error, :empty_values} ->
84 |> put_status(:unprocessable_entity)
85 |> json(%{error: "new_shortcode or new_filename cannot be empty"})
87 {:error, _} = error ->
88 handle_error(conn, error, %{
91 message: "Unexpected error occurred while updating."
97 %{private: %{open_api_spex: %{params: %{name: pack_name, shortcode: shortcode}}}} = conn,
100 with {:ok, pack} <- Pack.load_pack(pack_name),
101 {:ok, pack} <- Pack.delete_file(pack, shortcode) do
102 json(conn, pack.files)
104 {:error, :empty_values} ->
106 |> put_status(:unprocessable_entity)
107 |> json(%{error: "pack name or shortcode cannot be empty"})
109 {:error, _} = error ->
110 handle_error(conn, error, %{
111 pack_name: pack_name,
113 message: "Unexpected error occurred while deleting emoji file."
118 defp handle_error(conn, {:error, :doesnt_exist}, %{code: emoji_code}) do
120 |> put_status(:bad_request)
121 |> json(%{error: "Emoji \"#{emoji_code}\" does not exist"})
124 defp handle_error(conn, {:error, :enoent}, %{pack_name: pack_name}) do
126 |> put_status(:not_found)
127 |> json(%{error: "pack \"#{pack_name}\" is not found"})
130 defp handle_error(conn, {:error, error}, opts) do
133 Map.get(opts, :message, "Unexpected error occurred."),
134 Pleroma.Utils.posix_error_message(error)
140 |> put_status(:internal_server_error)
141 |> json(%{error: message})
144 defp get_filename(%Plug.Upload{filename: filename}), do: filename
145 defp get_filename(url) when is_binary(url), do: Path.basename(url)
147 def get_file(%Plug.Upload{} = file), do: {:ok, file}
149 def get_file(url) when is_binary(url) do
150 with {:ok, %Tesla.Env{body: body, status: code, headers: headers}}
151 when code in 200..299 <- Pleroma.HTTP.get(url) do
152 path = Plug.Upload.random_file!("emoji")
155 case List.keyfind(headers, "content-type", 0) do
156 {"content-type", value} -> value
160 File.write(path, body)
164 filename: Path.basename(url),
166 content_type: content_type