@@ -6,10 +6,10 @@ var EventEmitter = require('events').EventEmitter,
6
6
mkdirp = require ( 'mkdirp' ) ,
7
7
_ = require ( 'lodash' ) ,
8
8
async = require ( 'async' ) ;
9
+ crypto = require ( 'crypto' ) ;
9
10
var doNothing = function ( ) { } ;
10
11
11
12
module . exports = function ( options ) {
12
-
13
13
var FileInfo = require ( './fileinfo' ) (
14
14
_ . extend ( {
15
15
baseDir : options . uploadDir
@@ -64,6 +64,10 @@ module.exports = function (options) {
64
64
} , this ) ) ;
65
65
} ;
66
66
67
+ var isFileExist = function ( filePath ) {
68
+ return fs . existsSync ( filePath ) ;
69
+ }
70
+
67
71
UploadHandler . prototype . post = function ( ) {
68
72
var self = this ,
69
73
form = new formidable . IncomingForm ( ) ,
@@ -89,6 +93,7 @@ module.exports = function (options) {
89
93
this . noCache ( ) ;
90
94
91
95
form . uploadDir = options . tmpDir ;
96
+ form . hash = "sha1" ;
92
97
form
93
98
. on ( 'fileBegin' , function ( name , file ) {
94
99
tmpFiles . push ( file . path ) ;
@@ -128,7 +133,7 @@ module.exports = function (options) {
128
133
imageMagick . resize ( {
129
134
width : opts . width ,
130
135
height : opts . height ,
131
- srcPath : options . uploadDir ( ) + '/' + fileInfo . name ,
136
+ srcPath : options . uploadDir ( ) + '/' + fileInfo . fileDir + '/' + fileInfo . name ,
132
137
dstPath : options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ,
133
138
customArgs : opts . imageArgs || [ '-auto-orient' ]
134
139
} , finish ) ;
@@ -138,22 +143,40 @@ module.exports = function (options) {
138
143
}
139
144
140
145
mkdirp ( options . uploadDir ( ) + '/' , function ( err , made ) {
141
- fs . rename ( file . path , options . uploadDir ( ) + '/' + fileInfo . name , function ( err ) {
142
- if ( ! err ) {
143
- generatePreviews ( ) ;
144
- finish ( ) ;
145
- } else {
146
- var is = fs . createReadStream ( file . path ) ;
147
- var os = fs . createWriteStream ( options . uploadDir ( ) + '/' + fileInfo . name ) ;
148
- is . on ( 'end' , function ( err ) {
149
- if ( ! err ) {
150
- fs . unlink ( file . path ) ;
151
- generatePreviews ( ) ;
152
- }
146
+ //文件后缀
147
+ const fileSuffix = fileInfo . name . substring ( fileInfo . name . lastIndexOf ( '.' ) ) ;
148
+ const fileName = file . hash + fileSuffix ;
149
+ //子目录
150
+ const fileDir = fileName . substring ( 0 , 2 ) ;
151
+ const filePath = path . join ( fileDir + fileName ) ;
152
+ fileInfo . fileDir = fileDir ;
153
+ fileInfo . name = fileName ;
154
+ const isExist = isFileExist ( filePath ) ;
155
+
156
+ if ( isExist ) {
157
+ finish ( ) ;
158
+ return ;
159
+ }
160
+
161
+ mkdirp ( path . join ( options . uploadDir ( ) , fileDir ) , function ( err , made_1 ) {
162
+ const new_path = path . join ( options . uploadDir ( ) , fileDir , fileName ) ;
163
+ fs . rename ( file . path , new_path , function ( err ) {
164
+ if ( ! err ) {
165
+ generatePreviews ( ) ;
153
166
finish ( ) ;
154
- } ) ;
155
- is . pipe ( os ) ;
156
- }
167
+ } else {
168
+ var is = fs . createReadStream ( file . path ) ;
169
+ var os = fs . createWriteStream ( new_path ) ;
170
+ is . on ( 'end' , function ( err ) {
171
+ if ( ! err ) {
172
+ fs . unlink ( file . path ) ;
173
+ generatePreviews ( ) ;
174
+ }
175
+ finish ( ) ;
176
+ } ) ;
177
+ is . pipe ( os ) ;
178
+ }
179
+ } ) ;
157
180
} ) ;
158
181
} ) ;
159
182
}
@@ -199,8 +222,8 @@ module.exports = function (options) {
199
222
200
223
UploadHandler . prototype . initUrls = function ( fileInfo , cb ) {
201
224
var baseUrl = ( options . ssl ? 'https:' : 'http:' ) + '//' + ( options . hostname || this . req . get ( 'Host' ) ) ;
202
- fileInfo . setUrl ( null , baseUrl + options . uploadUrl ( ) ) ;
203
- fileInfo . setUrl ( 'delete' , baseUrl + this . req . originalUrl ) ;
225
+ fileInfo . setUrl ( null , baseUrl + options . uploadUrl ( ) + '/' + fileInfo . fileDir ) ;
226
+ fileInfo . setUrl ( 'delete' , baseUrl + this . req . originalUrl + '/' + fileInfo . fileDir ) ;
204
227
async . each ( Object . keys ( options . imageVersions ) , function ( version , cb ) {
205
228
fs . exists ( options . uploadDir ( ) + '/' + version + '/' + fileInfo . name , function ( exists ) {
206
229
if ( exists ) fileInfo . setUrl ( version , baseUrl + options . uploadUrl ( ) + '/' + version ) ;
0 commit comments