@@ -29,36 +29,60 @@ const CHUNK = 100;
29
29
Project . count ( { } ) . exec ( ) . then ( ( numProjects ) => {
30
30
console . log ( numProjects ) ;
31
31
let index = 0 ;
32
- } )
33
- Project . find ( { } , ( err , projects ) => {
34
- projects . forEach ( ( project , projectIndex ) => {
35
- if ( ! project . user ) return ;
36
- const userId = project . user . valueOf ( ) ;
37
- project . files . forEach ( ( file , fileIndex ) => {
38
- if ( file . url && file . url . includes ( process . env . S3_BUCKET ) && ! file . url . includes ( userId ) ) {
39
- const key = file . url . split ( '/' ) . pop ( ) ;
40
- console . log ( key ) ;
41
- const params = {
42
- Bucket : `${ process . env . S3_BUCKET } ` ,
43
- CopySource : `${ process . env . S3_BUCKET } /${ key } ` ,
44
- Key : `${ userId } /${ key } `
45
- } ;
46
- try {
47
- client . moveObject ( params )
48
- . on ( 'err' , ( err ) => {
49
- console . log ( err ) ;
50
- } )
51
- . on ( 'end' , ( ) => {
52
- file . url = ( process . env . S3_BUCKET_URL_BASE ||
53
- `https://s3-${ process . env . AWS_REGION } .amazonaws.com/${ process . env . S3_BUCKET } ` ) + `/${ userId } /${ key } ` ;
54
- project . save ( ( err , savedProject ) => {
55
- console . log ( `updated file ${ key } ` ) ;
56
- } ) ;
57
- } ) ;
58
- } catch ( e ) {
59
- console . log ( e ) ;
60
- }
61
- }
62
- } ) ;
63
- } ) ;
32
+ async . whilst (
33
+ ( ) => {
34
+ return index < numProjects ;
35
+ } ,
36
+ ( whilstCb ) => {
37
+ Project . find ( { } ) . skip ( index ) . limit ( CHUNK ) . exec ( ( err , projects ) => {
38
+ async . eachSeries ( projects , ( project , cb ) => {
39
+ if ( ! project . user ) {
40
+ cb ( ) ;
41
+ return ;
42
+ }
43
+ const userId = project . user . valueOf ( ) ;
44
+ console . log ( project . name ) ;
45
+ async . eachSeries ( project . files , ( file , fileCb ) => {
46
+ if ( file . url && file . url . includes ( process . env . S3_BUCKET ) && ! file . url . includes ( userId ) ) {
47
+ const key = file . url . split ( '/' ) . pop ( ) ;
48
+ console . log ( key ) ;
49
+ const params = {
50
+ Bucket : `${ process . env . S3_BUCKET } ` ,
51
+ CopySource : `${ process . env . S3_BUCKET } /${ key } ` ,
52
+ Key : `${ userId } /${ key } `
53
+ } ;
54
+ try {
55
+ client . moveObject ( params )
56
+ . on ( 'err' , ( err ) => {
57
+ console . log ( err ) ;
58
+ } )
59
+ . on ( 'end' , ( ) => {
60
+ file . url = ( process . env . S3_BUCKET_URL_BASE ||
61
+ `https://s3-${ process . env . AWS_REGION } .amazonaws.com/${ process . env . S3_BUCKET } ` ) + `/${ userId } /${ key } ` ;
62
+ project . save ( ( err , savedProject ) => {
63
+ console . log ( `updated file ${ key } ` ) ;
64
+ fileCb ( ) ;
65
+ } ) ;
66
+ } ) ;
67
+ } catch ( e ) {
68
+ console . log ( e ) ;
69
+ fileCb ( e ) ;
70
+ }
71
+ } else {
72
+ fileCb ( ) ;
73
+ }
74
+ } , ( ) => {
75
+ cb ( ) ;
76
+ } ) ;
77
+ } , ( ) => {
78
+ index += CHUNK ;
79
+ whilstCb ( ) ;
80
+ } ) ;
81
+ } ) ;
82
+ } ,
83
+ ( ) => {
84
+ console . log ( 'finished processing all documents.' ) ;
85
+ process . exit ( 0 ) ;
86
+ }
87
+ ) ;
64
88
} ) ;
0 commit comments