@@ -7,13 +7,14 @@ import * as supertest from 'supertest'
7
7
import * as multer from 'multer'
8
8
import * as aws from 'aws-sdk'
9
9
import * as crypto from 'crypto'
10
+ import * as sharp from 'sharp'
10
11
11
12
import multerSharp from '../src/main'
12
13
const config = {
13
14
uploads : {
14
15
aws : {
15
16
Bucket : process . env . AWS_BUCKET ,
16
- ACL : 'public-read ' ,
17
+ ACL : 'private ' ,
17
18
secretAccessKey : process . env . AWS_SECRET_ACCESS_KEY ,
18
19
accessKeyId : process . env . AWS_ACCESS_KEY_ID ,
19
20
region : process . env . AWS_REGION ,
@@ -54,11 +55,9 @@ const storage = multerSharp({
54
55
width : 400 ,
55
56
height : 400 ,
56
57
options : {
57
- kernel : 'lanczos2' ,
58
- interpolator : 'nohalo' ,
58
+ kernel : sharp . kernel . lanczos2 ,
59
59
} ,
60
60
} ,
61
- max : true ,
62
61
} )
63
62
const upload = multer ( { storage } )
64
63
const storage2 = multerSharp ( {
@@ -97,10 +96,6 @@ const storage4 = multerSharp({
97
96
Key : `${ config . uploads . aws . Bucket } /test/${ Date . now ( ) } -myPic` ,
98
97
ACL : config . uploads . aws . ACL ,
99
98
resize : { width : 200 } ,
100
- crop : 16 , // crop strategy
101
- background : { r : 0 , g : 0 , b : 100 , alpha : 0 } ,
102
- withoutEnlargement : true ,
103
- ignoreAspectRatio : true ,
104
99
trim : 50 ,
105
100
flatten : true ,
106
101
extend : { top : 10 , bottom : 20 , left : 10 , right : 10 } ,
@@ -129,13 +124,6 @@ const storage5 = multerSharp({
129
124
Key : `${ config . uploads . aws . Bucket } /test/${ Date . now ( ) } -myPic` ,
130
125
ACL : config . uploads . aws . ACL ,
131
126
resize : { width : 400 , height : 400 } ,
132
- crop : 'north' ,
133
- background : { r : 0 , g : 0 , b : 0 , alpha : 0 } ,
134
- embed : true ,
135
- max : true ,
136
- min : true ,
137
- withoutEnlargement : true ,
138
- ignoreAspectRatio : true ,
139
127
extract : { left : 0 , top : 2 , width : 50 , height : 100 } ,
140
128
trim : 50 ,
141
129
flatten : true ,
@@ -152,6 +140,7 @@ const storage5 = multerSharp({
152
140
toFormat : 'jpeg' ,
153
141
withMetadata : {
154
142
orientation : 4 ,
143
+ chromaSubsampling : '4:4:4' ,
155
144
} ,
156
145
convolve : {
157
146
width : 3 ,
@@ -172,7 +161,6 @@ const storage6 = multerSharp({
172
161
width : 400 ,
173
162
height : 400 ,
174
163
} ,
175
- max : true ,
176
164
extract : { left : 0 , top : 2 , width : 400 , height : 400 } ,
177
165
} )
178
166
const upload6 = multer ( { storage : storage6 } ) ;
@@ -181,7 +169,7 @@ const storage8 = multerSharp({
181
169
s3,
182
170
Bucket : config . uploads . aws . Bucket ,
183
171
Key : `${ config . uploads . aws . Bucket } /test/${ Date . now ( ) } -myPic` ,
184
- // ACL: config.uploads.aws.ACL,
172
+ ACL : config . uploads . aws . ACL ,
185
173
multiple : true ,
186
174
resize : [
187
175
// { suffix: 'xlg', width: 1200, height: 1200 },
@@ -249,14 +237,14 @@ app.post('/upload', (req, res, next) => {
249
237
250
238
// express setup
251
239
app . post ( '/uploadwitherrorkey' , ( req , res , next ) => {
252
- upload2 . single ( 'myPic' ) ( req , res , ( errorFile ) => {
240
+ upload2 . single ( 'myPic' ) ( req , res , ( errorFile ) => {
253
241
lastReq = req
254
242
lastRes = res
255
243
// console.log(errorFile.stack);
256
244
res . status ( 400 ) . json ( { message : errorFile . message } )
257
245
258
246
next ( )
259
- } )
247
+ } )
260
248
} ) ;
261
249
262
250
// express setup
@@ -278,10 +266,10 @@ app.post('/uploadwithsharpsetting', upload4.single('myPic'), (req, res, next) =>
278
266
// // express setup
279
267
app . post ( '/uploadanddelete' , ( req , res , next ) => {
280
268
upload5 . single ( 'myPic' ) ( req , res , ( err ) => {
281
- if ( err ) { next ( err ) }
269
+ if ( err ) { next ( err ) }
282
270
storage5 . _removeFile ( req , req . file , ( err ) => {
283
271
// eslint-disable-line no-underscore-dangle
284
- if ( err ) { next ( err ) }
272
+ if ( err ) { next ( err ) }
285
273
res . sendStatus ( 200 )
286
274
next ( )
287
275
} )
@@ -330,15 +318,22 @@ app.post('/uploadfilewithdefaultkey', upload11.single('myFile'), (req, res, next
330
318
// express setup
331
319
app . post (
332
320
'/uploadwithmultiplesize' ,
333
- upload8 . single ( 'myPic' ) ,
334
321
( req , res , next ) => {
335
- lastReq = req
336
- lastRes = res
337
-
338
- if ( lastReq && lastReq . file ) {
322
+ upload8 . single ( 'myPic' ) ( req , res , ( err ) => {
323
+ lastReq = req
324
+ lastRes = res
325
+ if ( err ) { throw err } ;
339
326
res . sendStatus ( 200 )
340
- }
341
- next ( )
327
+ next ( )
328
+ } )
329
+ // lastReq = req
330
+ // lastRes = res
331
+ // console.log('req ', req.file)
332
+
333
+ // if (lastReq && lastReq.file) {
334
+ // res.sendStatus(200)
335
+ // }
336
+ // next()
342
337
}
343
338
)
344
339
@@ -396,7 +391,7 @@ describe('S3Storage', () => {
396
391
} )
397
392
398
393
describe ( 'Upload test' , ( ) => {
399
- // this.timeout (15000);
394
+ jest . setTimeout ( 15000 ) ;
400
395
it ( 'initial server' , ( done ) => {
401
396
supertest ( app )
402
397
. get ( '/book' )
@@ -408,6 +403,8 @@ describe('Upload test', () => {
408
403
. attach ( 'myPic' , '__tests__/nodejs-512.png' )
409
404
. end ( ( err ) => {
410
405
const file = lastReq . file
406
+ // console.log('filee ', file);
407
+ expect ( file ) . toBeDefined ( )
411
408
expect ( file ) . toHaveProperty ( 'Location' )
412
409
expect ( file ) . toHaveProperty ( 'fieldname' ) ;
413
410
expect ( file ) . toHaveProperty ( 'encoding' ) ;
@@ -418,6 +415,50 @@ describe('Upload test', () => {
418
415
} )
419
416
// .expect(200, done);
420
417
} )
418
+ it ( 'return a req.file with multiple sizes' , ( done ) => {
419
+ // jest.setTimeout(done, 1000);
420
+ supertest ( app )
421
+ . post ( '/uploadwithmultiplesize' )
422
+ . attach ( 'myPic' , '__tests__/nodejs-512.png' )
423
+ . end ( ( ) => {
424
+ const file = lastReq . file
425
+ expect ( file ) . toHaveProperty ( 'original' )
426
+ expect ( file ) . toHaveProperty ( 'md' )
427
+ expect ( file ) . toHaveProperty ( 'sm' )
428
+ expect ( file ) . toHaveProperty ( 'xs' )
429
+ expect ( file ) . toHaveProperty ( 'fieldname' ) ;
430
+ expect ( file ) . toHaveProperty ( 'encoding' ) ;
431
+ expect ( file ) . toHaveProperty ( 'mimetype' ) ;
432
+ expect ( file ) . toHaveProperty ( 'originalname' ) ;
433
+ expect ( file . xs ) . toHaveProperty ( 'Key' )
434
+ expect ( file . md ) . toHaveProperty ( 'Key' )
435
+ expect ( file . sm ) . toHaveProperty ( 'Key' )
436
+ expect ( file . original ) . toHaveProperty ( 'Key' )
437
+ expect ( file . xs ) . toHaveProperty ( 'Location' )
438
+ expect ( file . md ) . toHaveProperty ( 'Location' )
439
+ expect ( file . sm ) . toHaveProperty ( 'Location' )
440
+ expect ( file . original ) . toHaveProperty ( 'Location' )
441
+ done ( )
442
+ } )
443
+ } )
444
+
445
+ it ( 'upload file without Key' , ( done ) => {
446
+ supertest ( app )
447
+ . post ( '/uploadfilewithdefaultkey' )
448
+ . attach ( 'myFile' , '.travis.yml' )
449
+ . end ( ( err , res ) => {
450
+ const { file } = lastReq
451
+ expect ( file ) . toHaveProperty ( 'Key' )
452
+ expect ( file ) . toHaveProperty ( 'fieldname' )
453
+ expect ( file ) . toHaveProperty ( 'encoding' )
454
+ expect ( file ) . toHaveProperty ( 'mimetype' )
455
+ expect ( file ) . toHaveProperty ( 'originalname' )
456
+ expect ( file . mimetype ) . toMatch ( 'text/yaml' )
457
+ expect ( file . fieldname ) . toMatch ( 'myFile' )
458
+ expect ( file . Location ) . toMatch ( 'aws' )
459
+ done ( )
460
+ } )
461
+ } )
421
462
422
463
// it('returns a req.file with the Google Cloud Storage filename and path', (done) => {
423
464
// supertest(app)
@@ -508,49 +549,6 @@ describe('Upload test', () => {
508
549
done ( )
509
550
} )
510
551
} ) ;
511
- it ( 'return a req.file with multiple sizes' , ( done ) => {
512
- // jest.setTimeout(done, 1000);
513
- supertest ( app )
514
- . post ( '/uploadwithmultiplesize' )
515
- . attach ( 'myPic' , '__tests__/nodejs-512.png' )
516
- . end ( ( ) => {
517
- const file = lastReq . file
518
- expect ( file ) . toHaveProperty ( 'original' )
519
- expect ( file ) . toHaveProperty ( 'md' )
520
- expect ( file ) . toHaveProperty ( 'sm' )
521
- expect ( file ) . toHaveProperty ( 'xs' )
522
- expect ( file ) . toHaveProperty ( 'fieldname' ) ;
523
- expect ( file ) . toHaveProperty ( 'encoding' ) ;
524
- expect ( file ) . toHaveProperty ( 'mimetype' ) ;
525
- expect ( file ) . toHaveProperty ( 'originalname' ) ;
526
- expect ( file . xs ) . toHaveProperty ( 'Key' )
527
- expect ( file . md ) . toHaveProperty ( 'Key' )
528
- expect ( file . sm ) . toHaveProperty ( 'Key' )
529
- expect ( file . original ) . toHaveProperty ( 'Key' )
530
- expect ( file . xs ) . toHaveProperty ( 'Location' )
531
- expect ( file . md ) . toHaveProperty ( 'Location' )
532
- expect ( file . sm ) . toHaveProperty ( 'Location' )
533
- expect ( file . original ) . toHaveProperty ( 'Location' )
534
- done ( )
535
- } )
536
- } )
537
- it ( 'upload file without Key' , ( done ) => {
538
- supertest ( app )
539
- . post ( '/uploadfilewithdefaultkey' )
540
- . attach ( 'myFile' , '.travis.yml' )
541
- . end ( ( err , res ) => {
542
- const { file } = lastReq
543
- expect ( file ) . toHaveProperty ( 'Key' )
544
- expect ( file ) . toHaveProperty ( 'fieldname' )
545
- expect ( file ) . toHaveProperty ( 'encoding' )
546
- expect ( file ) . toHaveProperty ( 'mimetype' )
547
- expect ( file ) . toHaveProperty ( 'originalname' )
548
- expect ( file . mimetype ) . toMatch ( 'text/yaml' )
549
- expect ( file . fieldname ) . toMatch ( 'myFile' )
550
- expect ( file . Location ) . toMatch ( 'aws' )
551
- done ( )
552
- } )
553
- } ) ;
554
552
// it('upload multisize and return error, cause transform/resize error', (done) => {
555
553
// supertest(app)
556
554
// .post('/uploadwithmultiplesizetransformerror')
0 commit comments