diff --git a/lib/atomic/accounts.ex b/lib/atomic/accounts.ex index 676aa304d..ea8854fc7 100644 --- a/lib/atomic/accounts.ex +++ b/lib/atomic/accounts.ex @@ -491,7 +491,7 @@ defmodule Atomic.Accounts do {:error, %Ecto.Changeset{}} """ - def update_user(%User{} = user, attrs \\ %{}, _after_save \\ &{:ok, &1}) do + def update_user(%User{} = user, attrs \\ %{}) do user |> User.changeset(attrs) |> Repo.update() diff --git a/lib/atomic/uploader.ex b/lib/atomic/uploader.ex index 8bb59c9fe..eb6089f24 100644 --- a/lib/atomic/uploader.ex +++ b/lib/atomic/uploader.ex @@ -11,16 +11,32 @@ defmodule Atomic.Uploader do def validate({file, _}) do file_extension = file.file_name |> Path.extname() |> String.downcase() + size = file_size(file) case Enum.member?(extension_whitelist(), file_extension) do - true -> :ok - false -> {:error, "invalid file extension"} + true -> + if 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_file_size, 100_000_000) + end + + def file_size(%Waffle.File{} = file) do + File.stat!(file.path) |> Map.get(:size) + end end end end diff --git a/lib/atomic_web/components/image_uploader.ex b/lib/atomic_web/components/image_uploader.ex index 39a00e1d8..bb0af43c5 100644 --- a/lib/atomic_web/components/image_uploader.ex +++ b/lib/atomic_web/components/image_uploader.ex @@ -1,60 +1,54 @@ 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={@upload.ref}> +or drag and drop
+ <%= extensions_to_string(@upload.accept) %> up to <%= assigns.size_file %> <%= @type %> +
<%= Phoenix.Naming.humanize(err) %>
+ <%= for entry <- @upload.entries do %> + <%= for err <- upload_errors(@upload, entry) do %> +<%= Phoenix.Naming.humanize(err) %>
- <% end %> -