akkoma/lib/pleroma/web/pleroma_api/controllers/emoji_pack_controller.ex

231 lines
6.3 KiB
Elixir
Raw Normal View History

2020-10-12 11:00:50 -06:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
2020-10-12 11:00:50 -06:00
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.PleromaAPI.EmojiPackController do
use Pleroma.Web, :controller
2020-03-28 04:34:32 -06:00
alias Pleroma.Emoji.Pack
2019-08-12 04:13:01 -06:00
plug(Pleroma.Web.ApiSpec.CastAndValidate)
plug(
2020-06-24 00:57:27 -06:00
Pleroma.Web.Plugs.OAuthScopesPlug,
%{scopes: ["admin:write"]}
when action in [
2020-04-30 08:50:57 -06:00
:import_from_filesystem,
:remote,
:download,
:create,
:update,
:delete
]
)
plug(:skip_auth when action in [:index, :archive, :show])
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaEmojiPackOperation
2020-06-27 05:33:49 -06:00
def remote(conn, params) do
with {:ok, packs} <-
Pack.list_remote(url: params.url, page_size: params.page_size, page: params.page) do
2020-03-28 04:34:32 -06:00
json(conn, packs)
else
2020-05-18 09:43:23 -06:00
{:error, :not_shareable} ->
2020-03-28 04:34:32 -06:00
conn
|> put_status(:internal_server_error)
|> json(%{error: "The requested instance does not support sharing emoji packs"})
end
end
2020-06-18 05:32:21 -06:00
def index(conn, params) do
2020-03-28 04:34:32 -06:00
emoji_path =
[:instance, :static_dir]
|> Pleroma.Config.get!()
|> Path.join("emoji")
2020-03-28 04:34:32 -06:00
with {:ok, packs, count} <- Pack.list_local(page: params.page, page_size: params.page_size) do
json(conn, %{packs: packs, count: count})
else
2020-05-18 09:43:23 -06:00
{:error, :create_dir, e} ->
conn
|> put_status(:internal_server_error)
2020-03-28 04:34:32 -06:00
|> json(%{error: "Failed to create the emoji pack directory at #{emoji_path}: #{e}"})
2020-05-18 09:43:23 -06:00
{:error, :ls, e} ->
conn
|> put_status(:internal_server_error)
|> json(%{
2020-03-28 04:34:32 -06:00
error: "Failed to get the contents of the emoji pack directory at #{emoji_path}: #{e}"
})
2019-09-11 09:48:51 -06:00
end
end
2020-06-18 09:50:03 -06:00
def show(conn, %{name: name, page: page, page_size: page_size}) do
2020-03-28 04:34:32 -06:00
name = String.trim(name)
2019-09-11 09:48:51 -06:00
2020-06-18 09:50:03 -06:00
with {:ok, pack} <- Pack.show(name: name, page: page, page_size: page_size) do
2020-03-28 04:34:32 -06:00
json(conn, pack)
2019-09-11 09:48:51 -06:00
else
{:error, :enoent} ->
2020-03-28 04:34:32 -06:00
conn
|> put_status(:not_found)
|> json(%{error: "Pack #{name} does not exist"})
2019-08-12 04:13:01 -06:00
2020-03-28 04:34:32 -06:00
{:error, :empty_values} ->
conn
|> put_status(:bad_request)
|> json(%{error: "pack name cannot be empty"})
{:error, error} ->
error_message =
add_posix_error(
"Failed to get the contents of the `#{name}` pack.",
error
)
conn
|> put_status(:internal_server_error)
|> json(%{error: error_message})
end
end
def archive(conn, %{name: name}) do
with {:ok, archive} <- Pack.get_archive(name) do
2020-03-28 04:34:32 -06:00
send_download(conn, {:binary, archive}, filename: "#{name}.zip")
else
2020-05-18 09:43:23 -06:00
{:error, :cant_download} ->
conn
|> put_status(:forbidden)
2019-09-11 10:39:47 -06:00
|> json(%{
2020-03-28 04:34:32 -06:00
error:
"Pack #{name} cannot be downloaded from this instance, either pack sharing was disabled for this pack or some files are missing"
2019-09-11 10:39:47 -06:00
})
{:error, :enoent} ->
conn
|> put_status(:not_found)
2019-09-11 10:39:47 -06:00
|> json(%{error: "Pack #{name} does not exist"})
end
end
def download(%{body_params: %{url: url, name: name} = params} = conn, _) do
2020-05-18 09:43:23 -06:00
with {:ok, _pack} <- Pack.download(name, url, params[:as]) do
2020-03-28 04:34:32 -06:00
json(conn, "ok")
2019-09-11 09:59:31 -06:00
else
2020-05-18 09:43:23 -06:00
{:error, :not_shareable} ->
2020-03-28 04:34:32 -06:00
conn
|> put_status(:internal_server_error)
|> json(%{error: "The requested instance does not support sharing emoji packs"})
2020-05-27 13:34:37 -06:00
{:error, :invalid_checksum} ->
2020-03-28 04:34:32 -06:00
conn
|> put_status(:internal_server_error)
|> json(%{error: "SHA256 for the pack doesn't match the one sent by the server"})
{:error, error} ->
2020-03-28 04:34:32 -06:00
conn
|> put_status(:internal_server_error)
|> json(%{error: error})
end
end
def create(conn, %{name: name}) do
2020-03-28 04:34:32 -06:00
name = String.trim(name)
2020-05-18 09:43:23 -06:00
with {:ok, _pack} <- Pack.create(name) do
2020-03-28 04:34:32 -06:00
json(conn, "ok")
else
{:error, :eexist} ->
conn
|> put_status(:conflict)
|> json(%{error: "A pack named \"#{name}\" already exists"})
2020-03-28 04:34:32 -06:00
{:error, :empty_values} ->
conn
|> put_status(:bad_request)
|> json(%{error: "pack name cannot be empty"})
{:error, error} ->
error_message =
add_posix_error(
"Unexpected error occurred while creating pack.",
error
)
conn
|> put_status(:internal_server_error)
|> json(%{error: error_message})
end
end
def delete(conn, %{name: name}) do
2020-03-28 04:34:32 -06:00
name = String.trim(name)
with {:ok, deleted} when deleted != [] <- Pack.delete(name) do
json(conn, "ok")
else
{:ok, []} ->
conn
|> put_status(:not_found)
|> json(%{error: "Pack #{name} does not exist"})
2020-03-28 04:34:32 -06:00
{:error, :empty_values} ->
conn
|> put_status(:bad_request)
|> json(%{error: "pack name cannot be empty"})
{:error, error, _} ->
2020-11-17 23:58:51 -07:00
error_message = add_posix_error("Couldn't delete the `#{name}` pack", error)
2019-09-11 10:39:47 -06:00
conn
|> put_status(:internal_server_error)
|> json(%{error: error_message})
end
end
def update(%{body_params: %{metadata: metadata}} = conn, %{name: name}) do
with {:ok, pack} <- Pack.update_metadata(name, metadata) do
2020-03-28 04:34:32 -06:00
json(conn, pack.pack)
else
2020-05-18 09:43:23 -06:00
{:error, :incomplete} ->
conn
|> put_status(:bad_request)
2019-09-11 10:39:47 -06:00
|> json(%{error: "The fallback archive does not have all files specified in pack.json"})
2020-02-06 08:01:12 -07:00
{:error, error} ->
error_message =
add_posix_error(
"Unexpected error occurred while updating pack metadata.",
error
)
conn
|> put_status(:internal_server_error)
|> json(%{error: error_message})
2019-09-11 10:39:47 -06:00
end
end
def import_from_filesystem(conn, _params) do
2020-03-28 04:34:32 -06:00
with {:ok, names} <- Pack.import_from_filesystem() do
json(conn, names)
else
2020-03-30 00:09:27 -06:00
{:error, :no_read_write} ->
2020-01-29 03:51:17 -07:00
conn
|> put_status(:internal_server_error)
2020-01-30 07:09:41 -07:00
|> json(%{error: "Error: emoji pack directory must be writable"})
2020-01-29 03:51:17 -07:00
{:error, _} ->
conn
|> put_status(:internal_server_error)
2019-09-11 10:39:47 -06:00
|> json(%{error: "Error accessing emoji pack directory"})
end
end
defp add_posix_error(msg, error) do
[msg, Pleroma.Utils.posix_error_message(error)]
|> Enum.join(" ")
|> String.trim()
end
end