File tree 1 file changed +9
-5
lines changed
1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -60,17 +60,21 @@ let options = {
60
60
} ;
61
61
62
62
async function downloadImage ( url , folder = '.' ) {
63
+ const { headers } = await axios . head ( url ) ;
63
64
const hash = crypto . createHash ( 'sha1' ) . update ( url ) . digest ( 'hex' ) ;
65
+ const existing = fs . readdirSync ( folder ) . find ( e => e . startsWith ( hash ) ) ;
66
+ if ( existing ) {
67
+ return existing ;
68
+ }
69
+
70
+ const [ type , ext ] = headers [ 'content-type' ] . split ( '/' ) ;
71
+ if ( type !== 'image' ) return null ;
72
+
64
73
const filename = `${ hash } .${ ext } ` ;
65
74
const saveTo = path . resolve ( folder , filename ) ;
66
75
67
76
if ( fs . existsSync ( path ) ) return filename ;
68
77
69
- const { headers } = await axios . head ( url ) ;
70
-
71
- const [ type , ext ] = headers [ 'content-type' ] . split ( '/' ) ;
72
- if ( type !== 'image' ) return null ;
73
-
74
78
const writer = fs . createWriteStream ( saveTo ) ;
75
79
const response = await axios ( {
76
80
url,
You can’t perform that action at this time.
0 commit comments