@@ -5,17 +5,18 @@ import {
5
5
getURLFile
6
6
} from '../../utils/common'
7
7
import { IPicGo , IPathTransformedImgInfo , IImgInfo , IImgSize } from '../../types'
8
+ import dayjs from 'dayjs'
8
9
9
10
const handle = async ( ctx : IPicGo ) : Promise < IPicGo > => {
10
11
const results : IImgInfo [ ] = ctx . output
11
- await Promise . all ( ctx . input . map ( async ( item : string , index : number ) => {
12
+ await Promise . all ( ctx . input . map ( async ( item : string | Buffer , index : number ) => {
12
13
let info : IPathTransformedImgInfo
13
14
if ( Buffer . isBuffer ( item ) ) {
14
15
info = {
15
16
success : true ,
16
17
buffer : item ,
17
- fileName : 'temporary.png' ,
18
- extname : '.png'
18
+ fileName : '' , // will use getImageSize result
19
+ extname : '' // will use getImageSize result
19
20
}
20
21
} else if ( isUrl ( item ) ) {
21
22
info = await getURLFile ( item , ctx )
@@ -24,26 +25,31 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
24
25
}
25
26
if ( info . success && info . buffer ) {
26
27
const imgSize = getImgSize ( ctx , info . buffer , item )
28
+ const extname = info . extname || imgSize . extname || '.png'
27
29
results [ index ] = {
28
30
buffer : info . buffer ,
29
- fileName : info . fileName ,
31
+ fileName : info . fileName || ` ${ dayjs ( ) . format ( 'YYYYMMDDHHmmss' ) } ${ extname } }` ,
30
32
width : imgSize . width ,
31
33
height : imgSize . height ,
32
- extname : info . extname
34
+ extname
33
35
}
34
36
} else {
35
- throw new Error ( info . reason )
37
+ ctx . log . error ( info . reason )
36
38
}
37
39
} ) )
38
40
// remove empty item
39
41
ctx . output = results . filter ( item => item )
40
42
return ctx
41
43
}
42
44
43
- const getImgSize = ( ctx : IPicGo , file : Buffer , path : string ) : IImgSize => {
45
+ const getImgSize = ( ctx : IPicGo , file : Buffer , path : string | Buffer ) : IImgSize => {
44
46
const imageSize = getImageSize ( file )
45
47
if ( ! imageSize . real ) {
46
- ctx . log . warn ( `can't get ${ path } 's image size` )
48
+ if ( typeof path === 'string' ) {
49
+ ctx . log . warn ( `can't get ${ path } 's image size` )
50
+ } else {
51
+ ctx . log . warn ( 'can\'t get image size' )
52
+ }
47
53
ctx . log . warn ( 'fallback to 200 * 200' )
48
54
}
49
55
return imageSize
0 commit comments