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 config option for Arc.Storage.Local directory #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Add the latest stable release to your `mix.exs` file:
```elixir
defp deps do
[
arc: "~> 0.5.2",
arc: "~> 0.5.3",
ex_aws: "~> 0.4.10", # Required if using Amazon S3
httpoison: "~> 0.7" # Required if using Amazon S3
poison: "~> 1.2" # Required if using Amazon S3
Expand Down
7 changes: 6 additions & 1 deletion lib/arc/storage/local.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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.join([local_dir, destination_dir, file.file_name])
path |> Path.dirname() |> File.mkdir_p()
File.copy!(file.path, path)
{:ok, file.file_name}
Expand All @@ -18,8 +18,13 @@ defmodule Arc.Storage.Local do

defp build_local_path(definition, version, file_and_scope) do
Path.join([
local_dir,
definition.storage_dir(version, file_and_scope),
Arc.Definition.Versioning.resolve_file_name(definition, version, file_and_scope)
])
end

defp local_dir do
Application.get_env(:arc, :local_dir) || ""
end
end
18 changes: 18 additions & 0 deletions test/storage/local_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,22 @@ defmodule ArcTest.Storage.Local do
refute File.exists?("arctest/uploads/original-image.png")
refute File.exists?("arctest/uploads/1/thumb-image.png")
end

test "local_dir option" do
local_dir = "arctest/uploads/local_dir"
Application.put_env :arc, :local_dir, local_dir

assert {:ok, "original-image.png"} == Arc.Storage.Local.put(DummyDefinition, :original, {Arc.File.new(%{filename: "original-image.png", path: @img}), nil})
assert {:ok, "1/thumb-image.png"} == Arc.Storage.Local.put(DummyDefinition, :thumb, {Arc.File.new(%{filename: "1/thumb-image.png", path: @img}), nil})

assert File.exists?("#{local_dir}/arctest/uploads/original-image.png")
assert File.exists?("#{local_dir}/arctest/uploads/1/thumb-image.png")
assert "#{local_dir}/arctest/uploads/original-image.png" == DummyDefinition.url("image.png", :original)
assert "#{local_dir}/arctest/uploads/1/thumb-image.png" == DummyDefinition.url("1/image.png", :thumb)

Arc.Storage.Local.delete(DummyDefinition, :original, {%{file_name: "image.png"}, nil})
Arc.Storage.Local.delete(DummyDefinition, :thumb, {%{file_name: "image.png"}, nil})
refute File.exists?("#{local_dir}/arctest/uploads/original-image.png")
refute File.exists?("#{local_dir}/arctest/uploads/1/thumb-image.png")
end
end