From fa4492a35a1bba91c379505231d049dc8654f9d5 Mon Sep 17 00:00:00 2001 From: Dirk Schumann Date: Mon, 11 Nov 2024 18:03:45 +0100 Subject: [PATCH 1/3] Spike fix publisher #1527 --- server/lib/publisher/wordpress/media.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/lib/publisher/wordpress/media.ex b/server/lib/publisher/wordpress/media.ex index b66e33e..5e53fe7 100644 --- a/server/lib/publisher/wordpress/media.ex +++ b/server/lib/publisher/wordpress/media.ex @@ -5,7 +5,8 @@ defmodule Publisher.WordPress.Media do ext = extension_from_url(url) filename = [slug, ext] |> Enum.join(".") - {:ok, resp} = Req.get(url) + encoded_url = URI.encode(url) + {:ok, resp} = Req.get(encoded_url) response = if post_id do upload_media(req, post_id, filename, content_type(resp), resp.body) From 40d410e2e3cd8b83781a7222da7cbaa5d8882151 Mon Sep 17 00:00:00 2001 From: Eric Teubert Date: Thu, 14 Nov 2024 21:40:17 +0100 Subject: [PATCH 2/3] more solid encoding --- server/lib/publisher/url_encoder.ex | 38 +++++++++++++++++++++++++ server/lib/publisher/wordpress/media.ex | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 server/lib/publisher/url_encoder.ex diff --git a/server/lib/publisher/url_encoder.ex b/server/lib/publisher/url_encoder.ex new file mode 100644 index 0000000..e099b3c --- /dev/null +++ b/server/lib/publisher/url_encoder.ex @@ -0,0 +1,38 @@ +defmodule URLEncoder do + @doc """ + Encodes a URL if it's not already encoded. + Returns {:ok, encoded_url} if successful, {:error, reason} otherwise. + + ## Examples + + iex> URLEncoder.encode("https://example.com/path with spaces") + {:ok, "https://example.com/path%20with%20spaces"} + + iex> URLEncoder.encode("https://example.com/path%20already%20encoded") + {:ok, "https://example.com/path%20already%20encoded"} + + """ + def maybe_encode(url) when is_binary(url) do + cond do + already_encoded?(url) -> + {:ok, url} + + valid_url?(url) -> + {:ok, URI.encode(url)} + + true -> + {:error, "Invalid URL format"} + end + end + + def maybe_encode(_), do: {:error, "URL must be a string"} + + defp already_encoded?(url) do + String.match?(url, ~r/%[0-9A-Fa-f]{2}/) + end + + defp valid_url?(url) do + uri = URI.parse(url) + !is_nil(uri.scheme) && !is_nil(uri.host) + end +end diff --git a/server/lib/publisher/wordpress/media.ex b/server/lib/publisher/wordpress/media.ex index 5e53fe7..344de71 100644 --- a/server/lib/publisher/wordpress/media.ex +++ b/server/lib/publisher/wordpress/media.ex @@ -5,7 +5,7 @@ defmodule Publisher.WordPress.Media do ext = extension_from_url(url) filename = [slug, ext] |> Enum.join(".") - encoded_url = URI.encode(url) + {:ok, encoded_url} = URLEncoder.maybe_encode(url) {:ok, resp} = Req.get(encoded_url) response = if post_id do From fe2737d8faa20f2e03bbfcb26a6d0da1fdabe5c1 Mon Sep 17 00:00:00 2001 From: Eric Teubert Date: Thu, 14 Nov 2024 21:40:24 +0100 Subject: [PATCH 3/3] chore: code format --- server/lib/publisher/wordpress/media.ex | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/lib/publisher/wordpress/media.ex b/server/lib/publisher/wordpress/media.ex index 344de71..5d5d5df 100644 --- a/server/lib/publisher/wordpress/media.ex +++ b/server/lib/publisher/wordpress/media.ex @@ -8,11 +8,12 @@ defmodule Publisher.WordPress.Media do {:ok, encoded_url} = URLEncoder.maybe_encode(url) {:ok, resp} = Req.get(encoded_url) - response = if post_id do - upload_media(req, post_id, filename, content_type(resp), resp.body) - else - upload_media(req, filename, content_type(resp), resp.body) - end + response = + if post_id do + upload_media(req, post_id, filename, content_type(resp), resp.body) + else + upload_media(req, filename, content_type(resp), resp.body) + end with {:ok, response} <- response, {:ok, _} <- extract_status(response), @@ -33,7 +34,6 @@ defmodule Publisher.WordPress.Media do else error -> error end - end defp content_type(resp) do