@@ -28,6 +28,8 @@ import { REST_API_TYPE } from '../apitypes'
2828import {
2929 EXPORT_FORMAT_TO_FILE_FORMAT ,
3030 fromBase64 ,
31+ getDocumentTitle ,
32+ getFileExtension ,
3133 removeFirstSlash ,
3234 slugify ,
3335 takeIfDefined ,
@@ -95,7 +97,7 @@ export class DocumentGroupStrategy implements BuilderStrategy {
9597 }
9698
9799 const transformedDocuments = await Promise . all ( transformTasks )
98- const transformedDocumentsWithoutCollisions = ( [ 'fileId' , 'filename' ] as const ) . reduce ( resolveCollisions , transformedDocuments )
100+ const transformedDocumentsWithoutCollisions = ( [ 'fileId' , 'filename' ] as const ) . reduce ( resolveCollisionsByField , transformedDocuments )
99101
100102 for ( const document of transformedDocumentsWithoutCollisions ) {
101103 buildResult . documents . set ( document . fileId , document )
@@ -106,19 +108,18 @@ export class DocumentGroupStrategy implements BuilderStrategy {
106108}
107109
108110// there is a chance that the renamed document will be the same as another document (this case has not been fixed yet)
109- function resolveCollisions ( docs : VersionRestDocument [ ] , field : 'fileId' | 'filename' ) : VersionRestDocument [ ] {
111+ function resolveCollisionsByField ( docs : VersionRestDocument [ ] , field : 'fileId' | 'filename' ) : VersionRestDocument [ ] {
110112 const fileIdMap = groupBy ( docs , ( document ) => document [ field ] )
111113 return ( [ ...fileIdMap . values ( ) ] as VersionRestDocument [ ] [ ] ) . reduce ( ( acc , docs ) => {
112114 const [ _ , ...duplicates ] = docs
113- duplicates . forEach ( ( document , index ) => { document [ field ] = rename ( document [ field ] , index ) } )
115+ duplicates . forEach ( ( document , index ) => { document [ field ] = renameDuplicate ( document [ field ] , index ) } )
114116 return [ ...acc , ...docs ]
115117 } , [ ] as VersionRestDocument [ ] )
116118}
117119
118- function rename ( fileName : string , index : number ) : string {
119- const nameParts = fileName . split ( '.' )
120- const extension = nameParts . length > 1 ? nameParts [ nameParts ?. length - 1 ] : ''
121- const nameWithPostfix = `${ nameParts [ 0 ] } ${ index + 1 } `
120+ function renameDuplicate ( fileName : string , index : number ) : string {
121+ const extension = getFileExtension ( fileName )
122+ const nameWithPostfix = `${ getDocumentTitle ( fileName ) } -${ index + 1 } `
122123
123124 if ( extension ) {
124125 return `${ nameWithPostfix } .${ extension } `
0 commit comments