logo

pleroma

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

media_controller.ex (3061B)


  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.MastodonAPI.MediaController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.Object
  7. alias Pleroma.User
  8. alias Pleroma.Web.ActivityPub.ActivityPub
  9. alias Pleroma.Web.Plugs.OAuthScopesPlug
  10. action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
  11. plug(Majic.Plug, [pool: Pleroma.MajicPool] when action in [:create, :create2])
  12. plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
  13. plug(OAuthScopesPlug, %{scopes: ["read:media"]} when action == :show)
  14. plug(OAuthScopesPlug, %{scopes: ["write:media"]} when action != :show)
  15. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.MediaOperation
  16. @doc "POST /api/v1/media"
  17. def create(
  18. %{assigns: %{user: user}, private: %{open_api_spex: %{body_params: %{file: file} = data}}} =
  19. conn,
  20. _
  21. ) do
  22. with {:ok, object} <-
  23. ActivityPub.upload(
  24. file,
  25. actor: User.ap_id(user),
  26. description: Map.get(data, :description)
  27. ) do
  28. attachment_data = Map.put(object.data, "id", object.id)
  29. render(conn, "attachment.json", %{attachment: attachment_data})
  30. end
  31. end
  32. def create(_conn, _data), do: {:error, :bad_request}
  33. @doc "POST /api/v2/media"
  34. def create2(
  35. %{assigns: %{user: user}, private: %{open_api_spex: %{body_params: %{file: file} = data}}} =
  36. conn,
  37. _
  38. ) do
  39. with {:ok, object} <-
  40. ActivityPub.upload(
  41. file,
  42. actor: User.ap_id(user),
  43. description: Map.get(data, :description)
  44. ) do
  45. attachment_data = Map.put(object.data, "id", object.id)
  46. render(conn, "attachment.json", %{attachment: attachment_data})
  47. end
  48. end
  49. def create2(_conn, _data), do: {:error, :bad_request}
  50. @doc "PUT /api/v1/media/:id"
  51. def update(
  52. %{
  53. assigns: %{user: user},
  54. private: %{
  55. open_api_spex: %{body_params: %{description: description}, params: %{id: id}}
  56. }
  57. } = conn,
  58. _
  59. ) do
  60. with %Object{} = object <- Object.get_by_id(id),
  61. :ok <- Object.authorize_access(object, user),
  62. {:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do
  63. attachment_data = Map.put(data, "id", object.id)
  64. render(conn, "attachment.json", %{attachment: attachment_data})
  65. end
  66. end
  67. def update(conn, data), do: show(conn, data)
  68. @doc "GET /api/v1/media/:id"
  69. def show(%{assigns: %{user: user}, private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
  70. with %Object{data: data, id: object_id} = object <- Object.get_by_id(id),
  71. :ok <- Object.authorize_access(object, user) do
  72. attachment_data = Map.put(data, "id", object_id)
  73. render(conn, "attachment.json", %{attachment: attachment_data})
  74. end
  75. end
  76. def show(_conn, _data), do: {:error, :bad_request}
  77. end