@@ -28,7 +28,11 @@ export class GridFSBucketAdapter extends FilesAdapter {
2828 this . _algorithm = 'aes-256-gcm' ;
2929 this . _encryptionKey =
3030 encryptionKey !== undefined
31- ? crypto . createHash ( 'sha256' ) . update ( String ( encryptionKey ) ) . digest ( 'base64' ) . substr ( 0 , 32 )
31+ ? crypto
32+ . createHash ( 'sha256' )
33+ . update ( String ( encryptionKey ) )
34+ . digest ( 'base64' )
35+ . substring ( 0 , 32 )
3236 : null ;
3337 const defaultMongoOptions = {
3438 useNewUrlParser : true ,
@@ -138,8 +142,8 @@ export class GridFSBucketAdapter extends FilesAdapter {
138142 }
139143
140144 async rotateEncryptionKey ( options = { } ) {
141- var fileNames = [ ] ;
142- var oldKeyFileAdapter = { } ;
145+ let fileNames = [ ] ;
146+ let oldKeyFileAdapter = { } ;
143147 const bucket = await this . _getBucket ( ) ;
144148 if ( options . oldKey !== undefined ) {
145149 oldKeyFileAdapter = new GridFSBucketAdapter (
@@ -158,51 +162,22 @@ export class GridFSBucketAdapter extends FilesAdapter {
158162 fileNames . push ( file . filename ) ;
159163 } ) ;
160164 }
161- return new Promise ( resolve => {
162- var fileNamesNotRotated = fileNames ;
163- var fileNamesRotated = [ ] ;
164- var fileNameTotal = fileNames . length ;
165- var fileNameIndex = 0 ;
166- fileNames . forEach ( fileName => {
167- oldKeyFileAdapter
168- . getFileData ( fileName )
169- . then ( plainTextData => {
170- //Overwrite file with data encrypted with new key
171- this . createFile ( fileName , plainTextData )
172- . then ( ( ) => {
173- fileNamesRotated . push ( fileName ) ;
174- fileNamesNotRotated = fileNamesNotRotated . filter ( function ( value ) {
175- return value !== fileName ;
176- } ) ;
177- fileNameIndex += 1 ;
178- if ( fileNameIndex == fileNameTotal ) {
179- resolve ( {
180- rotated : fileNamesRotated ,
181- notRotated : fileNamesNotRotated ,
182- } ) ;
183- }
184- } )
185- . catch ( ( ) => {
186- fileNameIndex += 1 ;
187- if ( fileNameIndex == fileNameTotal ) {
188- resolve ( {
189- rotated : fileNamesRotated ,
190- notRotated : fileNamesNotRotated ,
191- } ) ;
192- }
193- } ) ;
194- } )
195- . catch ( ( ) => {
196- fileNameIndex += 1 ;
197- if ( fileNameIndex == fileNameTotal ) {
198- resolve ( {
199- rotated : fileNamesRotated ,
200- notRotated : fileNamesNotRotated ,
201- } ) ;
202- }
203- } ) ;
204- } ) ;
205- } ) ;
165+ let fileNamesNotRotated = fileNames ;
166+ const fileNamesRotated = [ ] ;
167+ for ( const fileName of fileNames ) {
168+ try {
169+ const plainTextData = await oldKeyFileAdapter . getFileData ( fileName ) ;
170+ // Overwrite file with data encrypted with new key
171+ await this . createFile ( fileName , plainTextData ) ;
172+ fileNamesRotated . push ( fileName ) ;
173+ fileNamesNotRotated = fileNamesNotRotated . filter ( function ( value ) {
174+ return value !== fileName ;
175+ } ) ;
176+ } catch ( err ) {
177+ continue ;
178+ }
179+ }
180+ return { rotated : fileNamesRotated , notRotated : fileNamesNotRotated } ;
206181 }
207182
208183 getFileLocation ( config , filename ) {
0 commit comments