Skip to content

Commit

Permalink
changed order gen_magic & ex_image_info
Browse files Browse the repository at this point in the history
  • Loading branch information
ndrean committed Dec 21, 2023
1 parent dc509c6 commit 2965580
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
5 changes: 0 additions & 5 deletions .bumblebee/README.md

This file was deleted.

53 changes: 37 additions & 16 deletions lib/app_web/live/page_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,36 @@ defmodule AppWeb.PageLive do
Double-checks the MIME type of uploaded file to ensure that the file
is an image and is not corrupted.
"""
def check_file(binary, path) do
with {:image_info, {mimetype, width, height, variant}} <-
{:image_info, ExImageInfo.info(binary)},
{:gen_magic, {:ok, %{mime_type: mime}}} <-
{:gen_magic, App.Image.gen_magic_eval(path, @accepted_mime)} do
if mimetype == mime,
do: {:ok, {mimetype, width, height, variant}},
else: {:error, "MIME types do not correspond"}
else
{:image_info, nil} -> {:error, "bad file"}
{:gen_magic, {:error, reason}} -> {:error, reason}

# def check_file(binary, path) do
# with {:image_info, {mimetype, width, height, variant}} <-
# {:image_info, ExImageInfo.info(binary)},
# {:gen_magic, {:ok, %{mime_type: mime}}} <-
# {:gen_magic, App.Image.gen_magic_eval(path, @accepted_mime)} do
# if mimetype == mime,
# do: {:ok, {mimetype, width, height, variant}},
# else: {:error, "MIME types do not correspond"}
# else
# {:image_info, nil} -> {:error, "bad file"}
# {:gen_magic, {:error, reason}} -> {:error, reason}
# end
# end

def magic_check(path) do
App.Image.gen_magic_eval(path, @accepted_mime)
|> case do
{:ok, %{mime_type: mime}} ->
{:ok, %{mime_type: mime}}

{:error, msg} ->
{:error, msg}
end
end

def check_mime(magic_mime, info_mime) do
if magic_mime == info_mime, do: :ok, else: :error
end

@doc """
This function is called whenever an image is uploaded by the user.
Expand All @@ -110,11 +126,13 @@ defmodule AppWeb.PageLive do
# and if consuming the entry was successful.
with %{tensor: tensor, image_info: image_info} <-
consume_uploaded_entry(socket, entry, fn %{path: path} ->
with file_binary <-
File.read!(path),
# run MIME type checking
{:ok, {mimetype, width, height, _variant}} <-
check_file(file_binary, path),
with {:magic, {:ok, %{mime_type: mime}}} <-
{:magic, magic_check(path)} |> dbg(),
file_binary <- File.read!(path),
{:image_info, {mimetype, width, height, _variant}} <-
{:image_info, ExImageInfo.info(file_binary)},
{:check_mime, :ok} <-
{:check_mime, check_mime(mime, mimetype)},
# Get image and resize
{:ok, thumbnail_vimage} <-
Vix.Vips.Operation.thumbnail(path, @image_width, size: :VIPS_SIZE_DOWN),
Expand All @@ -140,6 +158,9 @@ defmodule AppWeb.PageLive do
{:ok, %{error: reason}}
end
else
{:magic, {:error, msg}} -> {:postpone, %{error: msg}}
{:check_mime, :error} -> {:postpone, %{error: "bad check"}}
{:image_info, nil} -> {:postpone, %{error: "image_info error"}}
{:error, reason} -> {:postpone, %{error: reason}}
end
end) do
Expand Down
2 changes: 1 addition & 1 deletion test/app_web/live/page_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ defmodule AppWeb.PageLiveTest do
path = [:code.priv_dir(:app), "static", "images", "test.png"] |> Path.join()
file = build_upload(path, "image/xyz")

Check warning on line 96 in test/app_web/live/page_live_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

variable "file" is unused (if the variable is not meant to be used, prefix it with an underscore)

assert AppWeb.PageLive.check_file(file, path) == {:error, "bad file"}
assert AppWeb.PageLive.magic_check(path) == {:error, "not acceptable"}

accepted_mime = ~w(image/jpeg image/jpg image/png image/webp)
assert App.Image.gen_magic_eval(path, accepted_mime) == {:error, "not acceptable"}
Expand Down

0 comments on commit 2965580

Please sign in to comment.