Skip to content

Commit

Permalink
Fix invalid file type error in source URL
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Sep 5, 2024
1 parent 1b33cc7 commit 034d2b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/plug_image_processing/sources/url.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ defmodule PlugImageProcessing.Sources.URL do
_ -> ""
end

{content_type, file_suffix} = get_file_suffix(source, content_type)
case get_file_suffix(source, content_type) do
{:invalid_file_type, type} ->
{:file_type_error, "Invalid file type: #{type}"}

{:ok, body, content_type, file_suffix}
{content_type, file_suffix} ->
{:ok, body, content_type, file_suffix}
end
end
end

Expand Down Expand Up @@ -132,11 +136,11 @@ defmodule PlugImageProcessing.Sources.URL do

{"image/#{content_type}", type <> options}
else
:invalid_file_type
{:invalid_file_type, extension_name}
end

_ ->
:invalid_file_type
{:invalid_file_type, image_type}
end
end

Expand All @@ -163,6 +167,10 @@ defmodule PlugImageProcessing.Sources.URL do
Logger.error("[PlugImageProcessing] - Cached error on #{url}")
{:error, :invalid_file}

{:file_type_error, message} ->
Logger.error("[PlugImageProcessing] - File type error while fetching source URL. Got #{message} on #{source.uri}")
{:error, :invalid_file_type}

{:http_error, status} ->
Logger.error("[PlugImageProcessing] - HTTP error while fetching source URL. Got #{status} on #{source.uri}")
{:error, :invalid_file}
Expand Down
10 changes: 10 additions & 0 deletions test/plug_image_processing/web_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule PlugImageProcessing.WebTest do
def get("http://example.org/valid-xml.svg", _), do: {:ok, @svg_image, [{"Content-type", "image/svg+xml"}]}
def get("http://example.org/retry.jpg", _), do: {:ok, @image, [{"Content-type", "image/jpg"}]}
def get("http://example.org/404.jpg", _), do: {:error, "404 Not found"}
def get("http://example.org/index.html", _), do: {:ok, "<html></html>", [{"Content-type", "text/html"}]}

def get("http://example.org/timeout.jpg", _) do
Process.sleep(1000)
Expand Down Expand Up @@ -49,6 +50,15 @@ defmodule PlugImageProcessing.WebTest do
assert conn.status === 400
end

test "source URL invalid type", %{config: config} do
plug_opts = Web.init(config)
conn = conn(:get, "/imageproxy/resize", %{width: 20, url: "http://example.org/index.html"})
conn = Web.call(conn, plug_opts)

assert conn.resp_body === "Bad request: :invalid_file_type"
assert conn.status === 400
end

test "source URL 404", %{config: config} do
plug_opts = Web.init(config)
conn = conn(:get, "/imageproxy/resize", %{width: 20, url: "http://example.org/404.jpg"})
Expand Down

0 comments on commit 034d2b2

Please sign in to comment.