@@ -57,12 +57,15 @@ export async function listUserImages(
5757 } ) ,
5858 ) ;
5959 items . sort ( ( a , b ) => new Date ( b . timeCreated ) . getTime ( ) - new Date ( a . timeCreated ) . getTime ( ) ) ;
60- // Deduplicate by ( displayName, hash) — keep newest (first after sort)
60+ // Deduplicate by displayName — keep newest (first after sort)
6161 const seen = new Set < string > ( ) ;
6262 return items . filter ( ( item ) => {
63- const key = `${ displayName ( item . name ) } |${ item . hash || item . downloadURL } ` ;
63+ const dn = displayName ( item . name ) ;
64+ const key = item . hash ? `${ dn } |${ item . hash } ` : dn ;
6465 if ( seen . has ( key ) ) return false ;
6566 seen . add ( key ) ;
67+ // Also mark the plain name as seen so hashless dupes are filtered
68+ if ( item . hash ) seen . add ( dn ) ;
6669 return true ;
6770 } ) ;
6871}
@@ -114,23 +117,25 @@ export async function uploadImageDeduped(
114117 const baseName = file . name . replace ( / \. [ ^ . ] + $ / , '' ) ;
115118 const ext = file . name . split ( '.' ) . pop ( ) || 'png' ;
116119
117- // Check for exact duplicate (same display name and hash)
118- const exactDup = existingImages . find (
119- ( img ) => displayName ( img . name ) === baseName && img . hash === hash ,
120- ) ;
121- if ( exactDup ) {
122- return { downloadURL : exactDup . downloadURL , fileName : file . name , skipped : true } ;
123- }
124-
125- // Check if same name exists with different hash — add suffix
120+ // Find images with the same base display name
126121 const sameNameImages = existingImages . filter (
127122 ( img ) => {
128123 const dn = displayName ( img . name ) ;
129124 return dn === baseName || / ^ .+ \( \d + \) $ / . test ( dn ) && dn . replace ( / \( \d + \) $ / , '' ) . trim ( ) === baseName ;
130125 } ,
131126 ) ;
127+
128+ // Skip if exact duplicate: same name and (matching hash OR no hash on existing image)
129+ const exactDup = sameNameImages . find (
130+ ( img ) => displayName ( img . name ) === baseName && ( ! img . hash || img . hash === hash ) ,
131+ ) ;
132+ if ( exactDup ) {
133+ return { downloadURL : exactDup . downloadURL , fileName : file . name , skipped : true } ;
134+ }
135+
136+ // Same name but different hash — add suffix
132137 let finalName = file . name ;
133- if ( sameNameImages . length > 0 && ! sameNameImages . some ( ( img ) => img . hash === hash ) ) {
138+ if ( sameNameImages . length > 0 ) {
134139 finalName = `${ baseName } (${ sameNameImages . length } ).${ ext } ` ;
135140 }
136141
0 commit comments