Move construction of S3 base URL with optional namespace and bucket to Upload.base_url/0
authorMark Felder <feld@feld.me>
Fri, 8 Jan 2021 23:24:19 +0000 (17:24 -0600)
committerMark Felder <feld@feld.me>
Fri, 8 Jan 2021 23:32:42 +0000 (17:32 -0600)
Now we should have a correct base URL for S3 hosted objects throughout the codebase.

lib/pleroma/upload.ex
lib/pleroma/uploaders/s3.ex

index 3061b2aed7fae26597419a2381d01f7d62702cc5..a52b698bf65a21f98bcb3466ffa43a35e91dcae4 100644 (file)
@@ -245,6 +245,29 @@ defmodule Pleroma.Upload 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) ->
index 5a91410d7c51909e5cd1feb54d4610c1669e125a..29a1c286121e848f158b9127afe1f9422ebf1337 100644 (file)
@@ -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