Merge branch 'refactor/locked_user_field' into 'develop'
[akkoma] / lib / pleroma / web / pleroma_api / controllers / emoji_file_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.EmojiFileController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Emoji.Pack
9 alias Pleroma.Web.ApiSpec
10
11 plug(Pleroma.Web.ApiSpec.CastAndValidate)
12
13 plug(
14 Pleroma.Web.Plugs.OAuthScopesPlug,
15 %{scopes: ["write"], admin: true}
16 when action in [
17 :create,
18 :update,
19 :delete
20 ]
21 )
22
23 defdelegate open_api_operation(action), to: ApiSpec.PleromaEmojiFileOperation
24
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))
28
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)
33 else
34 {:error, :already_exists} ->
35 conn
36 |> put_status(:conflict)
37 |> json(%{error: "An emoji with the \"#{shortcode}\" shortcode already exists"})
38
39 {:error, :empty_values} ->
40 conn
41 |> put_status(:unprocessable_entity)
42 |> json(%{error: "pack name, shortcode or filename cannot be empty"})
43
44 {:error, _} = error ->
45 handle_error(conn, error, %{pack_name: pack_name})
46 end
47 end
48
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]
53
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)
57 else
58 {:error, :already_exists} ->
59 conn
60 |> put_status(:conflict)
61 |> json(%{
62 error:
63 "New shortcode \"#{new_shortcode}\" is already used. If you want to override emoji use 'force' option"
64 })
65
66 {:error, :empty_values} ->
67 conn
68 |> put_status(:unprocessable_entity)
69 |> json(%{error: "new_shortcode or new_filename cannot be empty"})
70
71 {:error, _} = error ->
72 handle_error(conn, error, %{pack_name: pack_name, code: shortcode})
73 end
74 end
75
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)
80 else
81 {:error, :empty_values} ->
82 conn
83 |> put_status(:unprocessable_entity)
84 |> json(%{error: "pack name or shortcode cannot be empty"})
85
86 {:error, _} = error ->
87 handle_error(conn, error, %{pack_name: pack_name, code: shortcode})
88 end
89 end
90
91 defp handle_error(conn, {:error, :doesnt_exist}, %{code: emoji_code}) do
92 conn
93 |> put_status(:bad_request)
94 |> json(%{error: "Emoji \"#{emoji_code}\" does not exist"})
95 end
96
97 defp handle_error(conn, {:error, :not_found}, %{pack_name: pack_name}) do
98 conn
99 |> put_status(:not_found)
100 |> json(%{error: "pack \"#{pack_name}\" is not found"})
101 end
102
103 defp handle_error(conn, {:error, _}, _) do
104 render_error(
105 conn,
106 :internal_server_error,
107 "Unexpected error occurred while adding file to pack."
108 )
109 end
110
111 defp get_filename(%Plug.Upload{filename: filename}), do: filename
112 defp get_filename(url) when is_binary(url), do: Path.basename(url)
113
114 def get_file(%Plug.Upload{} = file), do: {:ok, file}
115
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")
120
121 content_type =
122 case List.keyfind(headers, "content-type", 0) do
123 {"content-type", value} -> value
124 nil -> nil
125 end
126
127 File.write(path, body)
128
129 {:ok,
130 %Plug.Upload{
131 filename: Path.basename(url),
132 path: path,
133 content_type: content_type
134 }}
135 end
136 end
137 end