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

support specifying asset host for local storage #185

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Arc expects certain properties to be configured at the application level:
config :arc,
storage: Arc.Storage.S3, # or Arc.Storage.Local
bucket: {:system, "AWS_S3_BUCKET"}, # if using Amazon S3
asset_host: "http://static.example.com" # or {:system "ASSET_HOST"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comma in example
{:system, "ASSET_HOST"}

```

Along with any configuration necessary for ExAws.
Expand Down
15 changes: 12 additions & 3 deletions lib/arc/storage/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ defmodule Arc.Storage.Local do
def url(definition, version, file_and_scope, _options \\ []) do
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an optimization issue here, host() is called two times. Should be:

local_path = build_local_path(definition, version, file_and_scope)
host = host()

if host == "" do
  Path.join "/", local_path
else
  Path.join [host, local_path]
end

local_path = build_local_path(definition, version, file_and_scope)

if String.starts_with?(local_path, "/") do
local_path
if host() == "" do
Path.join "/", local_path
else
"/" <> local_path
Path.join [host(), "/", local_path]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line breaks my tests as it creates URLs like "http://static.example.com/./arctest/uploads/original-binary%20file.png"

Should be Path.join [host, local_path]

end
end

Expand All @@ -34,4 +34,13 @@ defmodule Arc.Storage.Local do
Arc.Definition.Versioning.resolve_file_name(definition, version, file_and_scope)
])
end

defp host do
host_url = Application.get_env(:arc, :asset_host, "")

case host_url do
{:system, env_var} when is_binary(env_var) -> System.get_env(env_var)
url -> url
end
end
end
31 changes: 31 additions & 0 deletions test/storage/local_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule ArcTest.Storage.Local do
use ExUnit.Case
@img "test/support/image.png"
@custom_asset_host "http://static.example.com"

setup_all do
File.mkdir_p("arctest/uploads")
Expand All @@ -10,6 +11,18 @@ defmodule ArcTest.Storage.Local do
end
end

def with_env(app, key, value, fun) do
previous = Application.get_env(app, key, :nothing)

Application.put_env(app, key, value)
fun.()

case previous do
:nothing -> Application.delete_env(app, key)
_ -> Application.put_env(app, key, previous)
end
end


defmodule DummyDefinition do
use Arc.Definition.Storage
Expand Down Expand Up @@ -41,6 +54,24 @@ defmodule ArcTest.Storage.Local do
refute File.exists?("arctest/uploads/1/thumb-image.png")
end

test "get, delete with :asset_host set" do
with_env :arc, :asset_host, @custom_asset_host, fn ->

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?("arctest/uploads/original-image.png")
assert File.exists?("arctest/uploads/1/thumb-image.png")
assert @custom_asset_host <> "/arctest/uploads/original-image.png" == DummyDefinition.url("image.png", :original)
assert @custom_asset_host <> "/arctest/uploads/1/thumb-image.png" == DummyDefinition.url("1/image.png", :thumb)

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

test "save binary" do
Arc.Storage.Local.put(DummyDefinition, :original, {Arc.File.new(%{binary: "binary", filename: "binary.png"}), nil})
assert true == File.exists?("arctest/uploads/binary.png")
Expand Down