Add port if specified.
authorRoger Braun <roger@rogerbraun.net>
Thu, 30 Mar 2017 12:32:36 +0000 (14:32 +0200)
committerRoger Braun <roger@rogerbraun.net>
Thu, 30 Mar 2017 12:32:36 +0000 (14:32 +0200)
lib/pleroma/upload.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/web.ex

index fdda2634e84f90294722cb3c11a46812729d1cf7..158656e92ea117674aadabe89fd149aeeb9a02e0 100644 (file)
@@ -20,13 +20,6 @@ defmodule Pleroma.Upload do
   end
 
   defp url_for(file) do
-    host =
-      Application.get_env(:pleroma, Pleroma.Web.Endpoint)
-      |> Keyword.fetch!(:url)
-      |> Keyword.fetch!(:host)
-
-    protocol = Application.get_env(:pleroma, Pleroma.Web.Endpoint) |> Keyword.fetch!(:protocol)
-
-    "#{protocol}://#{host}/media/#{file}"
+    "#{Pleroma.Web.base_url()}/media/#{file}"
   end
 end
index f7ba1bb37e50959b09b20e42f92934ebfd96353e..6c8250de86f08923318cae7a15cf77f7952bfa30 100644 (file)
@@ -29,13 +29,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   end
 
   def generate_id(type) do
-    host =
-      Application.get_env(:pleroma, Pleroma.Web.Endpoint)
-      |> Keyword.fetch!(:url)
-      |> Keyword.fetch!(:host)
-
-    protocol = Application.get_env(:pleroma, Pleroma.Web.Endpoint) |> Keyword.fetch!(:protocol)
-    "#{protocol}://#{host}/#{type}/#{Ecto.UUID.generate}"
+    "#{Pleroma.Web.base_url()}/#{type}/#{Ecto.UUID.generate}"
   end
 
   def fetch_public_activities(opts \\ %{}) do
index f392af2ca17e016933814720ebaeaebdfc72c6a3..d03db22313df1bf78d6bd3f0ac81b39d60081dbd 100644 (file)
@@ -60,4 +60,23 @@ defmodule Pleroma.Web do
   defmacro __using__(which) when is_atom(which) do
     apply(__MODULE__, which, [])
   end
+
+  def base_url do
+    settings = Application.get_env(:pleroma, Pleroma.Web.Endpoint)
+    host =
+      settings
+      |> Keyword.fetch!(:url)
+      |> Keyword.fetch!(:host)
+
+    protocol = settings |> Keyword.fetch!(:protocol)
+
+    port_fragment = with {:ok, protocol_info} <- settings |> Keyword.fetch(String.to_atom(protocol)),
+                         {:ok, port} <- protocol_info |> Keyword.fetch(:port)
+    do
+      ":#{port}"
+    else _e ->
+      ""
+    end
+    "#{protocol}://#{host}#{port_fragment}"
+  end
 end