Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/arc/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@ defmodule Arc.File do
{:ok, local_path} -> %Arc.File{path: local_path, file_name: filename}
:error -> {:error, :invalid_file_path}
end
end
end

# Given a remote file with a filename
def new(%{remote_path: remote_path, filename: filename} = %{remote_path: "http" <> _, filename: _}) do
uri = URI.parse(remote_path)
case save_file(uri, filename) do
{:ok, local_path} -> %Arc.File{path: local_path, file_name: filename}
:error -> {:error, :invalid_file_path}
end
end

# Rejects remote file path
def new(%{remote_path: remote_path, filename: filename} = %{remote_path: _, filename: _}) do
{:error, :invalid_file_path}
end

# Accepts a path
def new(path) when is_binary(path) do
Expand Down
12 changes: 12 additions & 0 deletions test/actions/store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ defmodule ArcTest.Actions.Store do
assert DummyDefinition.store("https://www.google.com/favicon.ico") == {:ok, "favicon.ico"}
end
end

test "accepts remote files with filenames" do
with_mock Arc.Storage.S3, [put: fn(DummyDefinition, _, {%{file_name: "newfavicon.ico", path: _}, nil}) -> {:ok, "newfavicon.ico"} end] do
assert DummyDefinition.store(%{remote_path: "https://www.google.com/favicon.ico", filename: "newfavicon.ico"}) == {:ok, "newfavicon.ico"}
end
end

test "rejects remote files with filenames and invalid remote path" do
with_mock Arc.Storage.S3, [put: fn(DummyDefinition, _, {%{file_name: "newfavicon.ico", path: _}, nil}) -> {:ok, "newfavicon.ico"} end] do
assert DummyDefinition.store(%{remote_path: "path/favicon.ico", filename: "newfavicon.ico"}) == {:error, :invalid_file_path}
end
end
end