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

Custom error keys returned from validate function #73

Open
wants to merge 5 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
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,20 @@ end

While storing files on S3 (rather than your harddrive) eliminates some malicious attack vectors, it is strongly encouraged to validate the extensions of uploaded files as well.

Arc delegates validation to a `validate/1` function with a tuple of the file and scope. As an example, to validate that an uploaded file conforms to popular image formats, you may use:
Arc delegates validation to a `validate/1` function with a tuple of the file and scope. The function needs to return a tuple of two atoms `{:ok, :valid}` or `{:error, :reason}`. As an example, to validate that an uploaded file conforms to popular image formats, you may use:

```elixir
defmodule Avatar do
use Arc.Definition
@extension_whitelist ~w(.jpg .jpeg .gif .png)

def validate({file, _}) do
def validate({file, _}) do
file_extension = file.file_name |> Path.extname |> String.downcase
Enum.member?(@extension_whitelist, file_extension)
if Enum.member?(@extension_whitelist, file_extension) do
{:ok, :valid_extension}
else
{:error, :invalid_extension}
end
end
end
```
Expand Down Expand Up @@ -431,7 +435,7 @@ defmodule Avatar do

def acl(:thumb, _), do: :public_read

def validate({file, _}) do
def validate({file, _}) do
file_extension = file.file_name |> Path.extname |> String.downcase
Enum.member?(@extension_whitelist, file_extension)
end
Expand Down
5 changes: 3 additions & 2 deletions lib/arc/actions/store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ defmodule Arc.Actions.Store do

defp put(definition, {%Arc.File{}=file, scope}) do
case definition.validate({file, scope}) do
true -> put_versions(definition, {file, scope})
_ -> {:error, :invalid_file}
{:ok, _} -> put_versions(definition, {file, scope})
{_, error} -> {:error, error}
_ -> {:error, :invalid_file}
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/arc/definition/storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Arc.Definition.Storage do

def filename(_, {file, _}), do: Path.basename(file.file_name, Path.extname(file.file_name))
def storage_dir(_, _), do: "uploads"
def validate(_), do: true
def validate(_), do: {:ok, :valid}
def default_url(version, _), do: default_url(version)
def default_url(_), do: nil
def __storage, do: Arc.Storage.S3
Expand Down
9 changes: 8 additions & 1 deletion test/actions/store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ defmodule ArcTest.Actions.Store do
use Arc.Actions.Store
use Arc.Definition.Storage

def validate({file, _}), do: String.ends_with?(file.file_name, ".png")
def validate({file, _}) do
if String.ends_with?(file.file_name, ".png") do
{:ok, :ok}
else
{:error, :invalid_file}
end
end

def transform(_, _), do: :noaction
def __versions, do: [:original, :thumb]
end
Expand Down