From e8bf060e6e55396e7a0dbb53dacbf470d8f56beb Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 8 Jan 2021 17:24:19 -0600 Subject: [PATCH] Move construction of S3 base URL with optional namespace and bucket to Upload.base_url/0 Now we should have a correct base URL for S3 hosted objects throughout the codebase. --- lib/pleroma/upload.ex | 23 +++++++++++++++++++++++ lib/pleroma/uploaders/s3.ex | 16 ---------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index 3061b2aed..a52b698bf 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -245,6 +245,29 @@ def base_url do Pleroma.Web.base_url() <> "/media/" end + Pleroma.Uploaders.S3 -> + bucket = Config.get([Pleroma.Uploaders.S3, :bucket]) + + bucket_with_namespace = + cond do + truncated_namespace = Config.get([Pleroma.Uploaders.S3, :truncated_namespace]) -> + truncated_namespace + + namespace = Config.get([Pleroma.Uploaders.S3, :bucket_namespace]) -> + namespace <> ":" <> bucket + + true -> + bucket + end + + cond do + !is_nil(public_endpoint) -> + Path.join([public_endpoint, bucket_with_namespace]) + + true -> + Path.join([upload_base_url, bucket_with_namespace]) + end + _ -> cond do !is_nil(public_endpoint) -> diff --git a/lib/pleroma/uploaders/s3.ex b/lib/pleroma/uploaders/s3.ex index 5a91410d7..29a1c2861 100644 --- a/lib/pleroma/uploaders/s3.ex +++ b/lib/pleroma/uploaders/s3.ex @@ -12,26 +12,10 @@ defmodule Pleroma.Uploaders.S3 do # links with less strict filenames @impl true def get_file(file) do - config = Config.get([__MODULE__]) - bucket = Keyword.fetch!(config, :bucket) - - bucket_with_namespace = - cond do - truncated_namespace = Keyword.get(config, :truncated_namespace) -> - truncated_namespace - - namespace = Keyword.get(config, :bucket_namespace) -> - namespace <> ":" <> bucket - - true -> - bucket - end - {:ok, {:url, Path.join([ Pleroma.Upload.base_url(), - bucket_with_namespace, strict_encode(URI.decode(file)) ])}} end