@@ -4,7 +4,8 @@ var EventEmitter = require('events').EventEmitter,
44 formidable = require ( 'formidable' ) ,
55 imageMagick = require ( 'imagemagick' ) ,
66 mkdirp = require ( 'mkdirp' ) ,
7- _ = require ( 'lodash' ) ;
7+ _ = require ( 'lodash' ) ,
8+ async = require ( 'async' ) ;
89
910module . exports = function ( options ) {
1011
@@ -34,19 +35,25 @@ module.exports = function (options) {
3435 this . noCache ( ) ;
3536 var files = [ ] ;
3637 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+ } ) ;
5057 } , this ) ) ;
5158 } ;
5259
@@ -87,7 +94,8 @@ module.exports = function (options) {
8794 } )
8895 . on ( 'file' , function ( name , file ) {
8996 var fileInfo = map [ path . basename ( file . path ) ] ;
90- if ( fs . existsSync ( file . path ) ) {
97+ fs . exists ( file . path , function ( exists ) {
98+ if ( exists ) {
9199 fileInfo . size = file . size ;
92100 if ( ! fileInfo . validate ( ) ) {
93101 fs . unlink ( file . path ) ;
@@ -98,9 +106,7 @@ module.exports = function (options) {
98106 if ( options . imageTypes . test ( fileInfo . name ) ) {
99107 _ . each ( options . imageVersions , function ( value , version ) {
100108 // 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 ) {
104110 counter ++ ;
105111 var opts = options . imageVersions [ version ] ;
106112 imageMagick . resize ( {
@@ -110,13 +116,12 @@ module.exports = function (options) {
110116 dstPath : options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ,
111117 customArgs : opts . imageArgs || [ '-auto-orient' ]
112118 } , finish ) ;
119+ }
113120 } ) ;
114121 }
115122 }
116123
117- if ( ! fs . existsSync ( options . uploadDir ( ) + '/' ) )
118- mkdirp . sync ( options . uploadDir ( ) + '/' ) ;
119-
124+ mkdirp ( options . uploadDir ( ) + '/' , function ( err , made ) {
120125 counter ++ ;
121126 fs . rename ( file . path , options . uploadDir ( ) + '/' + fileInfo . name , function ( err ) {
122127 if ( ! err ) {
@@ -127,15 +132,17 @@ module.exports = function (options) {
127132 var os = fs . createWriteStream ( options . uploadDir ( ) + '/' + fileInfo . name ) ;
128133 is . on ( 'end' , function ( err ) {
129134 if ( ! err ) {
130- fs . unlinkSync ( file . path ) ;
135+ fs . unlink ( file . path ) ;
131136 generatePreviews ( ) ;
132137 }
133138 finish ( ) ;
134139 } ) ;
135140 is . pipe ( os ) ;
136141 }
137142 } ) ;
143+ } ) ;
138144 }
145+ }
139146 } )
140147 . on ( 'aborted' , function ( ) {
141148 _ . each ( tmpFiles , function ( file ) {
0 commit comments