Skip to content

Instantly share code, notes, and snippets.

@kexoth
Created April 21, 2016 17:15
Show Gist options
  • Save kexoth/0f69e37e8461ac6fa23209d77f974cea to your computer and use it in GitHub Desktop.
Save kexoth/0f69e37e8461ac6fa23209d77f974cea to your computer and use it in GitHub Desktop.
Ecto Arc S3 Image Uploader Not Working
<%= form_for @changeset, @action, [multipart: true], fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<div class="form-group">
<%= label f, :title, class: "control-label" %>
<%= text_input f, :title, class: "form-control" %>
<%= error_tag f, :title %>
</div>
<div class="form-group">
<%= label f, :image, class: "control-label" %>
<%= file_input f, :image, class: "form-control" %>
<%= error_tag f, :image %>
</div>
<div class="form-group">
<%= label f, :description, class: "control-label" %>
<%= textarea f, :description, class: "form-control" %>
<%= error_tag f, :description %>
</div>
<div class="form-group">
<%= label f, :url, class: "control-label" %>
<%= url_input f, :url, class: "form-control" %>
<%= error_tag f, :url %>
</div>
<div class="form-group">
<%= label f, :draft, class: "control-label" %>
<%= checkbox f, :draft, class: "form-control" %>
<%= error_tag f, :draft %>
</div>
<div class="form-group">
<%= submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
defmodule Estrada.Image do
use Arc.Definition
# Include ecto support (requires package arc_ecto installed):
use Arc.Ecto.Definition
@acl :public_read
# @versions [:original]
# To add a thumbnail version:
@versions [:original, :thumb]
# Whitelist file extensions:
# def validate({file, _}) do
# ~w(.jpg .jpeg .gif .png) |> Enum.member?(Path.extname(file.file_name))
# end
# Define a thumbnail transformation:
def transform(:thumb, _) do
{:convert, "-strip -thumbnail 250x250^ -gravity center -extent 250x250 -format png", :png}
end
def transform(:original, _), do: :noaction
# Override the persisted filenames:
def filename(version, {file, scope}) do
# version
"#{version}_#{file.file_name}"
end
# Override the storage directory:
def storage_dir(version, {file, scope}) do
"marijaserifovic/news/" #{scope.id}"
end
# Provide a default URL if there hasn't been a file uploaded
def default_url(version, scope) do
# "/images/avatars/default_#{version}.png"
"marijaserifovic/maxresdefault.jpg"
end
# Specify custom headers for s3 objects
# Available options are [:cache_control, :content_disposition,
# :content_encoding, :content_length, :content_type,
# :expect, :expires, :storage_class, :website_redirect_location]
#
# def s3_object_headers(version, {file, scope}) do
# [content_type: Plug.MIME.path(file.file_name)]
# end
end
defmodule Estrada.NewsArticle do
use Estrada.Web, :model
use Arc.Ecto.Model
schema "news" do
field :title, :string
field :image, Estrada.Image.Type
field :description, :string
field :url, :string
field :draft, :boolean, default: false
timestamps
end
@required_fields ~w(title description draft)
@optional_fields ~w(url)
@required_file_fields ~w(image)
@optional_file_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> cast_attachments(params, @required_file_fields, @optional_file_fields)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment