From 888b458eef82cb4eee0dcedd64f5febb9b5d3508 Mon Sep 17 00:00:00 2001 From: azhi Date: Tue, 16 Jan 2018 18:18:00 +0300 Subject: [PATCH 1/2] allow passing binary data structs as attachments arc allows storing `%{filename: filename, binary: data}`, and now arc_ecto allows these structs to be accepted in `cast_attachments` fixes #54 --- lib/waffle_ecto/schema.ex | 5 +++++ test/schema_test.exs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/lib/waffle_ecto/schema.ex b/lib/waffle_ecto/schema.ex index 835f2b5..c7ef371 100644 --- a/lib/waffle_ecto/schema.ex +++ b/lib/waffle_ecto/schema.ex @@ -36,6 +36,11 @@ defmodule Waffle.Ecto.Schema do # Allow casting Plug.Uploads {field, upload = %{__struct__: Plug.Upload}}, fields -> [{field, {upload, scope}} | fields] + # Allow casting binary data structs + {field, upload = %{filename: filename, binary: binary}}, fields + when is_binary(filename) and is_binary(binary) -> + [{field, {upload, scope}} | fields] + # If casting a binary (path), ensure we've explicitly allowed paths {field, path}, fields when is_binary(path) -> cond do diff --git a/test/schema_test.exs b/test/schema_test.exs index fa84c15..974fdbc 100644 --- a/test/schema_test.exs +++ b/test/schema_test.exs @@ -113,4 +113,9 @@ defmodule WaffleTest.Ecto.Schema do TestUser.url_changeset(%TestUser{}, %{"avatar" => "/path/to/my/file.png"}) assert not called DummyDefinition.store({"/path/to/my/file.png", %TestUser{}}) end + + test_with_mock "casting binary data struct attachments", DummyDefinition, [store: fn({%{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}, %TestUser{}}) -> {:ok, "file.png"} end] do + changeset = TestUser.changeset(%TestUser{}, %{"avatar" => %{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}}) + assert called DummyDefinition.store({%{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}, %TestUser{}}) + end end From 3382e130e6676bcf29d83b4c1303b235f93c694b Mon Sep 17 00:00:00 2001 From: Boris Kuznetsov Date: Mon, 16 Sep 2019 21:50:41 +0300 Subject: [PATCH 2/2] remove warning --- test/schema_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/schema_test.exs b/test/schema_test.exs index 974fdbc..8cf4f36 100644 --- a/test/schema_test.exs +++ b/test/schema_test.exs @@ -115,7 +115,7 @@ defmodule WaffleTest.Ecto.Schema do end test_with_mock "casting binary data struct attachments", DummyDefinition, [store: fn({%{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}, %TestUser{}}) -> {:ok, "file.png"} end] do - changeset = TestUser.changeset(%TestUser{}, %{"avatar" => %{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}}) + TestUser.changeset(%TestUser{}, %{"avatar" => %{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}}) assert called DummyDefinition.store({%{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}, %TestUser{}}) end end