1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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: ["write"], admin: true}
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, %{pack_name: pack_name})
49 def update(%{body_params: %{shortcode: shortcode} = params} = conn, %{name: pack_name}) do
50 new_shortcode = params[:new_shortcode]
51 new_filename = params[:new_filename]
52 force = params[:force]
54 with {:ok, pack} <- Pack.load_pack(pack_name),
55 {:ok, pack} <- Pack.update_file(pack, shortcode, new_shortcode, new_filename, force) do
56 json(conn, pack.files)
58 {:error, :already_exists} ->
60 |> put_status(:conflict)
63 "New shortcode \"#{new_shortcode}\" is already used. If you want to override emoji use 'force' option"
66 {:error, :empty_values} ->
68 |> put_status(:unprocessable_entity)
69 |> json(%{error: "new_shortcode or new_filename cannot be empty"})
71 {:error, _} = error ->
72 handle_error(conn, error, %{pack_name: pack_name, code: shortcode})
76 def delete(conn, %{name: pack_name, shortcode: shortcode}) do
77 with {:ok, pack} <- Pack.load_pack(pack_name),
78 {:ok, pack} <- Pack.delete_file(pack, shortcode) do
79 json(conn, pack.files)
81 {:error, :empty_values} ->
83 |> put_status(:unprocessable_entity)
84 |> json(%{error: "pack name or shortcode cannot be empty"})
86 {:error, _} = error ->
87 handle_error(conn, error, %{pack_name: pack_name, code: shortcode})
91 defp handle_error(conn, {:error, :doesnt_exist}, %{code: emoji_code}) do
93 |> put_status(:bad_request)
94 |> json(%{error: "Emoji \"#{emoji_code}\" does not exist"})
97 defp handle_error(conn, {:error, :not_found}, %{pack_name: pack_name}) do
99 |> put_status(:not_found)
100 |> json(%{error: "pack \"#{pack_name}\" is not found"})
103 defp handle_error(conn, {:error, _}, _) do
106 :internal_server_error,
107 "Unexpected error occurred while adding file to pack."
111 defp get_filename(%Plug.Upload{filename: filename}), do: filename
112 defp get_filename(url) when is_binary(url), do: Path.basename(url)
114 def get_file(%Plug.Upload{} = file), do: {:ok, file}
116 def get_file(url) when is_binary(url) do
117 with {:ok, %Tesla.Env{body: body, status: code, headers: headers}}
118 when code in 200..299 <- Pleroma.HTTP.get(url) do
119 path = Plug.Upload.random_file!("emoji")
122 case List.keyfind(headers, "content-type", 0) do
123 {"content-type", value} -> value
127 File.write(path, body)
131 filename: Path.basename(url),
133 content_type: content_type