@@ -13,6 +13,8 @@ import {
1313 latlongRule ,
1414 parseEncounterDateString ,
1515} from "./BulkImportConstants" ;
16+ import { defaultMaxMediaSize } from "../../constants/photoUpload.js" ;
17+ import { toast } from "react-toastify" ;
1618
1719dayjs . extend ( customParseFormat ) ;
1820export class BulkImportStore {
@@ -38,6 +40,9 @@ export class BulkImportStore {
3840 _columnsDef = [ ] ;
3941 _rawColumns = [ ] ;
4042 _maxImageCount = 200 ;
43+ _maxImageSizeMB = defaultMaxMediaSize ;
44+ _rejectedTooLarge = [ ] ;
45+ _rejectedToastTimer = null ;
4146 _pageSize = 10 ;
4247 _locationID = [ ] ;
4348 _locationIDOptions = [ ] ;
@@ -424,6 +429,10 @@ export class BulkImportStore {
424429 return this . _maxImageCount ;
425430 }
426431
432+ get maxImageSizeMB ( ) {
433+ return this . _maxImageSizeMB ;
434+ }
435+
427436 get worksheetInfo ( ) {
428437 return this . _worksheetInfo ;
429438 }
@@ -656,6 +665,10 @@ export class BulkImportStore {
656665 this . _maxImageCount = maxImageCount ;
657666 }
658667
668+ setMaxImageSizeMB ( mb ) {
669+ this . _maxImageSizeMB = Number ( mb ) || defaultMaxMediaSize ;
670+ }
671+
659672 setLocationIDOptions ( options ) {
660673 this . _locationIDOptions = options ;
661674 }
@@ -990,7 +1003,7 @@ export class BulkImportStore {
9901003 this . _submissionId = submissionId ;
9911004 }
9921005
993- initializeFlow ( fileInputRef , maxSize ) {
1006+ initializeFlow ( fileInputRef ) {
9941007 const submissionId = this . _submissionId || uuidv4 ( ) ;
9951008 this . _submissionId = submissionId ;
9961009 const flowInstance = new Flow ( {
@@ -1009,16 +1022,15 @@ export class BulkImportStore {
10091022 `${ file . name } -${ file . size } -${ file . lastModified } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } ` ;
10101023
10111024 flowInstance . assignBrowse ( fileInputRef ) ;
1012- let rejectedFiles = [ ] ;
10131025 let hasShownImageLimitAlert = false ;
10141026 flowInstance . on ( "fileAdded" , ( file ) => {
10151027 const currentCount = this . _imagePreview . length ;
10161028 const totalCount = currentCount + 1 ;
1017- const isTooLarge = file . size > maxSize * 1024 * 1024 ;
1029+ const maxSizeMB = this . _maxImageSizeMB ;
1030+ const isTooLarge = file . size > maxSizeMB * 1024 * 1024 ;
10181031
10191032 if ( isTooLarge ) {
1020- const reason = "too large" ;
1021- rejectedFiles . push ( `${ file . name } (${ reason } )` ) ;
1033+ this . _notifyTooLarge ( file ) ;
10221034 return false ;
10231035 }
10241036
@@ -1115,10 +1127,6 @@ export class BulkImportStore {
11151127 } ) ;
11161128
11171129 flowInstance . on ( "fileError" , ( file , chunk ) => {
1118- // if (!navigator.onLine) {
1119- // console.log(`Chunk uploading failed due to no internet connection`, file.name, chunk.offset);
1120- // return;
1121- // }
11221130 console . error ( `Upload failed: ${ file . name } , chunk: ${ chunk . offset } ` ) ;
11231131 this . _imagePreview = this . _imagePreview . map ( ( f ) =>
11241132 f . fileName === file . name ? { ...f , error : true , progress : 0 } : f ,
@@ -1130,6 +1138,27 @@ export class BulkImportStore {
11301138 this . _flow = flowInstance ;
11311139 }
11321140
1141+ _notifyTooLarge ( file ) {
1142+ const maxSizeMB = this . _maxImageSizeMB ;
1143+ this . _rejectedTooLarge . push ( file . name ) ;
1144+
1145+ if ( this . _rejectedToastTimer ) return ;
1146+
1147+ this . _rejectedToastTimer = setTimeout ( ( ) => {
1148+ const names = this . _rejectedTooLarge ;
1149+ this . _rejectedTooLarge = [ ] ;
1150+ this . _rejectedToastTimer = null ;
1151+
1152+ const firstFew = names . slice ( 0 , 5 ) . join ( ", " ) ;
1153+ const more = names . length > 5 ? ` (+${ names . length - 5 } more)` : "" ;
1154+
1155+ toast . error (
1156+ `Some files exceed the ${ maxSizeMB } MB limit: ${ firstFew } ${ more } ` ,
1157+ { autoClose : 6000 } ,
1158+ ) ;
1159+ } , 3000 ) ;
1160+ }
1161+
11331162 generateThumbnailsForFirst50 ( ) {
11341163 const previews = this . _imagePreview ;
11351164 if ( previews . length > this . _imageCountGenerateThumbnail )
@@ -1199,35 +1228,17 @@ export class BulkImportStore {
11991228 } ) ;
12001229 }
12011230
1202- uploadFilteredFiles ( maxSize ) {
1231+ uploadFilteredFiles ( ) {
12031232 if ( ! this . _flow ) {
12041233 console . warn ( "Flow instance not initialized." ) ;
12051234 return ;
12061235 }
1236+ const maxSize = this . _maxImageSizeMB ;
12071237
12081238 const validFiles = this . _flow . files . filter (
12091239 ( file ) => file . size <= maxSize * 1024 * 1024 ,
12101240 ) ;
12111241
1212- const invalidFiles = this . _flow . files . filter (
1213- ( file ) => file . size > maxSize * 1024 * 1024 ,
1214- ) ;
1215-
1216- if ( invalidFiles . length > 0 ) {
1217- console . warn (
1218- `The following files are too large (> ${ maxSize } MB) and will not be uploaded:` ,
1219- invalidFiles . map (
1220- ( f ) => `${ f . name } (${ ( f . size / 1024 / 1024 ) . toFixed ( 2 ) } MB)` ,
1221- ) ,
1222- ) ;
1223- alert (
1224- `The following files are too large (> ${ maxSize } MB) and will not be uploaded:\n` +
1225- invalidFiles
1226- . map ( ( f ) => `${ f . name } (${ ( f . size / 1024 / 1024 ) . toFixed ( 2 ) } MB)` )
1227- . join ( "\n" ) ,
1228- ) ;
1229- }
1230-
12311242 if ( validFiles . length === 0 ) {
12321243 return ;
12331244 }
@@ -1329,13 +1340,13 @@ export class BulkImportStore {
13291340 } ) ;
13301341 }
13311342
1332- traverseFileTree ( entry , maxSize ) {
1343+ traverseFileTree ( entry ) {
13331344 this . _incrementPending ( ) ;
13341345
13351346 if ( entry . isDirectory ) {
13361347 const reader = entry . createReader ( ) ;
13371348 reader . readEntries ( ( entries ) => {
1338- entries . forEach ( ( ent ) => this . traverseFileTree ( ent , maxSize ) ) ;
1349+ entries . forEach ( ( ent ) => this . traverseFileTree ( ent ) ) ;
13391350 this . _decrementPending ( ) ;
13401351 } ) ;
13411352 } else if ( entry . isFile ) {
@@ -1347,7 +1358,15 @@ export class BulkImportStore {
13471358 // "image/bmp",
13481359 ] ;
13491360 const isValid = supportedTypes . includes ( file . type ) ;
1350- // && file.size <= maxSize * 1024 * 1024;
1361+
1362+ const maxSizeMB = this . _maxImageSizeMB ;
1363+ const isTooLarge = file . size > maxSizeMB * 1024 * 1024 ;
1364+
1365+ if ( isTooLarge ) {
1366+ this . _notifyTooLarge ( { name : file . name , size : file . size } ) ;
1367+ this . _decrementPending ( ) ;
1368+ return ;
1369+ }
13511370
13521371 if ( isValid && ! this . _imageSectionFileNames . includes ( file . name ) ) {
13531372 this . _collectedValidFiles . push ( file ) ;
0 commit comments