@@ -28,7 +28,11 @@ export class GridFSBucketAdapter extends FilesAdapter {
28
28
this . _algorithm = 'aes-256-gcm' ;
29
29
this . _encryptionKey =
30
30
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 )
32
36
: null ;
33
37
const defaultMongoOptions = {
34
38
useNewUrlParser : true ,
@@ -138,8 +142,8 @@ export class GridFSBucketAdapter extends FilesAdapter {
138
142
}
139
143
140
144
async rotateEncryptionKey ( options = { } ) {
141
- var fileNames = [ ] ;
142
- var oldKeyFileAdapter = { } ;
145
+ let fileNames = [ ] ;
146
+ let oldKeyFileAdapter = { } ;
143
147
const bucket = await this . _getBucket ( ) ;
144
148
if ( options . oldKey !== undefined ) {
145
149
oldKeyFileAdapter = new GridFSBucketAdapter (
@@ -158,51 +162,22 @@ export class GridFSBucketAdapter extends FilesAdapter {
158
162
fileNames . push ( file . filename ) ;
159
163
} ) ;
160
164
}
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 } ;
206
181
}
207
182
208
183
getFileLocation ( config , filename ) {
0 commit comments