@@ -111,34 +111,40 @@ class StorageService {
111111 const sortedFolderPaths = Object . keys ( folderPathsToCreate ) . sort ( ) ;
112112
113113 const foldersCreated : Record < string , FolderInterface > = { } ;
114-
115114 for ( const folderPath of sortedFolderPaths ) {
115+ const tempParentList = [ ] ;
116116 const subFolders = folderPath . split ( "/" ) ;
117- const parentDirectory = subFolders
118- . slice ( 0 , subFolders . length - 1 )
119- . join ( "/" ) ;
120117
121- const tempParentList = [ ] ;
118+ for ( let i = 0 ; i < subFolders . length ; i ++ ) {
119+ const parentDirectory = subFolders . slice ( 0 , i ) . join ( "/" ) ;
120+ if ( tempParentList . length === 0 ) {
121+ tempParentList . push ( ...parentList ) ;
122+ }
122123
123- if ( parentDirectory && foldersCreated [ parentDirectory ] ) {
124- tempParentList . push (
125- ...foldersCreated [ parentDirectory ] . parentList ,
126- foldersCreated [ parentDirectory ] . _id ! . toString ( )
127- ) ;
128- } else {
129- tempParentList . push ( ...parentList ) ;
130- }
124+ if ( parentDirectory && foldersCreated [ parentDirectory ] ) {
125+ tempParentList . push (
126+ foldersCreated [ parentDirectory ] . _id ! . toString ( )
127+ ) ;
128+ }
131129
132- const folderToCreate = subFolders [ subFolders . length - 1 ] ;
130+ const folderToCreate = subFolders [ i ] ;
131+ const tmpPath = ( parentDirectory )
132+ ? [ parentDirectory , folderToCreate ] . join ( "/" )
133+ : folderToCreate ;
133134
134- const folder = await folderDB . createFolder ( {
135- name : folderToCreate ,
136- parent : tempParentList [ tempParentList . length - 1 ] ,
137- owner : user . _id . toString ( ) ,
138- parentList : tempParentList ,
139- } ) ;
135+ if ( foldersCreated [ tmpPath ] ) {
136+ continue ;
137+ }
138+
139+ const folder = await folderDB . createFolder ( {
140+ name : folderToCreate ,
141+ parent : tempParentList [ tempParentList . length - 1 ] ,
142+ owner : user . _id . toString ( ) ,
143+ parentList : tempParentList ,
144+ } ) ;
140145
141- foldersCreated [ folderPath ] = folder ;
146+ foldersCreated [ tmpPath ] = folder ;
147+ }
142148 }
143149
144150 for ( const key of keys ) {
@@ -147,24 +153,16 @@ class StorageService {
147153 const parentDirectory = parentSplit
148154 . slice ( 1 , parentSplit . length - 1 )
149155 . join ( "/" ) ;
150- if ( parentDirectory && foldersCreated [ parentDirectory ] ) {
151- await fileDB . updateFolderUploadedFile (
152- currentFile . uploadedFileId ,
153- user . _id . toString ( ) ,
154- foldersCreated [ parentDirectory ] . _id ! . toString ( ) ,
155- [
156- ...foldersCreated [ parentDirectory ] . parentList ,
157- foldersCreated [ parentDirectory ] . _id ! . toString ( ) ,
158- ] . toString ( )
159- ) ;
160- } else {
161- await fileDB . updateFolderUploadedFile (
162- currentFile . uploadedFileId ,
163- user . _id . toString ( ) ,
164- rootFolder . _id . toString ( ) ,
165- [ ...rootFolder . parentList , rootFolder . _id . toString ( ) ] . toString ( )
166- ) ;
167- }
156+
157+ const currentParent = ( parentDirectory && foldersCreated [ parentDirectory ] )
158+ ? foldersCreated [ parentDirectory ] : rootFolder ;
159+
160+ await fileDB . updateFolderUploadedFile (
161+ currentFile . uploadedFileId ,
162+ user . _id . toString ( ) ,
163+ currentParent . _id ! . toString ( ) ,
164+ [ ...currentParent . parentList , currentParent . _id ! . toString ( ) ] . toString ( )
165+ ) ;
168166 }
169167 } ;
170168
0 commit comments