2020-11-26 11:41:13 +01:00
|
|
|
defmodule Mobilizon.Federation.ActivityStream.Converter.Media do
|
|
|
|
@moduledoc """
|
|
|
|
Media converter.
|
|
|
|
|
|
|
|
This module allows to convert events from ActivityStream format to our own
|
|
|
|
internal one, and back.
|
|
|
|
"""
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
alias Mobilizon.Federation.ActivityStream
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.Medias
|
|
|
|
alias Mobilizon.Medias.Media, as: MediaModel
|
2024-06-30 13:18:10 +02:00
|
|
|
alias Mobilizon.Service.HTTP.RemoteMediaDownloaderClient
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.Web.Upload
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Convert a media struct to an ActivityStream representation.
|
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec model_to_as(MediaModel.t()) :: ActivityStream.t()
|
2020-11-26 11:41:13 +01:00
|
|
|
def model_to_as(%MediaModel{file: file}) do
|
|
|
|
%{
|
|
|
|
"type" => "Document",
|
|
|
|
"mediaType" => file.content_type,
|
|
|
|
"url" => file.url,
|
|
|
|
"name" => file.name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Save media data from raw data and return AS Link data.
|
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec find_or_create_media(map(), String.t() | integer()) ::
|
|
|
|
{:ok, MediaModel.t()} | {:error, atom() | String.t() | Ecto.Changeset.t()}
|
2020-11-26 11:41:13 +01:00
|
|
|
def find_or_create_media(%{"type" => "Link", "href" => url}, actor_id),
|
2021-09-24 16:46:42 +02:00
|
|
|
do:
|
|
|
|
find_or_create_media(
|
|
|
|
%{"type" => "Document", "url" => url, "name" => "External media"},
|
|
|
|
actor_id
|
|
|
|
)
|
2020-11-26 11:41:13 +01:00
|
|
|
|
|
|
|
def find_or_create_media(
|
|
|
|
%{"type" => "Document", "url" => media_url, "name" => name},
|
|
|
|
actor_id
|
|
|
|
)
|
2021-04-08 16:41:49 +02:00
|
|
|
when is_binary(media_url) do
|
2022-05-03 12:23:09 +02:00
|
|
|
with {:ok, %{url: url} = uploaded} <- upload_media(media_url, name) do
|
|
|
|
case Medias.get_media_by_url(url) do
|
|
|
|
%MediaModel{file: _file} = media ->
|
|
|
|
{:ok, media}
|
2021-09-24 16:46:42 +02:00
|
|
|
|
2022-05-03 12:23:09 +02:00
|
|
|
nil ->
|
|
|
|
Medias.create_media(%{
|
|
|
|
file: Map.take(uploaded, [:url, :name, :content_type, :size]),
|
|
|
|
metadata: Map.take(uploaded, [:width, :height, :blurhash]),
|
|
|
|
actor_id: actor_id
|
|
|
|
})
|
|
|
|
end
|
2021-09-24 16:46:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec upload_media(String.t(), String.t()) :: {:ok, map()} | {:error, atom() | String.t()}
|
2021-11-10 16:39:22 +01:00
|
|
|
defp upload_media(media_url, ""), do: upload_media(media_url, "unknown")
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
defp upload_media(media_url, name) do
|
2024-06-30 13:18:10 +02:00
|
|
|
case RemoteMediaDownloaderClient.get(media_url) do
|
2021-09-24 16:46:42 +02:00
|
|
|
{:ok, %{body: body}} ->
|
|
|
|
case Upload.store(%{body: body, name: name}) do
|
|
|
|
{:ok, %{url: _url} = uploaded} ->
|
|
|
|
{:ok, uploaded}
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2020-11-26 11:41:13 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|