2020-10-12 11:00:50 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-10-12 11:00:50 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-08-18 21:50:20 -06:00
|
|
|
defmodule Pleroma.Web.PleromaAPI.EmojiFileController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
alias Pleroma.Emoji.Pack
|
|
|
|
alias Pleroma.Web.ApiSpec
|
|
|
|
|
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
|
|
|
|
plug(
|
2020-10-13 08:10:34 -06:00
|
|
|
Pleroma.Web.Plugs.OAuthScopesPlug,
|
2020-08-18 21:50:20 -06:00
|
|
|
%{scopes: ["write"], admin: true}
|
|
|
|
when action in [
|
|
|
|
:create,
|
|
|
|
:update,
|
|
|
|
:delete
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
defdelegate open_api_operation(action), to: ApiSpec.PleromaEmojiFileOperation
|
|
|
|
|
|
|
|
def create(%{body_params: params} = conn, %{name: pack_name}) do
|
|
|
|
filename = params[:filename] || get_filename(params[:file])
|
|
|
|
shortcode = params[:shortcode] || Path.basename(filename, Path.extname(filename))
|
|
|
|
|
2020-08-22 01:42:02 -06:00
|
|
|
with {:ok, pack} <- Pack.load_pack(pack_name),
|
|
|
|
{:ok, file} <- get_file(params[:file]),
|
|
|
|
{:ok, pack} <- Pack.add_file(pack, shortcode, filename, file) do
|
2020-08-18 21:50:20 -06:00
|
|
|
json(conn, pack.files)
|
|
|
|
else
|
|
|
|
{:error, :already_exists} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:conflict)
|
|
|
|
|> json(%{error: "An emoji with the \"#{shortcode}\" shortcode already exists"})
|
|
|
|
|
|
|
|
{:error, :empty_values} ->
|
|
|
|
conn
|
2020-08-22 01:42:02 -06:00
|
|
|
|> put_status(:unprocessable_entity)
|
2020-08-18 21:50:20 -06:00
|
|
|
|> json(%{error: "pack name, shortcode or filename cannot be empty"})
|
|
|
|
|
2020-08-22 01:56:26 -06:00
|
|
|
{:error, _} = error ->
|
2020-11-13 05:13:14 -07:00
|
|
|
handle_error(conn, error, %{
|
|
|
|
pack_name: pack_name,
|
|
|
|
message: "Unexpected error occurred while adding file to pack."
|
|
|
|
})
|
2020-08-18 21:50:20 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(%{body_params: %{shortcode: shortcode} = params} = conn, %{name: pack_name}) do
|
|
|
|
new_shortcode = params[:new_shortcode]
|
|
|
|
new_filename = params[:new_filename]
|
|
|
|
force = params[:force]
|
|
|
|
|
2020-08-22 01:42:02 -06:00
|
|
|
with {:ok, pack} <- Pack.load_pack(pack_name),
|
|
|
|
{:ok, pack} <- Pack.update_file(pack, shortcode, new_shortcode, new_filename, force) do
|
2020-08-18 21:50:20 -06:00
|
|
|
json(conn, pack.files)
|
|
|
|
else
|
|
|
|
{:error, :already_exists} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:conflict)
|
|
|
|
|> json(%{
|
|
|
|
error:
|
|
|
|
"New shortcode \"#{new_shortcode}\" is already used. If you want to override emoji use 'force' option"
|
|
|
|
})
|
|
|
|
|
|
|
|
{:error, :empty_values} ->
|
|
|
|
conn
|
2020-08-22 01:56:26 -06:00
|
|
|
|> put_status(:unprocessable_entity)
|
2020-08-18 21:50:20 -06:00
|
|
|
|> json(%{error: "new_shortcode or new_filename cannot be empty"})
|
|
|
|
|
2020-08-22 01:56:26 -06:00
|
|
|
{:error, _} = error ->
|
2020-11-13 05:13:14 -07:00
|
|
|
handle_error(conn, error, %{
|
|
|
|
pack_name: pack_name,
|
|
|
|
code: shortcode,
|
|
|
|
message: "Unexpected error occurred while updating."
|
|
|
|
})
|
2020-08-18 21:50:20 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, %{name: pack_name, shortcode: shortcode}) do
|
2020-08-22 01:42:02 -06:00
|
|
|
with {:ok, pack} <- Pack.load_pack(pack_name),
|
|
|
|
{:ok, pack} <- Pack.delete_file(pack, shortcode) do
|
2020-08-18 21:50:20 -06:00
|
|
|
json(conn, pack.files)
|
|
|
|
else
|
|
|
|
{:error, :empty_values} ->
|
|
|
|
conn
|
2020-08-22 01:56:26 -06:00
|
|
|
|> put_status(:unprocessable_entity)
|
2020-08-18 21:50:20 -06:00
|
|
|
|> json(%{error: "pack name or shortcode cannot be empty"})
|
|
|
|
|
2020-08-22 01:56:26 -06:00
|
|
|
{:error, _} = error ->
|
2020-11-13 05:13:14 -07:00
|
|
|
handle_error(conn, error, %{
|
|
|
|
pack_name: pack_name,
|
|
|
|
code: shortcode,
|
|
|
|
message: "Unexpected error occurred while deleting emoji file."
|
|
|
|
})
|
2020-08-18 21:50:20 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-22 01:56:26 -06:00
|
|
|
defp handle_error(conn, {:error, :doesnt_exist}, %{code: emoji_code}) do
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{error: "Emoji \"#{emoji_code}\" does not exist"})
|
|
|
|
end
|
|
|
|
|
2020-11-13 05:13:14 -07:00
|
|
|
defp handle_error(conn, {:error, :enoent}, %{pack_name: pack_name}) do
|
2020-08-22 01:56:26 -06:00
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
|
|
|
|> json(%{error: "pack \"#{pack_name}\" is not found"})
|
|
|
|
end
|
|
|
|
|
2020-11-13 05:13:14 -07:00
|
|
|
defp handle_error(conn, {:error, error}, opts) do
|
|
|
|
message =
|
|
|
|
[
|
|
|
|
Map.get(opts, :message, "Unexpected error occurred."),
|
|
|
|
Pleroma.Utils.posix_error_message(error)
|
|
|
|
]
|
|
|
|
|> Enum.join(" ")
|
|
|
|
|> String.trim()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(:internal_server_error)
|
|
|
|
|> json(%{error: message})
|
2020-08-22 01:56:26 -06:00
|
|
|
end
|
|
|
|
|
2020-08-18 21:50:20 -06:00
|
|
|
defp get_filename(%Plug.Upload{filename: filename}), do: filename
|
|
|
|
defp get_filename(url) when is_binary(url), do: Path.basename(url)
|
2020-08-22 01:42:02 -06:00
|
|
|
|
|
|
|
def get_file(%Plug.Upload{} = file), do: {:ok, file}
|
|
|
|
|
|
|
|
def get_file(url) when is_binary(url) do
|
|
|
|
with {:ok, %Tesla.Env{body: body, status: code, headers: headers}}
|
|
|
|
when code in 200..299 <- Pleroma.HTTP.get(url) do
|
|
|
|
path = Plug.Upload.random_file!("emoji")
|
|
|
|
|
|
|
|
content_type =
|
|
|
|
case List.keyfind(headers, "content-type", 0) do
|
|
|
|
{"content-type", value} -> value
|
|
|
|
nil -> nil
|
|
|
|
end
|
|
|
|
|
|
|
|
File.write(path, body)
|
|
|
|
|
|
|
|
{:ok,
|
|
|
|
%Plug.Upload{
|
|
|
|
filename: Path.basename(url),
|
|
|
|
path: path,
|
|
|
|
content_type: content_type
|
|
|
|
}}
|
|
|
|
end
|
|
|
|
end
|
2020-08-18 21:50:20 -06:00
|
|
|
end
|