From 65e7307d6853064d9c09c80f1aca0c487001f9f4 Mon Sep 17 00:00:00 2001 From: href Date: Fri, 30 Nov 2018 17:44:12 +0100 Subject: [PATCH] Upload: bring back base_url --- lib/pleroma/upload.ex | 15 +++++++++++---- test/media_proxy_test.exs | 17 +++++++++++++++++ test/upload_test.exs | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index a298ab488..bf2c60102 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -5,6 +5,7 @@ defmodule Pleroma.Upload do Options: * `:type`: presets for activity type (defaults to Document) and size limits from app configuration * `:description`: upload alternative text + * `:base_url`: override base url * `:uploader`: override uploader * `:filters`: override filters * `:size_limit`: override size limit @@ -64,7 +65,7 @@ def store(upload, opts \\ []) do %{ "type" => "Link", "mediaType" => upload.content_type, - "href" => url_from_spec(url_spec) + "href" => url_from_spec(opts.base_url, url_spec) } ], "name" => Map.get(opts, :description) || upload.name @@ -100,7 +101,13 @@ defp get_opts(opts) do size_limit: Keyword.get(opts, :size_limit, size_limit), uploader: Keyword.get(opts, :uploader, Pleroma.Config.get([__MODULE__, :uploader])), filters: Keyword.get(opts, :filters, Pleroma.Config.get([__MODULE__, :filters])), - description: Keyword.get(opts, :description) + description: Keyword.get(opts, :description), + base_url: + Keyword.get( + opts, + :base_url, + Pleroma.Config.get([__MODULE__, :base_url], Pleroma.Web.base_url()) + ) } # TODO: 1.0+ : remove old config compatibility @@ -204,8 +211,8 @@ defp tempfile_for_image(data) do tmp_path end - defp url_from_spec({:file, path}) do - [Pleroma.Web.base_url(), "media", path] + defp url_from_spec(base_url, {:file, path}) do + [base_url, "media", path] |> Path.join() end diff --git a/test/media_proxy_test.exs b/test/media_proxy_test.exs index c69ed7ea4..d71f9f13a 100644 --- a/test/media_proxy_test.exs +++ b/test/media_proxy_test.exs @@ -82,6 +82,23 @@ test "validates signature" do [_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/") assert decode_url(sig, base64) == {:error, :invalid_signature} end + + test "uses the configured base_url" do + base_url = Pleroma.Config.get([:media_proxy, :base_url]) + + if base_url do + on_exit(fn -> + Pleroma.Config.put([:media_proxy, :base_url], base_url) + end) + end + + Pleroma.Config.put([:media_proxy, :base_url], "https://cache.pleroma.social") + + url = "https://pleroma.soykaf.com/static/logo.png" + encoded = url(url) + + assert String.starts_with?(encoded, Pleroma.Config.get([:media_proxy, :base_url])) + end end describe "when disabled" do diff --git a/test/upload_test.exs b/test/upload_test.exs index 7117373bd..cfd86ddd0 100644 --- a/test/upload_test.exs +++ b/test/upload_test.exs @@ -36,6 +36,24 @@ test "returns a media url" do assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/") end + test "returns a media url with configured base_url" do + base_url = "https://cache.pleroma.social" + + File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg") + + file = %Plug.Upload{ + content_type: "image/jpg", + path: Path.absname("test/fixtures/image_tmp.jpg"), + filename: "image.jpg" + } + + {:ok, data} = Upload.store(file, base_url: base_url) + + assert %{"url" => [%{"href" => url}]} = data + + assert String.starts_with?(url, base_url <> "/media/") + end + test "copies the file to the configured folder with deduping" do File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")