Skip to content

Commit

Permalink
fix: handle valid URL without file path (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
drozdzynski authored Jul 23, 2024
1 parent e7b0e35 commit 32ca2cf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
8 changes: 7 additions & 1 deletion lib/bow/download.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ defmodule Bow.Download do
def download(client \\ %Tesla.Client{}, url) do
case get!(client, encode(url)) do
%{status: 200, url: url, body: body} = env ->
base = url |> URI.parse() |> Map.get(:path) |> Path.basename()
base =
url
|> URI.parse()
|> case do
%{path: path} when not is_nil(path) -> path |> Path.basename()
_ -> ""
end

name =
case Tesla.get_header(env, "content-type") do
Expand Down
35 changes: 27 additions & 8 deletions test/bow/download_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ defmodule Bow.DownloadTest do
headers: [{"content-type", "image/png"}]
}}

%{url: "http://example.com"} ->
{:ok,
%{
env
| status: 200,
body: File.read!(@file_cat),
headers: [{"content-type", "image/png"}]
}}

%{url: "http://example.com/dog.jpg"} ->
{:ok,
%{
Expand All @@ -48,14 +57,14 @@ defmodule Bow.DownloadTest do
headers: [{"content-type", "example/dog/nope"}]
}}

%{url: "http://example.com/.weird-path"} ->
{:ok,
%{
env
| status: 200,
body: File.read!(@file_cat),
headers: [{"content-type", "image/png"}]
}}
%{url: "http://example.com/.weird-path"} ->
{:ok,
%{
env
| status: 200,
body: File.read!(@file_cat),
headers: [{"content-type", "image/png"}]
}}

_ ->
{:ok, %{env | status: 404, body: "NotFound"}}
Expand Down Expand Up @@ -115,6 +124,16 @@ defmodule Bow.DownloadTest do
assert {:error, %{status: 404}} = download(client, "http://example.com/nope")
end

test "without file path", %{client: client} do
assert {:ok, file} = download(client, "http://example.com")

uuid_name = file.name |> String.split(".") |> Enum.at(0)

assert {:ok, _} = Ecto.UUID.dump(uuid_name)
assert file.path != nil
assert File.read!(file.path) == File.read!(@file_cat)
end

test "dynamic URL", %{client: client} do
assert {:ok, file} = download(client, "http://example.com/u/91372?v=3&s=460")
assert file.name == "91372.png"
Expand Down
15 changes: 15 additions & 0 deletions test/bow/ecto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,15 @@ defmodule Bow.EctoTest do
headers: [{"Content-Type", "image/png"}]
}}

%{url: "http://example.com"} = env ->
{:ok,
%{
env
| status: 200,
body: File.read!("test/files/bear.png"),
headers: [{"Content-Type", "image/png"}]
}}

env ->
{:ok, %{env | status: 404}}
end)
Expand Down Expand Up @@ -354,6 +363,12 @@ defmodule Bow.EctoTest do
user = %User{} |> Bow.Ecto.cast_uploads(params, [:avatar], client())
assert %Bow{} = user.changes.avatar
end

test "valid URL without file path" do
params = %{"remote_avatar_url" => "http://example.com"}
user = %User{} |> Bow.Ecto.cast_uploads(params, [:avatar], client())
assert %Bow{} = user.changes.avatar
end
end

describe "#url" do
Expand Down

0 comments on commit 32ca2cf

Please sign in to comment.