Merge branch 'use-reverse-proxy-for-remote-images' into 'master'

Add proxy route and proxify pics everywhere

See merge request framasoft/mobilizon!142
This commit is contained in:
Thomas Citharel 2019-05-28 11:03:23 +02:00
commit 01f6f8c35e
8 changed files with 65 additions and 17 deletions

View File

@ -8,6 +8,7 @@ defmodule MobilizonWeb.Resolvers.Event do
alias Mobilizon.Media.Picture alias Mobilizon.Media.Picture
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Users.User alias Mobilizon.Users.User
alias MobilizonWeb.Resolvers.Person
# We limit the max number of events that can be retrieved # We limit the max number of events that can be retrieved
@event_max_limit 100 @event_max_limit 100
@ -28,7 +29,7 @@ defmodule MobilizonWeb.Resolvers.Event do
{:error, "Event with UUID #{uuid} not found"} {:error, "Event with UUID #{uuid} not found"}
event -> event ->
{:ok, event} {:ok, Map.put(event, :organizer_actor, Person.proxify_pictures(event.organizer_actor))}
end end
end end
@ -116,7 +117,7 @@ defmodule MobilizonWeb.Resolvers.Event do
}), }),
participant <- participant <-
Map.put(participant, :event, event) Map.put(participant, :event, event)
|> Map.put(:actor, actor) do |> Map.put(:actor, Person.proxify_pictures(actor)) do
{:ok, participant} {:ok, participant}
else else
{:is_owned, false} -> {:is_owned, false} ->

View File

@ -7,16 +7,17 @@ defmodule MobilizonWeb.Resolvers.Group do
alias Mobilizon.Users.User alias Mobilizon.Users.User
alias Mobilizon.Service.ActivityPub alias Mobilizon.Service.ActivityPub
alias Mobilizon.Activity alias Mobilizon.Activity
alias MobilizonWeb.Resolvers.Person
require Logger require Logger
@doc """ @doc """
Find a group Find a group
""" """
def find_group(_parent, %{preferred_username: name}, _resolution) do def find_group(_parent, %{preferred_username: name}, _resolution) do
case ActivityPub.find_or_make_group_from_nickname(name) do with {:ok, actor} <- ActivityPub.find_or_make_group_from_nickname(name),
{:ok, actor} -> actor <- Person.proxify_pictures(actor) do
{:ok, actor} {:ok, actor}
else
_ -> _ ->
{:error, "Group with name #{name} not found"} {:error, "Group with name #{name} not found"}
end end
@ -26,7 +27,8 @@ defmodule MobilizonWeb.Resolvers.Group do
Lists all groups Lists all groups
""" """
def list_groups(_parent, %{page: page, limit: limit}, _resolution) do def list_groups(_parent, %{page: page, limit: limit}, _resolution) do
{:ok, Actors.list_groups(page, limit)} {:ok,
Actors.list_groups(page, limit) |> Enum.map(fn actor -> Person.proxify_pictures(actor) end)}
end end
@doc """ @doc """
@ -141,7 +143,12 @@ defmodule MobilizonWeb.Resolvers.Group do
actor_id: actor.id, actor_id: actor.id,
role: role role: role
}) do }) do
{:ok, %{parent: group, actor: actor, role: role}} {:ok,
%{
parent: group |> Person.proxify_pictures(),
actor: actor |> Person.proxify_pictures(),
role: role
}}
else else
{:is_owned, false} -> {:is_owned, false} ->
{:error, "Actor id is not owned by authenticated user"} {:error, "Actor id is not owned by authenticated user"}

View File

@ -13,10 +13,10 @@ defmodule MobilizonWeb.Resolvers.Person do
Find a person Find a person
""" """
def find_person(_parent, %{preferred_username: name}, _resolution) do def find_person(_parent, %{preferred_username: name}, _resolution) do
case ActivityPub.find_or_make_person_from_nickname(name) do with {:ok, actor} <- ActivityPub.find_or_make_person_from_nickname(name),
{:ok, actor} -> actor <- proxify_pictures(actor) do
{:ok, actor} {:ok, actor}
else
_ -> _ ->
{:error, "Person with name #{name} not found"} {:error, "Person with name #{name} not found"}
end end
@ -135,4 +135,26 @@ defmodule MobilizonWeb.Resolvers.Person do
{:error, "Actor id is not owned by authenticated user"} {:error, "Actor id is not owned by authenticated user"}
end end
end end
def proxify_pictures(%Actor{} = actor) do
actor
|> proxify_avatar
|> proxify_banner
end
@spec proxify_avatar(Actor.t()) :: Actor.t()
defp proxify_avatar(%Actor{avatar: %{url: avatar_url} = avatar} = actor) do
actor |> Map.put(:avatar, avatar |> Map.put(:url, MobilizonWeb.MediaProxy.url(avatar_url)))
end
@spec proxify_avatar(Actor.t()) :: Actor.t()
defp proxify_avatar(%Actor{} = actor), do: actor
@spec proxify_banner(Actor.t()) :: Actor.t()
defp proxify_banner(%Actor{banner: %{url: banner_url} = banner} = actor) do
actor |> Map.put(:banner, banner |> Map.put(:url, MobilizonWeb.MediaProxy.url(banner_url)))
end
@spec proxify_banner(Actor.t()) :: Actor.t()
defp proxify_banner(%Actor{} = actor), do: actor
end end

View File

@ -39,6 +39,9 @@ defmodule MobilizonWeb.Router do
plug(:put_secure_browser_headers) plug(:put_secure_browser_headers)
end end
pipeline :remote_media do
end
scope "/api" do scope "/api" do
pipe_through(:graphql) pipe_through(:graphql)
@ -94,6 +97,13 @@ defmodule MobilizonWeb.Router do
post("/inbox", ActivityPubController, :inbox) post("/inbox", ActivityPubController, :inbox)
end end
scope "/proxy/", MobilizonWeb do
pipe_through(:remote_media)
get("/:sig/:url", MediaProxyController, :remote)
get("/:sig/:url/:filename", MediaProxyController, :remote)
end
if Mix.env() == :dev do if Mix.env() == :dev do
# If using Phoenix # If using Phoenix
forward("/sent_emails", Bamboo.SentEmailViewerPlug) forward("/sent_emails", Bamboo.SentEmailViewerPlug)

View File

@ -5,6 +5,7 @@ defmodule MobilizonWeb.JsonLD.ObjectView do
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Addresses.Address alias Mobilizon.Addresses.Address
alias MobilizonWeb.JsonLD.ObjectView alias MobilizonWeb.JsonLD.ObjectView
alias MobilizonWeb.MediaProxy
def render("event.json", %{event: %Event{} = event}) do def render("event.json", %{event: %Event{} = event}) do
# TODO: event.description is actually markdown! # TODO: event.description is actually markdown!
@ -25,7 +26,7 @@ defmodule MobilizonWeb.JsonLD.ObjectView do
json_ld = json_ld =
if event.picture do if event.picture do
Map.put(json_ld, "image", [ Map.put(json_ld, "image", [
event.picture.file.url event.picture.file.url |> MediaProxy.url()
]) ])
else else
json_ld json_ld

View File

@ -13,6 +13,7 @@ defmodule Mobilizon.Service.Export.Feed do
import MobilizonWeb.Gettext import MobilizonWeb.Gettext
alias MobilizonWeb.Router.Helpers, as: Routes alias MobilizonWeb.Router.Helpers, as: Routes
alias MobilizonWeb.Endpoint alias MobilizonWeb.Endpoint
alias MobilizonWeb.MediaProxy
require Logger require Logger
@version Mix.Project.config()[:version] @version Mix.Project.config()[:version]
@ -76,14 +77,14 @@ defmodule Mobilizon.Service.Export.Feed do
feed = feed =
if actor.avatar do if actor.avatar do
Feed.icon(feed, actor.avatar.url) feed |> Feed.icon(actor.avatar.url |> MediaProxy.url())
else else
feed feed
end end
feed = feed =
if actor.banner do if actor.banner do
Feed.logo(feed, actor.banner.url) feed |> Feed.logo(actor.banner.url |> MediaProxy.url())
else else
feed feed
end end

View File

@ -1,6 +1,7 @@
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Actors.Actor do defimpl Mobilizon.Service.Metadata, for: Mobilizon.Actors.Actor do
alias Phoenix.HTML.Tag alias Phoenix.HTML.Tag
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias MobilizonWeb.MediaProxy
def build_tags(%Actor{} = actor) do def build_tags(%Actor{} = actor) do
tags = [ tags = [
@ -15,7 +16,8 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Actors.Actor do
if is_nil(actor.avatar) do if is_nil(actor.avatar) do
tags tags
else else
tags ++ [Tag.tag(:meta, property: "og:image", content: actor.avatar.url)] tags ++
[Tag.tag(:meta, property: "og:image", content: actor.avatar.url |> MediaProxy.url())]
end end
end end
end end

View File

@ -3,6 +3,7 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
alias Phoenix.HTML.Tag alias Phoenix.HTML.Tag
alias Mobilizon.Events.Event alias Mobilizon.Events.Event
alias MobilizonWeb.JsonLD.ObjectView alias MobilizonWeb.JsonLD.ObjectView
alias MobilizonWeb.MediaProxy
def build_tags(%Event{} = event) do def build_tags(%Event{} = event) do
tags = [ tags = [
@ -18,7 +19,10 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
else else
tags ++ tags ++
[ [
Tag.tag(:meta, property: "og:image", content: event.picture.file.url) Tag.tag(:meta,
property: "og:image",
content: event.picture.file.url |> MediaProxy.url()
)
] ]
end end