logo

pleroma

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

media_controller.ex (3094B)


  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. conn
  47. |> put_status(202)
  48. |> render("attachment.json", %{attachment: attachment_data})
  49. end
  50. end
  51. def create2(_conn, _data), do: {:error, :bad_request}
  52. @doc "PUT /api/v1/media/:id"
  53. def update(
  54. %{
  55. assigns: %{user: user},
  56. private: %{
  57. open_api_spex: %{body_params: %{description: description}, params: %{id: id}}
  58. }
  59. } = conn,
  60. _
  61. ) do
  62. with %Object{} = object <- Object.get_by_id(id),
  63. :ok <- Object.authorize_access(object, user),
  64. {:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do
  65. attachment_data = Map.put(data, "id", object.id)
  66. render(conn, "attachment.json", %{attachment: attachment_data})
  67. end
  68. end
  69. def update(conn, data), do: show(conn, data)
  70. @doc "GET /api/v1/media/:id"
  71. def show(%{assigns: %{user: user}, private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
  72. with %Object{data: data, id: object_id} = object <- Object.get_by_id(id),
  73. :ok <- Object.authorize_access(object, user) do
  74. attachment_data = Map.put(data, "id", object_id)
  75. render(conn, "attachment.json", %{attachment: attachment_data})
  76. end
  77. end
  78. def show(_conn, _data), do: {:error, :bad_request}
  79. end