diff --git a/lib/atomic/uploader.ex b/lib/atomic/uploader.ex index 8bb59c9f..b029f442 100644 --- a/lib/atomic/uploader.ex +++ b/lib/atomic/uploader.ex @@ -9,18 +9,29 @@ defmodule Atomic.Uploader do use Waffle.Definition use Waffle.Ecto.Definition - def validate({file, _}) do + def validate(file, _) do file_extension = file.file_name |> Path.extname() |> String.downcase() case Enum.member?(extension_whitelist(), file_extension) do - true -> :ok - false -> {:error, "invalid file extension"} + true -> + if file.size <= max_size() do + :ok + else + {:error, "file size exceeds maximum allowed size"} + end + + false -> + {:error, "invalid file extension"} end end def extension_whitelist do Keyword.get(unquote(opts), :extensions, []) end + + def max_size do + Keyword.get(unquote(opts), :max_size, 500) + end end end end diff --git a/lib/atomic_web/components/image_uploader.ex b/lib/atomic_web/components/image_uploader.ex index 86d19f23..02e2c553 100644 --- a/lib/atomic_web/components/image_uploader.ex +++ b/lib/atomic_web/components/image_uploader.ex @@ -1,60 +1,49 @@ defmodule AtomicWeb.Components.ImageUploader do @moduledoc """ An image uploader component that allows you to upload an image. - The component attributes are: - @uploads - the uploads object - @target - the target to send the event to - - The component events the parent component should define are: - cancel-image - cancels the upload of an image. This event should be defined in the component that you passed in the @target attribute. """ + use AtomicWeb, :live_component def render(assigns) do ~H""" -
or drag and drop
-- PNG, JPG, GIF up to 10MB -
+ end} #{@class} border-2 border-gray-300 border-dashed rounded-md" + } phx-drop-target={@uploads.ref}> +or drag and drop
PNG, JPG, GIF up to <%= @size_file %>
<%= Phoenix.Naming.humanize(err) %>
<% end %><%= Phoenix.Naming.humanize(err) %>
- <% end %> -