1
1
const { NIL : SYSTEM_USER } = require ( 'uuid' ) ;
2
2
3
3
const errorToProblem = require ( '../components/errorToProblem' ) ;
4
- const { addDashesToUuid, getCurrentIdentity, formatS3KeyForCompare, isAtPath } = require ( '../components/utils' ) ;
4
+ const { addDashesToUuid, getCurrentIdentity, formatS3KeyForCompare, isPrefixOfPath } = require ( '../components/utils' ) ;
5
5
const utils = require ( '../db/models/utils' ) ;
6
6
const log = require ( '../components/log' ) ( module . filename ) ;
7
7
@@ -126,31 +126,40 @@ const controller = {
126
126
try {
127
127
// delete buckets not found in S3 from COMS db
128
128
const oldDbBuckets = dbBuckets . filter ( b => ! s3Keys . includes ( b . key ) ) ;
129
- for ( const dbBucket of oldDbBuckets ) {
130
- await bucketService . delete ( dbBucket . bucketId , trx ) ;
131
- dbBuckets = dbBuckets . filter ( b => b . bucketId != dbBucket . bucketId ) ;
132
- }
129
+ await Promise . all (
130
+ oldDbBuckets . map ( dbBucket =>
131
+ bucketService . delete ( dbBucket . bucketId , trx )
132
+ . then ( ( ) => {
133
+ dbBuckets = dbBuckets . filter ( b => b . bucketId !== dbBucket . bucketId ) ;
134
+ } )
135
+ )
136
+ ) ;
137
+
133
138
// Create buckets only found in S3 in COMS db
134
139
const newS3Keys = s3Keys . filter ( k => ! dbBuckets . map ( b => b . key ) . includes ( k ) ) ;
135
- for ( const s3Key of newS3Keys ) {
136
- const data = {
137
- bucketName : s3Key . substring ( s3Key . lastIndexOf ( '/' ) + 1 ) ,
138
- accessKeyId : parentBucket . accessKeyId ,
139
- bucket : parentBucket . bucket ,
140
- endpoint : parentBucket . endpoint ,
141
- key : s3Key ,
142
- secretAccessKey : parentBucket . secretAccessKey ,
143
- region : parentBucket . region ?? undefined ,
144
- active : parentBucket . active ,
145
- userId : parentBucket . createdBy ?? SYSTEM_USER ,
146
- // current user has MANAGE perm on parent folder (see route.hasPermission)
147
- // ..so copy all their perms to NEW subfolders
148
- permCodes : currentUserParentBucketPerms
149
- } ;
140
+ await Promise . all (
141
+ newS3Keys . map ( s3Key => {
142
+ const data = {
143
+ bucketName : s3Key . substring ( s3Key . lastIndexOf ( '/' ) + 1 ) ,
144
+ accessKeyId : parentBucket . accessKeyId ,
145
+ bucket : parentBucket . bucket ,
146
+ endpoint : parentBucket . endpoint ,
147
+ key : s3Key ,
148
+ secretAccessKey : parentBucket . secretAccessKey ,
149
+ region : parentBucket . region ?? undefined ,
150
+ active : parentBucket . active ,
151
+ userId : parentBucket . createdBy ?? SYSTEM_USER ,
152
+ // current user has MANAGE perm on parent folder (see route.hasPermission)
153
+ // ..so copy all their perms to NEW subfolders
154
+ permCodes : currentUserParentBucketPerms
155
+ } ;
156
+ return bucketService . create ( data , trx )
157
+ . then ( ( dbResponse ) => {
158
+ dbBuckets . push ( dbResponse ) ;
159
+ } ) ;
160
+ } )
161
+ ) ;
150
162
151
- const dbResponse = await bucketService . create ( data , trx ) ;
152
- dbBuckets . push ( dbResponse ) ;
153
- }
154
163
return dbBuckets ;
155
164
}
156
165
catch ( err ) {
@@ -175,6 +184,7 @@ const controller = {
175
184
const dbObjects = await objectService . searchObjects ( {
176
185
bucketId : dbBuckets . map ( b => b . bucketId )
177
186
} , trx ) ;
187
+
178
188
/**
179
189
* merge arrays of objects from COMS db and S3 to form an array of jobs with format:
180
190
* [ { path: '/images/img3.jpg', bucketId: '123' }, { path: '/images/album1/img1.jpg', bucketId: '456' } ]
@@ -191,9 +201,7 @@ const controller = {
191
201
...s3Objects . DeleteMarkers . map ( object => {
192
202
return {
193
203
path : object . Key ,
194
- bucketId : dbBuckets
195
- . filter ( b => isAtPath ( b . key , object . Key ) )
196
- . map ( b => b . bucketId ) [ 0 ]
204
+ bucketId : dbBuckets . find ( b => isPrefixOfPath ( b . key , object . Key ) ) ?. bucketId
197
205
} ;
198
206
} ) ,
199
207
// Versions found in S3
@@ -202,15 +210,14 @@ const controller = {
202
210
. map ( object => {
203
211
return {
204
212
path : object . Key ,
205
- bucketId : dbBuckets
206
- . filter ( b => isAtPath ( b . key , object . Key ) )
207
- . map ( b => b . bucketId ) [ 0 ] ,
208
- // adding current userId will give ALL perms on new objects
213
+ bucketId : dbBuckets . find ( b => isPrefixOfPath ( b . key , object . Key ) ) ?. bucketId
214
+ // NOTE: adding current userId will give ALL perms on new objects
209
215
// and set createdBy on all downstream resources (versions, tags, meta)
210
216
// userId: userId
211
217
} ;
212
- } )
218
+ } ) ,
213
219
] ) ] ;
220
+
214
221
// merge and remove duplicates
215
222
const jobs = [ ...new Map ( objects . map ( o => [ o . path , o ] ) ) . values ( ) ] ;
216
223
0 commit comments