From ae6640e9d3b36bf640d7867d60e2b3402653e935 Mon Sep 17 00:00:00 2001 From: liamwhite Date: Tue, 7 Apr 2020 00:22:03 -0400 Subject: [PATCH] image upload mime validation - shows you what it thinks the mime type is (#79) --- lib/philomena/images/image.ex | 3 ++- lib/philomena/mime.ex | 2 +- lib/philomena/uploader.ex | 4 ++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/philomena/images/image.ex b/lib/philomena/images/image.ex index cd104150..8204c45f 100644 --- a/lib/philomena/images/image.ex +++ b/lib/philomena/images/image.ex @@ -155,7 +155,8 @@ defmodule Philomena.Images.Image do |> validate_length(:image_name, max: 255, count: :bytes) |> validate_inclusion( :image_mime_type, - ~W(image/gif image/jpeg image/png image/svg+xml video/webm) + ~W(image/gif image/jpeg image/png image/svg+xml video/webm), + message: "(#{attrs["image_mime_type"]}) is invalid" ) |> unsafe_validate_unique([:image_orig_sha512_hash], Repo) end diff --git a/lib/philomena/mime.ex b/lib/philomena/mime.ex index 9ef37285..08d5dfc1 100644 --- a/lib/philomena/mime.ex +++ b/lib/philomena/mime.ex @@ -33,5 +33,5 @@ defmodule Philomena.Mime do when mime in ~W(image/gif image/jpeg image/png image/svg+xml video/webm), do: {:ok, mime} - def true_mime(_mime), do: :error + def true_mime(mime), do: {:unsupported_mime, mime} end diff --git a/lib/philomena/uploader.ex b/lib/philomena/uploader.ex index 8e73db47..c246f102 100644 --- a/lib/philomena/uploader.ex +++ b/lib/philomena/uploader.ex @@ -43,6 +43,10 @@ defmodule Philomena.Uploader do changeset_fn.(model_or_changeset, attributes) else + {:unsupported_mime, mime} -> + attributes = prefix_attributes(%{"mime_type" => mime}, field_name) + changeset_fn.(model_or_changeset, attributes) + _error -> changeset_fn.(model_or_changeset, %{}) end