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""" -
+
- <.live_file_input upload={@uploads.image} class="hidden" /> + <.live_file_input upload={@uploads} class="hidden" />
-
-
-
- -
- -

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 %>

- <%= for entry <- @uploads.image.entries do %> - <%= for err <- upload_errors(@uploads.image, entry) do %> + <%= for entry <- @uploads.entries do %> + <%= for err <- upload_errors(@uploads, entry) do %>

<%= Phoenix.Naming.humanize(err) %>

<% end %>
-
- <.live_img_preview entry={entry} /> +
+ <.live_img_preview entry={entry} id={"preview-#{entry.ref}"} class="rounded-lg shadow-lg" />
<%= if String.length(entry.client_name) < 30 do %> - <% entry.client_name %> + <%= entry.client_name %> <% else %> - <% String.slice(entry.client_name, 0..30) <> "... " %> + <%= String.slice(entry.client_name, 0..30) <> "... " %> <% end %>
- <.live_file_input upload={@uploads.picture} class="hidden" /> - -
-
- <.icon name="hero-camera" class="mx-auto w-12 h-12 sm:w-20 sm:h-20 text-white group-hover:text-opacity-70" /> -
-
-
- <%= for entry <- @uploads.picture.entries do %> - <%= for err <- upload_errors(@uploads.picture, entry) do %> -

<%= Phoenix.Naming.humanize(err) %>

- <% end %> -
-
- <.icon name="hero-camera" class="mx-auto w-12 h-12 sm:w-20 sm:h-20 text-white text-opacity-0 rounded-full group-hover:text-opacity-100" /> -
-
- <.live_img_preview entry={entry} class="object-cover object-center rounded-full w-40 h-40 sm:w-48 sm:h-48 border-4 border-white" /> -
-
- <% end %> -
-
+ <%= label(f, :name, "Profile Picture", class: "mt-3 mb-1 text-sm font-medium text-zinc-700") %> + <.live_component module={ImageUploader} id="uploader-profile-picture_1" uploads={@uploads.image_1} target={@myself} class="h-100px w-100px" size_file="10MB" /> + <.live_component module={ImageUploader} id="uploader-profile-picture_2" uploads={@uploads.image_2} target={@myself} class="h-100px w-100px" size_file="100MB" />
<%= submit do %>