-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
29 lines (23 loc) · 798 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const path = require('path');
const request = require('./lib/request');
module.exports.image = ({ extractFilename = true, ...options } = {}) => {
if (!options.url) {
return Promise.reject(new Error('The options.url is required'));
}
if (!options.dest) {
return Promise.reject(new Error('The options.dest is required'));
}
if (extractFilename) {
if (!path.extname(options.dest)) {
const url = new URL(options.url);
const pathname = url.pathname;
const basename = path.basename(pathname);
const decodedBasename = decodeURIComponent(basename);
options.dest = path.join(options.dest, decodedBasename);
}
}
if (!path.isAbsolute(options.dest)) {
options.dest = path.resolve(__dirname, options.dest);
}
return request(options);
};