From 600a68806f603329815631bcebf3a0a6a2f215bd Mon Sep 17 00:00:00 2001 From: Opak Alex Date: Wed, 12 Aug 2015 14:18:46 +0300 Subject: [PATCH] write binary to file --- .gitignore | 3 ++- lib/arc/actions/store.ex | 4 ++++ lib/arc/file.ex | 6 +++++- lib/arc/storage/local.ex | 18 ++++++++++++++++-- lib/arc/storage/s3.ex | 11 ++++++++++- mix.lock | 20 ++++++++++---------- test/storage/local_test.exs | 6 ++++++ 7 files changed, 53 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index fc40176..9d1dd56 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /deps erl_crash.dump *.ez -.DS_Store \ No newline at end of file +.DS_Store +/arctest diff --git a/lib/arc/actions/store.ex b/lib/arc/actions/store.ex index daaec6e..03aa103 100644 --- a/lib/arc/actions/store.ex +++ b/lib/arc/actions/store.ex @@ -5,6 +5,10 @@ defmodule Arc.Actions.Store do end end + def store(definition, {file = %Arc.File{}, scope}) do + put(definition, {file, scope}) + end + def store(definition, {file, scope}) when is_binary(file) or is_map(file) do put(definition, {Arc.File.new(file), scope}) end diff --git a/lib/arc/file.ex b/lib/arc/file.ex index d83124e..8797d07 100644 --- a/lib/arc/file.ex +++ b/lib/arc/file.ex @@ -1,5 +1,5 @@ defmodule Arc.File do - defstruct [:path, :file_name] + defstruct [:path, :file_name, :binary] # Accepts a path def new(path) when is_binary(path) do @@ -9,6 +9,10 @@ defmodule Arc.File do end end + def new(binary, filename) do + %Arc.File{ binary: binary, file_name: Path.basename(filename) } + end + # Accepts a map conforming to %Plug.Upload{} syntax def new(%{filename: filename, path: path}) do case File.exists?(path) do diff --git a/lib/arc/storage/local.ex b/lib/arc/storage/local.ex index f343f1d..683b8ba 100644 --- a/lib/arc/storage/local.ex +++ b/lib/arc/storage/local.ex @@ -2,8 +2,13 @@ defmodule Arc.Storage.Local do def put(definition, version, {file, scope}) do destination_dir = definition.storage_dir(version, {file, scope}) path = Path.join(destination_dir, file.file_name) - path |> Path.dirname() |> File.mkdir_p() - File.copy!(file.path, path) + path |> Path.dirname() |> File.mkdir_p!() + binary = extract_binary(file) + if file.binary do + File.write!(path, binary) + else + File.copy!(file.path, path) + end {:ok, file.file_name} end @@ -22,4 +27,13 @@ defmodule Arc.Storage.Local do Arc.Definition.Versioning.resolve_file_name(definition, version, file_and_scope) ]) end + + defp extract_binary(file) do + if file.binary do + binary = file.binary + else + {:ok, binary} = File.read(file.path) + binary + end + end end diff --git a/lib/arc/storage/s3.ex b/lib/arc/storage/s3.ex index 2675da9..b257a25 100644 --- a/lib/arc/storage/s3.ex +++ b/lib/arc/storage/s3.ex @@ -4,7 +4,7 @@ defmodule Arc.Storage.S3 do def put(definition, version, {file, scope}) do destination_dir = definition.storage_dir(version, {file, scope}) s3_key = Path.join(destination_dir, file.file_name) - binary = File.read!(file.path) + binary = extract_binary(file) acl = definition.acl(version, {file, scope}) s3_options = @@ -72,4 +72,13 @@ defmodule Arc.Storage.S3 do name -> name end end + + defp extract_binary(file) do + if file.binary do + binary = file.binary + else + {:ok, binary} = File.read(file.path) + binary + end + end end diff --git a/mix.lock b/mix.lock index aef9a58..b427b8c 100644 --- a/mix.lock +++ b/mix.lock @@ -1,10 +1,10 @@ -%{"certifi": {:hex, :certifi, "0.3.0"}, - "ex_aws": {:hex, :ex_aws, "0.4.13"}, - "hackney": {:hex, :hackney, "1.4.7"}, - "httpoison": {:hex, :httpoison, "0.8.0"}, - "idna": {:hex, :idna, "1.0.2"}, - "meck": {:hex, :meck, "0.8.2"}, - "mimerl": {:hex, :mimerl, "1.0.2"}, - "mock": {:hex, :mock, "0.1.1"}, - "poison": {:hex, :poison, "1.5.0"}, - "ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5"}} +%{"certifi": {:hex, :certifi, "0.3.0", "389d4b126a47895fe96d65fcf8681f4d09eca1153dc2243ed6babad0aac1e763", [:rebar3], []}, + "ex_aws": {:hex, :ex_aws, "0.4.13", "80578054ccd8fda9e7ebb8c84d94075e15db4e29665c7c435244d3d065dc3667", [:mix], [{:httpoison, "~> 0.7", [hex: :httpoison, optional: true]}, {:httpotion, "~> 2.0", [hex: :httpotion, optional: true]}, {:jsx, "~> 2.5", [hex: :jsx, optional: true]}, {:poison, "~> 1.2", [hex: :poison, optional: true]}, {:sweet_xml, "~> 0.5", [hex: :sweet_xml, optional: true]}]}, + "hackney": {:hex, :hackney, "1.4.7", "fcca8e6ba215de6225cc9b56230a3ef441ddaee05d963a39b56ca7b064026342", [:rebar3], [{:certifi, "0.3.0", [hex: :certifi, optional: false]}, {:idna, "1.0.2", [hex: :idna, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_hostname, "1.0.5", [hex: :ssl_verify_hostname, optional: false]}]}, + "httpoison": {:hex, :httpoison, "0.8.0", "52a958d40b2aa46da418cdf6d8dfd82ba83e94d5e60920dfa5f40c05b34fe073", [:mix], [{:hackney, "~> 1.4.4", [hex: :hackney, optional: false]}]}, + "idna": {:hex, :idna, "1.0.2", "397e3d001c002319da75759b0a81156bf11849c71d565162436d50020cb7265e", [:make], []}, + "meck": {:hex, :meck, "0.8.2", "f15f7d513e14ec8c8dee9a95d4ae585b3e5a88bf0fa6a7573240d6ddb58a7236", [:make, :rebar], []}, + "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []}, + "mock": {:hex, :mock, "0.1.1", "e21469ca27ba32aa7b18b61699db26f7a778171b21c0e5deb6f1218a53278574", [:mix], [{:meck, "~> 0.8.2", [hex: :meck, optional: false]}]}, + "poison": {:hex, :poison, "1.5.0", "f2f4f460623a6f154683abae34352525e1d918380267cdbd949a07ba57503248", [:mix], []}, + "ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5", "2e73e068cd6393526f9fa6d399353d7c9477d6886ba005f323b592d389fb47be", [:make], []}} diff --git a/test/storage/local_test.exs b/test/storage/local_test.exs index d6676df..35de78a 100644 --- a/test/storage/local_test.exs +++ b/test/storage/local_test.exs @@ -14,6 +14,8 @@ defmodule ArcTest.Storage.Local do defmodule DummyDefinition do use Arc.Definition.Storage use Arc.Actions.Url + use Arc.Definition.Storage + @acl :public_read def transform(:thumb, _), do: {:convert, "-strip -thumbnail 10x10"} def transform(:original, _), do: :noaction @@ -38,4 +40,8 @@ defmodule ArcTest.Storage.Local do refute File.exists?("arctest/uploads/original-image.png") refute File.exists?("arctest/uploads/1/thumb-image.png") end + test "save binary" do + Arc.Storage.Local.put(DummyDefinition, :original, {Arc.File.new("binary", "binary.png"), nil}) + assert true == File.exists?("arctest/uploads/binary.png") + end end