diff --git a/lib/arc/file.ex b/lib/arc/file.ex index dadccb4..5dc4ad4 100644 --- a/lib/arc/file.ex +++ b/lib/arc/file.ex @@ -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 diff --git a/test/actions/store_test.exs b/test/actions/store_test.exs index 6e517aa..26074ac 100644 --- a/test/actions/store_test.exs +++ b/test/actions/store_test.exs @@ -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