@@ -5,7 +5,7 @@ var EventEmitter = require('events').EventEmitter,
5
5
imageMagick = require ( 'imagemagick' ) ,
6
6
mkdirp = require ( 'mkdirp' ) ,
7
7
_ = require ( 'lodash' ) ,
8
- async = require ( 'async' ) ;
8
+ async = require ( 'async' ) ;
9
9
10
10
module . exports = function ( options ) {
11
11
@@ -35,28 +35,28 @@ module.exports = function (options) {
35
35
this . noCache ( ) ;
36
36
var files = [ ] ;
37
37
fs . readdir ( options . uploadDir ( ) , _ . bind ( function ( err , list ) {
38
- async . each ( list , _ . bind ( function ( name , cb ) {
39
- fs . stat ( options . uploadDir ( ) + '/' + name , _ . bind ( 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 , function ( err ) {
47
- files . push ( fileInfo ) ;
48
- cb ( err ) ;
49
- return ;
50
- } ) ;
51
- }
52
- }
53
- cb ( err ) ;
54
- } , this ) ) ;
55
- } , this ) ,
56
- function ( err ) {
57
- if ( err ) console . log ( err ) ;
58
- this . callback ( { files : files } ) ;
59
- } ) ;
38
+ async . each ( list , _ . bind ( function ( name , cb ) {
39
+ fs . stat ( options . uploadDir ( ) + '/' + name , _ . bind ( 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 , function ( err ) {
47
+ files . push ( fileInfo ) ;
48
+ cb ( err ) ;
49
+ return ;
50
+ } ) ;
51
+ }
52
+ }
53
+ cb ( err ) ;
54
+ } , this ) ) ;
55
+ } , this ) ,
56
+ function ( err ) {
57
+ if ( err ) console . log ( err ) ;
58
+ this . callback ( { files : files } ) ;
59
+ } ) ;
60
60
} , this ) ) ;
61
61
} ;
62
62
@@ -70,15 +70,15 @@ module.exports = function (options) {
70
70
redirect ,
71
71
finish = _ . bind ( function ( ) {
72
72
if ( ! -- counter ) {
73
- async . each ( files , _ . bind ( function ( fileInfo , cb ) {
73
+ async . each ( files , _ . bind ( function ( fileInfo , cb ) {
74
74
this . initUrls ( fileInfo , _ . bind ( function ( err ) {
75
- this . emit ( 'end' , fileInfo ) ;
76
- cb ( err ) ;
77
- } , this ) ) ;
78
- } , this ) ,
79
- _ . bind ( function ( err ) {
80
- this . callback ( { files : files } , redirect ) ;
81
- } , this ) ) ;
75
+ this . emit ( 'end' , fileInfo ) ;
76
+ cb ( err ) ;
77
+ } , this ) ) ;
78
+ } , this ) ,
79
+ _ . bind ( function ( err ) {
80
+ this . callback ( { files : files } , redirect ) ;
81
+ } , this ) ) ;
82
82
}
83
83
} , this ) ;
84
84
@@ -100,58 +100,58 @@ module.exports = function (options) {
100
100
}
101
101
} )
102
102
. on ( 'file' , function ( name , file ) {
103
- counter ++ ;
103
+ counter ++ ;
104
104
var fileInfo = map [ path . basename ( file . path ) ] ;
105
- fs . exists ( file . path , function ( exists ) {
106
- if ( exists ) {
107
- fileInfo . size = file . size ;
108
- if ( ! fileInfo . validate ( ) ) {
109
- fs . unlink ( file . path ) ;
110
- counter -- ;
111
- return ;
112
- }
113
-
114
- var generatePreviews = function ( ) {
115
- if ( options . imageTypes . test ( fileInfo . name ) ) {
116
- _ . each ( options . imageVersions , function ( value , version ) {
117
- counter ++ ;
118
- // creating directory recursive
119
- mkdirp ( options . uploadDir ( ) + '/' + version + '/' , function ( err , made ) {
120
- var opts = options . imageVersions [ version ] ;
121
- imageMagick . resize ( {
122
- width : opts . width ,
123
- height : opts . height ,
124
- srcPath : options . uploadDir ( ) + '/' + fileInfo . name ,
125
- dstPath : options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ,
126
- customArgs : opts . imageArgs || [ '-auto-orient' ]
127
- } , finish ) ;
128
- } ) ;
129
- } ) ;
105
+ fs . exists ( file . path , function ( exists ) {
106
+ if ( exists ) {
107
+ fileInfo . size = file . size ;
108
+ if ( ! fileInfo . validate ( ) ) {
109
+ fs . unlink ( file . path ) ;
110
+ counter -- ;
111
+ return ;
130
112
}
131
- }
132
-
133
- mkdirp ( options . uploadDir ( ) + '/' , function ( err , made ) {
134
- fs . rename ( file . path , options . uploadDir ( ) + '/' + fileInfo . name , function ( err ) {
135
- if ( ! err ) {
136
- generatePreviews ( ) ;
137
- finish ( ) ;
138
- } else {
139
- var is = fs . createReadStream ( file . path ) ;
140
- var os = fs . createWriteStream ( options . uploadDir ( ) + '/' + fileInfo . name ) ;
141
- is . on ( 'end' , function ( err ) {
113
+
114
+ var generatePreviews = function ( ) {
115
+ if ( options . imageTypes . test ( fileInfo . name ) ) {
116
+ _ . each ( options . imageVersions , function ( value , version ) {
117
+ counter ++ ;
118
+ // creating directory recursive
119
+ mkdirp ( options . uploadDir ( ) + '/' + version + '/' , function ( err , made ) {
120
+ var opts = options . imageVersions [ version ] ;
121
+ imageMagick . resize ( {
122
+ width : opts . width ,
123
+ height : opts . height ,
124
+ srcPath : options . uploadDir ( ) + '/' + fileInfo . name ,
125
+ dstPath : options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ,
126
+ customArgs : opts . imageArgs || [ '-auto-orient' ]
127
+ } , finish ) ;
128
+ } ) ;
129
+ } ) ;
130
+ }
131
+ }
132
+
133
+ mkdirp ( options . uploadDir ( ) + '/' , function ( err , made ) {
134
+ fs . rename ( file . path , options . uploadDir ( ) + '/' + fileInfo . name , function ( err ) {
142
135
if ( ! err ) {
143
- fs . unlink ( file . path ) ;
144
136
generatePreviews ( ) ;
137
+ finish ( ) ;
138
+ } else {
139
+ var is = fs . createReadStream ( file . path ) ;
140
+ var os = fs . createWriteStream ( options . uploadDir ( ) + '/' + fileInfo . name ) ;
141
+ is . on ( 'end' , function ( err ) {
142
+ if ( ! err ) {
143
+ fs . unlink ( file . path ) ;
144
+ generatePreviews ( ) ;
145
+ }
146
+ finish ( ) ;
147
+ } ) ;
148
+ is . pipe ( os ) ;
145
149
}
146
- finish ( ) ;
147
150
} ) ;
148
- is . pipe ( os ) ;
149
- }
150
- } ) ;
151
- } ) ;
152
- }
153
- else counter -- ;
154
- } ) ;
151
+ } ) ;
152
+ }
153
+ else counter -- ;
154
+ } ) ;
155
155
} )
156
156
. on ( 'aborted' , function ( ) {
157
157
_ . each ( tmpFiles , function ( file ) {
@@ -188,13 +188,13 @@ module.exports = function (options) {
188
188
var baseUrl = ( options . ssl ? 'https:' : 'http:' ) + '//' + ( options . hostname || this . req . get ( 'Host' ) ) ;
189
189
fileInfo . setUrl ( null , baseUrl + options . uploadUrl ( ) ) ;
190
190
fileInfo . setUrl ( 'delete' , baseUrl + this . req . originalUrl ) ;
191
- async . each ( Object . keys ( options . imageVersions ) , function ( version , cb ) {
192
- fs . exists ( options . uploadDir ( ) + '/' + version + '/' + fileInfo . name , function ( exists ) {
193
- if ( exists ) fileInfo . setUrl ( version , baseUrl + options . uploadUrl ( ) + '/' + version ) ;
194
- cb ( null ) ;
195
- } )
196
- } ,
197
- cb ) ;
191
+ async . each ( Object . keys ( options . imageVersions ) , function ( version , cb ) {
192
+ fs . exists ( options . uploadDir ( ) + '/' + version + '/' + fileInfo . name , function ( exists ) {
193
+ if ( exists ) fileInfo . setUrl ( version , baseUrl + options . uploadUrl ( ) + '/' + version ) ;
194
+ cb ( null ) ;
195
+ } )
196
+ } ,
197
+ cb ) ;
198
198
} ;
199
199
200
200
return UploadHandler ;
0 commit comments