@@ -184,7 +184,7 @@ function renderSelected(files) {
184184 li . append ( info , status , remove ) ;
185185 list . appendChild ( li ) ;
186186 } ) ;
187- upload . disabled = accepted . length === 0 ;
187+ upload . disabled = uploadLimitReached || accepted . length === 0 ;
188188}
189189
190190async function refreshReadyState ( ) {
@@ -219,6 +219,37 @@ function assignFiles(fileList) {
219219 }
220220}
221221
222+ function getUserCode ( ) {
223+ const parts = [
224+ navigator . userAgent , navigator . language , screen . width + "x" + screen . height ,
225+ screen . colorDepth , new Date ( ) . getTimezoneOffset ( ) , navigator . hardwareConcurrency || 0
226+ ] . join ( "|" ) ;
227+ let hash = 0 ;
228+ for ( let i = 0 ; i < parts . length ; i ++ ) { hash = ( ( hash << 5 ) - hash + parts . charCodeAt ( i ) ) | 0 ; }
229+ return "u" + Math . abs ( hash ) . toString ( 36 ) + "-" + parts . length ;
230+ }
231+ function applyUploadLimit ( data ) {
232+ const uploadButton = document . getElementById ( "uploadButton" ) ;
233+ const input = document . getElementById ( "fileInput" ) ;
234+ const status = document . getElementById ( "uploadStatus" ) ;
235+ uploadLimitReached = ! ! ( data && data . limitReached ) ;
236+ if ( uploadLimitReached ) {
237+ const when = data . resetAtUtc ? new Date ( data . resetAtUtc ) . toLocaleString ( ) : "" ;
238+ setLive ( status , "You can upload only one document per day. You can upload again after " + when + "." , "error" ) ;
239+ if ( uploadButton ) uploadButton . disabled = true ;
240+ if ( input ) input . disabled = true ;
241+ } else if ( input ) {
242+ input . disabled = false ;
243+ }
244+ }
245+ async function checkUploadLimit ( ) {
246+ try {
247+ const res = await fetch ( "/api/upload-quota/" + encodeURIComponent ( getUserCode ( ) ) ) ;
248+ if ( res . ok ) applyUploadLimit ( await res . json ( ) ) ;
249+ } catch ( e ) { /* quota check is best-effort; the server enforces it */ }
250+ }
251+ let uploadLimitReached = false ;
252+
222253async function uploadFiles ( ) {
223254 const input = document . getElementById ( "fileInput" ) ;
224255 const uploadButton = document . getElementById ( "uploadButton" ) ;
@@ -235,7 +266,13 @@ async function uploadFiles() {
235266 setLive ( status , rejected . map ( function ( f ) { return f . name + " is not supported. Use PDF, Word, or Excel." ; } ) . join ( " " ) , "error" ) ;
236267 return ;
237268 }
269+ if ( uploadLimitReached ) return ;
270+ if ( accepted . length > 1 ) {
271+ setLive ( status , "Only one document can be uploaded per day. Please select a single file." , "error" ) ;
272+ return ;
273+ }
238274 const formData = new FormData ( ) ;
275+ formData . append ( "userCode" , getUserCode ( ) ) ;
239276 accepted . forEach ( function ( file ) { formData . append ( "files" , file ) ; } ) ;
240277 uploadButton . disabled = true ;
241278 progress . classList . add ( "is-active" ) ;
@@ -247,6 +284,7 @@ async function uploadFiles() {
247284 const err = new Error ( "upload" ) ;
248285 err . status = response . status ;
249286 err . detail = payload && payload . error ;
287+ if ( response . status === 429 ) applyUploadLimit ( { limitReached : true , resetAtUtc : payload . resetAtUtc } ) ;
250288 throw err ;
251289 }
252290 const added = payload . files || accepted . map ( function ( f ) { return f . name ; } ) ;
@@ -258,10 +296,11 @@ async function uploadFiles() {
258296 input . value = "" ;
259297 renderSelected ( [ ] ) ;
260298 await refreshReadyState ( ) ;
299+ await checkUploadLimit ( ) ;
261300 } catch ( error ) {
262301 const fallback = friendlyError ( error , "Could not prepare these documents. Try adding them again." ) ;
263302 setLive ( status , error && error . detail ? fallback + " " + error . detail : fallback , "error" ) ;
264- uploadButton . disabled = accepted . length === 0 ;
303+ uploadButton . disabled = uploadLimitReached || accepted . length === 0 ;
265304 } finally {
266305 progress . classList . remove ( "is-active" ) ;
267306 }
@@ -1480,6 +1519,7 @@ document.addEventListener("click", function (event) {
14801519} ) ;
14811520document . addEventListener ( "DOMContentLoaded" , function ( ) {
14821521 refreshReadyState ( ) ;
1522+ checkUploadLimit ( ) ;
14831523 const input = document . getElementById ( "fileInput" ) ;
14841524 const drop = document . getElementById ( "dropZone" ) ;
14851525 if ( input ) input . addEventListener ( "change" , function ( ) { assignFiles ( input . files ) ; } ) ;
0 commit comments