Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 line
2.7KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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(Pleroma.Web.ApiSpec.CastAndValidate)
  12. plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
  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(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do
  18. with {:ok, object} <-
  19. ActivityPub.upload(
  20. file,
  21. actor: User.ap_id(user),
  22. description: Map.get(data, :description)
  23. ) do
  24. attachment_data = Map.put(object.data, "id", object.id)
  25. render(conn, "attachment.json", %{attachment: attachment_data})
  26. end
  27. end
  28. def create(_conn, _data), do: {:error, :bad_request}
  29. @doc "POST /api/v2/media"
  30. def create2(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do
  31. with {:ok, object} <-
  32. ActivityPub.upload(
  33. file,
  34. actor: User.ap_id(user),
  35. description: Map.get(data, :description)
  36. ) do
  37. attachment_data = Map.put(object.data, "id", object.id)
  38. conn
  39. |> put_status(202)
  40. |> render("attachment.json", %{attachment: attachment_data})
  41. end
  42. end
  43. def create2(_conn, _data), do: {:error, :bad_request}
  44. @doc "PUT /api/v1/media/:id"
  45. def update(%{assigns: %{user: user}, body_params: %{description: description}} = conn, %{id: id}) do
  46. with %Object{} = object <- Object.get_by_id(id),
  47. :ok <- Object.authorize_access(object, user),
  48. {:ok, %Object{data: data}} <- Object.update_data(object, %{"name" => description}) do
  49. attachment_data = Map.put(data, "id", object.id)
  50. render(conn, "attachment.json", %{attachment: attachment_data})
  51. end
  52. end
  53. def update(conn, data), do: show(conn, data)
  54. @doc "GET /api/v1/media/:id"
  55. def show(%{assigns: %{user: user}} = conn, %{id: id}) do
  56. with %Object{data: data, id: object_id} = object <- Object.get_by_id(id),
  57. :ok <- Object.authorize_access(object, user) do
  58. attachment_data = Map.put(data, "id", object_id)
  59. render(conn, "attachment.json", %{attachment: attachment_data})
  60. end
  61. end
  62. def show(_conn, _data), do: {:error, :bad_request}
  63. end