Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
write binary to file
Browse files Browse the repository at this point in the history
  • Loading branch information
OpakAlex authored and stavro committed Aug 25, 2016
1 parent e096199 commit 600a688
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/deps
erl_crash.dump
*.ez
.DS_Store
.DS_Store
/arctest
4 changes: 4 additions & 0 deletions lib/arc/actions/store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/arc/file.ex
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
18 changes: 16 additions & 2 deletions lib/arc/storage/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
11 changes: 10 additions & 1 deletion lib/arc/storage/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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
20 changes: 10 additions & 10 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -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], []}}
6 changes: 6 additions & 0 deletions test/storage/local_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 600a688

Please sign in to comment.