From f30f420f6408dada0879c0ba0cf5ccb0ad2c98ca Mon Sep 17 00:00:00 2001 From: Aya Date: Thu, 26 Jan 2017 14:03:25 +0200 Subject: [PATCH 1/2] Add new file from remote path with new filename --- test/actions/store_test.exs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/actions/store_test.exs b/test/actions/store_test.exs index f184203..7200684 100644 --- a/test/actions/store_test.exs +++ b/test/actions/store_test.exs @@ -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 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 From 0739e90f40cec9dbefc96bb8266062ac0c201598 Mon Sep 17 00:00:00 2001 From: Boris Kuznetsov Date: Sat, 7 Sep 2019 20:48:36 +0300 Subject: [PATCH 2/2] add support to add a custom filename for remote files --- lib/waffle/file.ex | 42 +++++++++++++++++++++++++++++++------ test/actions/store_test.exs | 4 ++-- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/lib/waffle/file.ex b/lib/waffle/file.ex index bc97ef1..bb18e81 100644 --- a/lib/waffle/file.ex +++ b/lib/waffle/file.ex @@ -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) @@ -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 @@ -44,6 +71,9 @@ end end end + # + # Support functions + # defp write_binary(file) do path = generate_temporary_path(file) diff --git a/test/actions/store_test.exs b/test/actions/store_test.exs index 7200684..3b32f1e 100644 --- a/test/actions/store_test.exs +++ b/test/actions/store_test.exs @@ -70,13 +70,13 @@ defmodule WaffleTest.Actions.Store do 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 + 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 Arc.Storage.S3, [put: fn(DummyDefinition, _, {%{file_name: "newfavicon.ico", path: _}, nil}) -> {:ok, "newfavicon.ico"} end] 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