logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

emoji_file_controller.ex (4949B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.PleromaAPI.EmojiFileController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.Emoji.Pack
  7. alias Pleroma.Web.ApiSpec
  8. plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
  9. plug(
  10. Pleroma.Web.Plugs.OAuthScopesPlug,
  11. %{scopes: ["admin:write"]}
  12. when action in [
  13. :create,
  14. :update,
  15. :delete
  16. ]
  17. )
  18. defdelegate open_api_operation(action), to: ApiSpec.PleromaEmojiFileOperation
  19. def create(
  20. %{private: %{open_api_spex: %{body_params: params, params: %{name: pack_name}}}} = conn,
  21. _
  22. ) do
  23. filename = params[:filename] || get_filename(params[:file])
  24. shortcode = params[:shortcode] || Path.basename(filename, Path.extname(filename))
  25. with {:ok, pack} <- Pack.load_pack(pack_name),
  26. {:ok, file} <- get_file(params[:file]),
  27. {:ok, pack} <- Pack.add_file(pack, shortcode, filename, file) do
  28. json(conn, pack.files)
  29. else
  30. {:error, :already_exists} ->
  31. conn
  32. |> put_status(:conflict)
  33. |> json(%{error: "An emoji with the \"#{shortcode}\" shortcode already exists"})
  34. {:error, :empty_values} ->
  35. conn
  36. |> put_status(:unprocessable_entity)
  37. |> json(%{error: "pack name, shortcode or filename cannot be empty"})
  38. {:error, _} = error ->
  39. handle_error(conn, error, %{
  40. pack_name: pack_name,
  41. message: "Unexpected error occurred while adding file to pack."
  42. })
  43. end
  44. end
  45. def update(
  46. %{
  47. private: %{
  48. open_api_spex: %{
  49. body_params: %{shortcode: shortcode} = params,
  50. params: %{name: pack_name}
  51. }
  52. }
  53. } = conn,
  54. _
  55. ) do
  56. new_shortcode = params[:new_shortcode]
  57. new_filename = params[:new_filename]
  58. force = params[:force]
  59. with {:ok, pack} <- Pack.load_pack(pack_name),
  60. {:ok, pack} <- Pack.update_file(pack, shortcode, new_shortcode, new_filename, force) do
  61. json(conn, pack.files)
  62. else
  63. {:error, :already_exists} ->
  64. conn
  65. |> put_status(:conflict)
  66. |> json(%{
  67. error:
  68. "New shortcode \"#{new_shortcode}\" is already used. If you want to override emoji use 'force' option"
  69. })
  70. {:error, :empty_values} ->
  71. conn
  72. |> put_status(:unprocessable_entity)
  73. |> json(%{error: "new_shortcode or new_filename cannot be empty"})
  74. {:error, _} = error ->
  75. handle_error(conn, error, %{
  76. pack_name: pack_name,
  77. code: shortcode,
  78. message: "Unexpected error occurred while updating."
  79. })
  80. end
  81. end
  82. def delete(
  83. %{private: %{open_api_spex: %{params: %{name: pack_name, shortcode: shortcode}}}} = conn,
  84. _
  85. ) do
  86. with {:ok, pack} <- Pack.load_pack(pack_name),
  87. {:ok, pack} <- Pack.delete_file(pack, shortcode) do
  88. json(conn, pack.files)
  89. else
  90. {:error, :empty_values} ->
  91. conn
  92. |> put_status(:unprocessable_entity)
  93. |> json(%{error: "pack name or shortcode cannot be empty"})
  94. {:error, _} = error ->
  95. handle_error(conn, error, %{
  96. pack_name: pack_name,
  97. code: shortcode,
  98. message: "Unexpected error occurred while deleting emoji file."
  99. })
  100. end
  101. end
  102. defp handle_error(conn, {:error, :doesnt_exist}, %{code: emoji_code}) do
  103. conn
  104. |> put_status(:bad_request)
  105. |> json(%{error: "Emoji \"#{emoji_code}\" does not exist"})
  106. end
  107. defp handle_error(conn, {:error, :enoent}, %{pack_name: pack_name}) do
  108. conn
  109. |> put_status(:not_found)
  110. |> json(%{error: "pack \"#{pack_name}\" is not found"})
  111. end
  112. defp handle_error(conn, {:error, error}, opts) do
  113. message =
  114. [
  115. Map.get(opts, :message, "Unexpected error occurred."),
  116. Pleroma.Utils.posix_error_message(error)
  117. ]
  118. |> Enum.join(" ")
  119. |> String.trim()
  120. conn
  121. |> put_status(:internal_server_error)
  122. |> json(%{error: message})
  123. end
  124. defp get_filename(%Plug.Upload{filename: filename}), do: filename
  125. defp get_filename(url) when is_binary(url), do: Path.basename(url)
  126. def get_file(%Plug.Upload{} = file), do: {:ok, file}
  127. def get_file(url) when is_binary(url) do
  128. with {:ok, %Tesla.Env{body: body, status: code, headers: headers}}
  129. when code in 200..299 <- Pleroma.HTTP.get(url) do
  130. path = Plug.Upload.random_file!("emoji")
  131. content_type =
  132. case List.keyfind(headers, "content-type", 0) do
  133. {"content-type", value} -> value
  134. nil -> nil
  135. end
  136. File.write(path, body)
  137. {:ok,
  138. %Plug.Upload{
  139. filename: Path.basename(url),
  140. path: path,
  141. content_type: content_type
  142. }}
  143. end
  144. end
  145. end