-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from gabrielpra1/improvements
Improvements
- Loading branch information
Showing
12 changed files
with
137 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Uploadex.Validation do | ||
@moduledoc false | ||
|
||
@spec validate_extensions(list(any()), any()) :: :ok | {:error, any()} | ||
def validate_extensions(files, accepted_extensions) do | ||
case all_extensions_accepted?(files, accepted_extensions) do | ||
true -> | ||
:ok | ||
|
||
false -> | ||
{:error, "Some files in #{inspect(get_file_names(files))} violate the accepted extensions: #{inspect(accepted_extensions)}"} | ||
end | ||
end | ||
|
||
defp all_extensions_accepted?(_files, :any), do: true | ||
defp all_extensions_accepted?(files, extensions) do | ||
list_extensions = List.wrap(extensions) | ||
Enum.all?(files, & extension_accepted?(&1, list_extensions)) | ||
end | ||
|
||
defp extension_accepted?(%{filename: filename}, accepted_extensions), do: extension_accepted?(filename, accepted_extensions) | ||
defp extension_accepted?(filename, accepted_extensions) when is_binary(filename) do | ||
extension = filename |> Path.extname() |> String.downcase() | ||
Enum.member?(accepted_extensions, extension) | ||
end | ||
|
||
def get_file_names(files) do | ||
Enum.map(files, fn | ||
%{filename: filename} -> filename | ||
filename -> filename | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule S3StorageTest do | ||
use ExUnit.Case | ||
|
||
alias Uploadex.S3Storage | ||
|
||
@opts [bucket: "my-bucket", region: "sa-east-1", directory: "thumbnails"] | ||
|
||
describe "get_url/2" do | ||
test "accepts both a map with filename and the filename directly" do | ||
assert S3Storage.get_url(%{filename: "filename.jpg"}, @opts) == S3Storage.get_url("filename.jpg", @opts) | ||
end | ||
|
||
test "builds the URL correctly" do | ||
assert "https://my-bucket.s3-sa-east-1.amazonaws.com/thumbnails/filename.jpg" == S3Storage.get_url("filename.jpg", @opts) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
defmodule User do | ||
@moduledoc false | ||
|
||
defstruct files: [%{filename: "1"}, %{filename: "2"}] | ||
defstruct files: [%{filename: "1.jpg"}, %{filename: "2.jpg"}] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule ValidationTest do | ||
use ExUnit.Case | ||
|
||
alias Uploadex.Validation | ||
|
||
describe "validate_extensions/2" do | ||
test "returns :ok when accepted extensions is :any" do | ||
assert :ok == Validation.validate_extensions(["anything"], :any) | ||
end | ||
|
||
test "handles both maps and strings" do | ||
assert :ok == Validation.validate_extensions([%{filename: "1.jpg"}, "2.jpg"], ".jpg") | ||
assert {:error, _} = Validation.validate_extensions([%{filename: "1.jpg"}, "2.png"], ".jpg") | ||
assert {:error, _} = Validation.validate_extensions([%{filename: "1.png"}, "2.jpg"], ".jpg") | ||
end | ||
|
||
test "handles uppercase extensions" do | ||
assert :ok == Validation.validate_extensions(["AAA.JPG"], ".jpg") | ||
assert :ok == Validation.validate_extensions(["AAA.JpG"], ".jpg") | ||
end | ||
end | ||
end |