image upload mime validation - shows you what it thinks the mime type is (#79)

This commit is contained in:
liamwhite 2020-04-07 00:22:03 -04:00 committed by GitHub
parent 603c8d4867
commit ae6640e9d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 2 deletions

View file

@ -155,7 +155,8 @@ defmodule Philomena.Images.Image do
|> validate_length(:image_name, max: 255, count: :bytes) |> validate_length(:image_name, max: 255, count: :bytes)
|> validate_inclusion( |> validate_inclusion(
:image_mime_type, :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) |> unsafe_validate_unique([:image_orig_sha512_hash], Repo)
end end

View file

@ -33,5 +33,5 @@ defmodule Philomena.Mime do
when mime in ~W(image/gif image/jpeg image/png image/svg+xml video/webm), when mime in ~W(image/gif image/jpeg image/png image/svg+xml video/webm),
do: {:ok, mime} do: {:ok, mime}
def true_mime(_mime), do: :error def true_mime(mime), do: {:unsupported_mime, mime}
end end

View file

@ -43,6 +43,10 @@ defmodule Philomena.Uploader do
changeset_fn.(model_or_changeset, attributes) changeset_fn.(model_or_changeset, attributes)
else else
{:unsupported_mime, mime} ->
attributes = prefix_attributes(%{"mime_type" => mime}, field_name)
changeset_fn.(model_or_changeset, attributes)
_error -> _error ->
changeset_fn.(model_or_changeset, %{}) changeset_fn.(model_or_changeset, %{})
end end