Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new file from remote path with new filename #13

Merged
merged 2 commits into from
Sep 9, 2019
Merged
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
42 changes: 36 additions & 6 deletions lib/waffle/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ defmodule Waffle.File do
Path.join(System.tmp_dir, file_name)
end

#
# Handle a remote file
#

# Given a remote file
def new(remote_path = "http" <> _) do
uri = URI.parse(remote_path)
Expand All @@ -21,21 +25,44 @@ defmodule Waffle.File do
{:ok, local_path} -> %Waffle.File{path: local_path, file_name: filename, is_tempfile?: true}
:error -> {:error, :invalid_file_path}
end
end
end

# Accepts a path
def new(path) when is_binary(path) do
case File.exists?(path) do
true -> %Waffle.File{path: path, file_name: Path.basename(path)}
false -> {:error, :invalid_file_path}
# Given a remote file with a filename
def new(%{filename: filename, remote_path: remote_path} = %{filename: _, remote_path: "http" <> _}) do
uri = URI.parse(remote_path)
case save_file(uri, filename) do
{:ok, local_path} -> %Waffle.File{path: local_path, file_name: filename, is_tempfile?: true}
:error -> {:error, :invalid_file_path}
end
end

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

#
# Handle a binary blob
#

def new(%{filename: filename, binary: binary}) do
%Waffle.File{binary: binary, file_name: Path.basename(filename)}
|> write_binary()
end


#
# Handle a local file
#

# Accepts a path
def new(path) when is_binary(path) do
case File.exists?(path) do
true -> %Waffle.File{path: path, file_name: Path.basename(path)}
false -> {:error, :invalid_file_path}
end
end

# Accepts a map conforming to %Plug.Upload{} syntax
def new(%{filename: filename, path: path}) do
case File.exists?(path) do
Expand All @@ -44,6 +71,9 @@ end
end
end

#
# Support functions
#

defp write_binary(file) do
path = generate_temporary_path(file)
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 @@ -68,4 +68,16 @@ defmodule WaffleTest.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 Waffle.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 Waffle.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