Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
* upstream/master:
  Fix POSIX error typo
  Better error message for failed uploads
  Rename perform_uploads/2 back to perform_uploads!/2
  Gracefully handle optional exfile fields not being set
  Do not add processed file back to changeset in cast_content_type. Fixes  keichan34#43 and keichan34#48
  Replace deprecated String.strip/1 with String.trim/1
  • Loading branch information
scarfacedeb committed Jun 7, 2019
2 parents 867d38d + 955a589 commit dfeb446
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
29 changes: 18 additions & 11 deletions lib/exfile/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,28 @@ defmodule Exfile.Ecto do
end

defp perform_uploads!(changeset, fields) do
Enum.reduce fields, changeset, fn
(field, changeset) ->
t = ecto_type_for_field(changeset.data, field)
file = Changeset.get_field(changeset, field)
case t.upload!(file) do
{:ok, uploaded_file} ->
Changeset.put_change(changeset, field, uploaded_file)
error ->
throw error
end
Enum.reduce fields, changeset, fn (field, changeset) ->
perform_upload!(changeset, field)
end
end

defp perform_upload!(changeset, field) do
with {:ok, t} <- ecto_type_for_field(changeset.data, field),
file when not is_nil(file) <- Changeset.get_field(changeset, field),
{:ok, uploaded_file} <- t.upload!(file)
do
Changeset.put_change(changeset, field, uploaded_file)
else
nil -> #happens if an exfile field is optional and thus might not be set
changeset
error ->
throw error
end

end

defp ecto_type_for_field(%{__struct__: mod}, field) do
mod.__schema__(:type, field)
{:ok, mod.__schema__(:type, field)}
end
end

Expand Down
1 change: 0 additions & 1 deletion lib/exfile/ecto/cast_content_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ defmodule Exfile.Ecto.CastContentType do
|> case do
{ :ok, processed_file } ->
changeset
|> Changeset.put_change(field, processed_file)
|> Changeset.put_change(content_type_field, processed_file.meta["content_type"])
_ -> changeset
end
Expand Down
2 changes: 1 addition & 1 deletion lib/exfile/identify.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Exfile.Identify do
end

defp extract_content_type_from_file_output(out) do
out = String.strip(out)
out = String.trim(out)
if Regex.match?(~r{^[-\w]+/[-\w]+$}, out) do
{:ok, out}
else
Expand Down
2 changes: 1 addition & 1 deletion lib/exfile/processor/content_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Exfile.Processor.ContentType do

defp extract_meta(result) do
result
|> String.strip
|> String.trim
|> String.split(": ")
|> List.last
|> (&%{ "content_type" => &1 }).()
Expand Down
2 changes: 1 addition & 1 deletion lib/exfile/tempfile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ defmodule Exfile.Tempfile do
:ok ->
:ets.update_element(ets, pid, {3, [path|paths]})
{:ok, path}
{:error, reason} when reason in [:eexist, :eaccess] ->
{:error, reason} when reason in [:eexist, :eacces] ->
open_random_file(prefix, tmp, attempts + 1, pid, ets, paths)
end
end
Expand Down
17 changes: 15 additions & 2 deletions test/exfile/ecto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,33 @@ defmodule Exfile.EctoTest do

image_file = %Plug.Upload{ path: "test/fixtures/sample.jpg", filename: "sample.jpg" }
changeset = cast(%Exfile.S.Image{}, %{image: image_file}, [:image])
changeset_without_image = cast(%Exfile.S.Image{}, %{}, [])

{:ok, %{ changeset: changeset }}
{:ok, %{ changeset: changeset, changeset_without_image: changeset_without_image }}
end

test "prepare_uploads/2 saves a file from the cache backend to the store backend", %{changeset: changeset} do

assert get_field(changeset, :image).backend.backend_name == "cache"

assert {:ok, data} = changeset
|> prepare_uploads([:image])
|> Exfile.Repo.insert

assert data.image.backend.backend_name == "store"
end

test "prepare_uploads/2 ignores a file field with null value", %{changeset_without_image: changeset} do

assert get_field(changeset, :image) == nil

assert {:ok, data} = changeset
|> prepare_uploads([:image])
|> Exfile.Repo.insert

assert data.image == nil
end


test "prepare_uploads/2 doesn't save the file to store if there are errors", %{changeset: changeset} do
changeset = add_error(changeset, :image_content_type, "invalid")

Expand All @@ -34,4 +46,5 @@ defmodule Exfile.EctoTest do
assert get_field(error_changeset, :image).id == get_field(changeset, :image).id
assert get_field(error_changeset, :image).backend.backend_name == "cache"
end

end

0 comments on commit dfeb446

Please sign in to comment.