Current section

Files

Jump to
ami lib ami thumb_image_attachment.ex
Raw

lib/ami/thumb_image_attachment.ex

defmodule Ami.ThumbImageAttachment do
use Ecto.Schema
use Arc.Ecto.Schema
import Ecto.{
Changeset
}
alias Ami.{
Course
}
alias Ami.ThumbImageAttachmentUploader.Type
schema "thumb_image_attachments" do
@timestamps_opts [type: :naive_datetime_usec]
field(:file_name, :string)
field(:file, Type)
timestamps()
belongs_to(:course, Course)
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:file_name, :course_id])
|> cast_attachments(params, [:file])
|> case do
%{valid?: false, changes: changes} = changeset when changes == %{} ->
%{changeset | action: :ignore}
changeset ->
changeset
end
end
# NOTE : Ugly hack around image path, at some point we must regenerate the old courses image attachments to normalize their url
def thumb_image_for_course(struct) do
ti = List.last(struct)
env = Application.get_env(:ami_web, :env)
if !is_nil(ti) do
case Regex.match?(~r{/\d+/\w+}, ti.file.file_name) do
true ->
case env do
:dev ->
"https://ami-aws-staging.s3.amazonaws.com/uploads/thumb_image/file#{
ti.file.file_name
}"
:staging ->
"https://ami-aws-staging.s3.amazonaws.com/uploads/thumb_image/file#{
ti.file.file_name
}"
:prod ->
"https://ami-aws-s3.s3.amazonaws.com/uploads/thumb_image/file#{ti.file.file_name}"
end
_ ->
case env do
:dev ->
"https://ami-aws-staging.s3.amazonaws.com/uploads/thumb_image/file/#{
ti.file.file_name
}"
:staging ->
"https://ami-aws-staging.s3.amazonaws.com/uploads/thumb_image/file/#{
ti.file.file_name
}"
:prod ->
"https://ami-aws-s3.s3.amazonaws.com/uploads/thumb_image/file/#{ti.file.file_name}"
end
end
end
end
end