From f407831120318dcd8fa5b31f39137cdb1ee87519 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 29 Oct 2018 17:59:24 +0000 Subject: [PATCH 1/2] common api: prefer formatting attachments using the attachment's name instead of URI --- lib/pleroma/web/common_api/utils.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index ed1fe1ad9..2a5a2cc15 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -89,8 +89,8 @@ def maybe_add_attachments(text, attachments, _no_links) do def add_attachments(text, attachments) do attachment_text = Enum.map(attachments, fn - %{"url" => [%{"href" => href} | _]} -> - name = URI.decode(Path.basename(href)) + %{"url" => [%{"href" => href} | _]} = attachment -> + name = attachment["name"] || URI.decode(Path.basename(href)) href = MediaProxy.url(href) "#{shortname(name)}" From 36825932eb04d9db3e2d24b02368d7dd709dea23 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 29 Oct 2018 18:00:59 +0000 Subject: [PATCH 2/2] s3 uploader: add new feature to force public attachment URIs to go through media proxy --- config/config.exs | 3 ++- lib/pleroma/uploaders/s3.ex | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/config/config.exs b/config/config.exs index 2d2cdda45..a71fedf1c 100644 --- a/config/config.exs +++ b/config/config.exs @@ -20,7 +20,8 @@ config :pleroma, Pleroma.Uploaders.S3, bucket: nil, - public_endpoint: "https://s3.amazonaws.com" + public_endpoint: "https://s3.amazonaws.com", + force_media_proxy: false config :pleroma, :emoji, shortcode_globs: ["/emoji/custom/**/*.png"] diff --git a/lib/pleroma/uploaders/s3.ex b/lib/pleroma/uploaders/s3.ex index 87322753d..40a836460 100644 --- a/lib/pleroma/uploaders/s3.ex +++ b/lib/pleroma/uploaders/s3.ex @@ -1,10 +1,13 @@ defmodule Pleroma.Uploaders.S3 do + alias Pleroma.Web.MediaProxy + @behaviour Pleroma.Uploaders.Uploader def put_file(name, uuid, path, content_type, _should_dedupe) do settings = Application.get_env(:pleroma, Pleroma.Uploaders.S3) bucket = Keyword.fetch!(settings, :bucket) public_endpoint = Keyword.fetch!(settings, :public_endpoint) + force_media_proxy = Keyword.fetch!(settings, :force_media_proxy) {:ok, file_data} = File.read(path) @@ -19,7 +22,16 @@ def put_file(name, uuid, path, content_type, _should_dedupe) do ]) |> ExAws.request() - {:ok, "#{public_endpoint}/#{bucket}/#{s3_name}"} + url_base = "#{public_endpoint}/#{bucket}/#{s3_name}" + + public_url = + if force_media_proxy do + MediaProxy.url(url_base) + else + url_base + end + + {:ok, public_url} end defp encode(name) do