Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[image: {"is invalid", [type: AppName.ImageUploader.Type]}] #81

@zachbosteel

Description

@zachbosteel

Back in February, I had arc_ecto configured and working just fine to upload images to S3 and save the path to my database. As sometimes happens with projects, the client didn't start using what I had built until earlier this week, and (without any changes to deps or related files since February on my part) things don't seem to work any longer. When I try to upload an image, I get the error mentioned in the title. Here's my ImageUploader file:

defmodule Ekf.ImageUploader do
  use Arc.Definition
  use Arc.Ecto.Definition

  @versions [:original]

  # Whitelist file extensions:
  def validate({file, _}) do
    ~w(.jpg .jpeg .gif .png) |> Enum.member?(Path.extname(file.file_name))
  end
end

Here's my Ecto Model file:

defmodule Ekf.Image do
  use Ekf.Web, :model
  use Arc.Ecto.Schema

  schema "images" do
    belongs_to :static_page, Ekf.StaticPage
    belongs_to :class_page, Ekf.ClassPage
    belongs_to :instructor_page, Ekf.InstructorPage
    field :path, :string
    field :title, :string
    field :alt, :string
    field :label, :string
    field :image, Ekf.ImageUploader.Type

    timestamps()
  end

  @doc """
  Builds a changeset based on the `struct` and `params`.
  """
  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:path, :title, :alt, :image, :label, :static_page_id, :class_page_id, :instructor_page_id])
    |> cast_attachments(params, [:image])
    |> validate_required([:title, :alt, :image, :label])
  end

  def path_changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:path])
    |> validate_required([:path])
  end
end

Here's the map I'm passing to the changeset method on update, along with a model struct:

%{
label: "page-image",
image: %Plug.Upload{content_type: "image/jpeg", filename: "martial_arts_by_minorityman.jpg", path: "/var/folders/hw/cyd1v_z95l115dr3m5j08l5h0000gn/T//plug-1501/multipart-474368-69450-4"}, 
alt: "A powerful roundhouse kick.", 
title: "Muay Thai",
class_page_id: 2
}

Here's the error I'm getting in its entirety:

#Ecto.Changeset<action: nil, changes: %{},
 errors: [image: {"is invalid", [type: Ekf.ImageUploader.Type]}],
 data: #Ekf.Image<>, valid?: false>

I'm pretty stumped here. It seems as if the custom type has stopped working, and I could see that happening if any of deps had crept, but I really don't think they have. Here are my pinned versions for reference:

  defp deps do
    [{:phoenix, "~> 1.2.0"},
     {:phoenix_pubsub, "~> 1.0"},
     {:phoenix_ecto, "~> 3.0"},
     {:postgrex, ">= 0.0.0"},
     {:phoenix_html, "~> 2.6"},
     {:phoenix_live_reload, "~> 1.0", only: :dev},
     {:gettext, "~> 0.11"},
     {:cowboy, "~> 1.0"},
     {:ex_machina, "~> 1.0"},
     {:comeonin, "~> 2.5"},
     {:guardian, "~> 0.13.0"},
     {:arc, "~> 0.5.2"},
     {:arc_ecto, "~> 0.4.4"},
     {:ex_aws, "~> 0.5.0"},
     {:poison, "~> 2.0"},
     {:httpoison, "~> 0.9.0"},
     {:bamboo, "~> 0.8.0"},
     {:bamboo_smtp, "~> 1.3.0"}
    ]
  end

Any help would be much appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions