keichan34 / exfile

File upload persistence and processing for Phoenix / Plug
MIT License
90 stars 19 forks source link

Gracefully handle non-existant exfile field #61

Closed cblock closed 6 years ago

cblock commented 6 years ago

Use Case: In my ecto model :profile_picture is an optional Exfile.Ecto.File field.

When I call the below changeset function with profile_picture not being set exfile raises an no function clause matching in Exfile.Backend.FileSystem.upload/2 error when calling Exfile.Ecto.prepare_uploads([:profile_picture]). This pull request fixes the problem.

def changeset(%Person{} = person attrs) do
    person
    |> cast(attrs, [:lastname, :firstname, :profile_picture, :birthday, :parents, :street, :postal_code, :town, :phone1_label, :phone1, :phone2_label, :phone2, :phone3_label, :phone3, :mail1, :mail2, :group_id])
    |> validate_required([:lastname, :firstname])
    |> validate_content_type(:profile_picture, :image)
    |> Exfile.Ecto.prepare_uploads([:profile_picture])
  end
keichan34 commented 6 years ago

Looks good. Thanks!