@@ -3,26 +3,29 @@ defmodule UploadsWeb.UploadLive do
33 use UploadsWeb , :live_view
44
55 @ accept ~w( .zip .jpg .jpeg .png)
6+ @ max_file_size 20_000_000_000
67
78 def root do
89 case Application . get_env ( :uploads , :uploads_path ) do
910 nil -> Application . app_dir ( :uploads , "priv/static/uploads" )
1011 path -> path
1112 end
1213 end
13-
14+
1415 @ impl true
1516 def mount ( _params , _session , socket ) do
1617 { :ok ,
1718 socket
1819 |> assign ( :done , false )
1920 |> assign ( :form , to_form ( % { } ) )
2021 |> assign ( :uploaded_files , list_uploads! ( ) )
21- |> allow_upload ( :archive , accept: @ accept , max_entries: 4 ) }
22+ |> allow_upload ( :archive , accept: @ accept , max_entries: 4 , max_file_size: @ max_file_size ) }
2223 end
2324
2425 defp list_uploads! do
25- root ( ) |> File . ls! ( ) |> Enum . map ( fn filename ->
26+ root ( )
27+ |> File . ls! ( )
28+ |> Enum . map ( fn filename ->
2629 root ( ) |> Path . join ( filename ) |> to_upload ( )
2730 end )
2831 end
@@ -66,11 +69,13 @@ defmodule UploadsWeb.UploadLive do
6669 def render ( assigns ) do
6770 ~H"""
6871 < div :if = { @ done } >
69- < h1 class = "w-full text-center text-3xl font-bold text-emerald-600 absolute top-[40%] left-0 " > Done!</ h1 >
72+ < h1 class = "w-full text-center text-3xl font-bold text-emerald-600 absolute top-[40%] left-0 " >
73+ Done!
74+ </ h1 >
7075 </ div >
7176 < . form :if = { not @ done } for = { @ form } phx-submit = "save " phx-change = "validate " >
7277 < h2 class = "font-bold text-2xl " > Upload new</ h2 >
73-
78+
7479 < div class = "my-3 " >
7580 < . live_file_input upload = { @ uploads . archive } />
7681 </ div >
@@ -80,12 +85,21 @@ defmodule UploadsWeb.UploadLive do
8085 < div :for = { entry <- @ uploads . archive . entries } >
8186 < div class = "flex gap-3 my-1 " >
8287 < progress value = { entry . progress } max = "100 " class = "flex-grow " > { entry . progress } %</ progress >
83- < button type = "button " phx-click = "cancel " phx-value-ref = { entry . ref } aria-label = "cancel " > ×</ button >
88+ < button type = "button " phx-click = "cancel " phx-value-ref = { entry . ref } aria-label = "cancel " >
89+ ×
90+ </ button >
8491 </ div >
85- < p :for = { err <- upload_errors ( @ uploads . archive , entry ) } class = "text-red-600 px-2 py-1 bg-red-100 " > { error_to_string ( err ) } </ p >
92+ < p
93+ :for = { err <- upload_errors ( @ uploads . archive , entry ) }
94+ class = "text-red-600 px-2 py-1 bg-red-100 "
95+ >
96+ { error_to_string ( err ) }
97+ </ p >
8698 </ div >
8799
88- < p :for = { err <- upload_errors ( @ uploads . archive ) } class = "text-red-600 px-2 py-1 bg-red-100 " > { error_to_string ( err ) } </ p >
100+ < p :for = { err <- upload_errors ( @ uploads . archive ) } class = "text-red-600 px-2 py-1 bg-red-100 " >
101+ { error_to_string ( err ) }
102+ </ p >
89103 </ div >
90104
91105 < . button > Upload!</ . button >
@@ -97,9 +111,15 @@ defmodule UploadsWeb.UploadLive do
97111 < img
98112 :for = { u <- @ uploaded_files }
99113 :if = { is_nil ( u . count ) }
100- src = { u . src } class = "aspect-square w-[200px] object-cover " />
114+ src = { u . src }
115+ class = "aspect-square w-[200px] object-cover "
116+ />
101117
102- < div :for = { u <- @ uploaded_files } :if = { u . count } class = "bg-gray-100 aspect-square w-[200px] shrink-0 flex flex-col justify-center items-center " >
118+ < div
119+ :for = { u <- @ uploaded_files }
120+ :if = { u . count }
121+ class = "bg-gray-100 aspect-square w-[200px] shrink-0 flex flex-col justify-center items-center "
122+ >
103123 < p class = "font-bold " > { Path . basename ( u . path ) } </ p >
104124 < p > < span title = { "#{ u . size } bytes" } > { format_bytes ( u . size ) } </ span > , { u . count } files</ p >
105125 </ div >
@@ -127,18 +147,21 @@ defmodule UploadsWeb.UploadLive do
127147 @ units [ "bytes" , "kB" , "MB" , "GB" , "TB" , "PB" ]
128148
129149 def format_bytes ( atom ) when is_atom ( atom ) , do: "#{ atom } bytes"
150+
130151 def format_bytes ( bytes ) when is_integer ( bytes ) and bytes >= 0 do
131152 format ( bytes , 0 )
132153 end
154+
133155 defp format ( bytes , unit_index ) when bytes < 1024 or unit_index == length ( @ units ) - 1 do
134156 value = Float . round ( bytes , 2 )
135157 "#{ value } #{ @ units |> Enum . at ( unit_index ) } "
136158 end
159+
137160 defp format ( bytes , unit_index ) do
138161 format ( bytes / 1024 , unit_index + 1 )
139162 end
140163
141164 defp error_to_string ( :too_large ) , do: "Too large"
142165 defp error_to_string ( :not_accepted ) , do: "You have selected an unacceptable file type"
143166 defp error_to_string ( :too_many_files ) , do: "You have selected too many files"
144- end
167+ end
0 commit comments