@@ -4,7 +4,8 @@ var EventEmitter = require('events').EventEmitter,
4
4
formidable = require ( 'formidable' ) ,
5
5
imageMagick = require ( 'imagemagick' ) ,
6
6
mkdirp = require ( 'mkdirp' ) ,
7
- _ = require ( 'lodash' ) ;
7
+ _ = require ( 'lodash' ) ,
8
+ async = require ( 'async' ) ;
8
9
9
10
module . exports = function ( options ) {
10
11
@@ -34,19 +35,25 @@ module.exports = function (options) {
34
35
this . noCache ( ) ;
35
36
var files = [ ] ;
36
37
fs . readdir ( options . uploadDir ( ) , _ . bind ( function ( err , list ) {
37
- _ . each ( list , function ( name ) {
38
- var stats = fs . statSync ( options . uploadDir ( ) + '/' + name ) ,
39
- fileInfo ;
40
- if ( stats . isFile ( ) ) {
41
- fileInfo = new FileInfo ( {
42
- name : name ,
43
- size : stats . size
44
- } ) ;
45
- this . initUrls ( fileInfo ) ;
46
- files . push ( fileInfo ) ;
47
- }
48
- } , this ) ;
49
- this . callback ( { files : files } ) ;
38
+ async . each ( list , function ( name , cb ) {
39
+ fs . stat ( options . uploadDir ( ) + '/' + name , function ( err , stats ) {
40
+ if ( ! err ) {
41
+ if ( stats . isFile ( ) ) {
42
+ fileInfo = new FileInfo ( {
43
+ name : name ,
44
+ size : stats . size
45
+ } ) ;
46
+ this . initUrls ( fileInfo ) ;
47
+ files . push ( fileInfo ) ;
48
+ }
49
+ }
50
+ cb ( err ) ;
51
+ } ) ;
52
+ } ,
53
+ function ( err ) {
54
+ if ( err ) console . log ( err ) ;
55
+ this . callback ( { files : files } ) ;
56
+ } ) ;
50
57
} , this ) ) ;
51
58
} ;
52
59
@@ -87,7 +94,8 @@ module.exports = function (options) {
87
94
} )
88
95
. on ( 'file' , function ( name , file ) {
89
96
var fileInfo = map [ path . basename ( file . path ) ] ;
90
- if ( fs . existsSync ( file . path ) ) {
97
+ fs . exists ( file . path , function ( exists ) {
98
+ if ( exists ) {
91
99
fileInfo . size = file . size ;
92
100
if ( ! fileInfo . validate ( ) ) {
93
101
fs . unlink ( file . path ) ;
@@ -98,9 +106,7 @@ module.exports = function (options) {
98
106
if ( options . imageTypes . test ( fileInfo . name ) ) {
99
107
_ . each ( options . imageVersions , function ( value , version ) {
100
108
// creating directory recursive
101
- if ( ! fs . existsSync ( options . uploadDir ( ) + '/' + version + '/' ) )
102
- mkdirp . sync ( options . uploadDir ( ) + '/' + version + '/' ) ;
103
-
109
+ mkdirp ( options . uploadDir ( ) + '/' + version + '/' , function ( err , made ) {
104
110
counter ++ ;
105
111
var opts = options . imageVersions [ version ] ;
106
112
imageMagick . resize ( {
@@ -110,13 +116,12 @@ module.exports = function (options) {
110
116
dstPath : options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ,
111
117
customArgs : opts . imageArgs || [ '-auto-orient' ]
112
118
} , finish ) ;
119
+ }
113
120
} ) ;
114
121
}
115
122
}
116
123
117
- if ( ! fs . existsSync ( options . uploadDir ( ) + '/' ) )
118
- mkdirp . sync ( options . uploadDir ( ) + '/' ) ;
119
-
124
+ mkdirp ( options . uploadDir ( ) + '/' , function ( err , made ) {
120
125
counter ++ ;
121
126
fs . rename ( file . path , options . uploadDir ( ) + '/' + fileInfo . name , function ( err ) {
122
127
if ( ! err ) {
@@ -127,15 +132,17 @@ module.exports = function (options) {
127
132
var os = fs . createWriteStream ( options . uploadDir ( ) + '/' + fileInfo . name ) ;
128
133
is . on ( 'end' , function ( err ) {
129
134
if ( ! err ) {
130
- fs . unlinkSync ( file . path ) ;
135
+ fs . unlink ( file . path ) ;
131
136
generatePreviews ( ) ;
132
137
}
133
138
finish ( ) ;
134
139
} ) ;
135
140
is . pipe ( os ) ;
136
141
}
137
142
} ) ;
143
+ } ) ;
138
144
}
145
+ }
139
146
} )
140
147
. on ( 'aborted' , function ( ) {
141
148
_ . each ( tmpFiles , function ( file ) {
0 commit comments