-
Notifications
You must be signed in to change notification settings - Fork 6
/
lib.js
64 lines (59 loc) · 2.13 KB
/
lib.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const path = require("path");
const url = require("url");
const request = require("request-promise-native");
const imageType = require("image-type");
const shortid = require("shortid");
const fs = require("fs");
exports.equativePath = (filePath, relativePath = "") => {
return path.resolve(filePath, "..", relativePath);
};
exports.checkRelative = link => require("is-relative-url")(link); //|| require("is-relative")(link);
exports.checkValid = link =>
require("valid-url").isUri(link) || require("valid-path")(link);
exports.downloader = async function(imgUrl, downloadPath, options = {}) {
// 图片名处理
const urlPathname = url.parse(imgUrl).pathname,
hasExtName = path.extname(urlPathname);
let basename = path.basename(urlPathname);
// 是否覆盖同名图片(对无后缀图片暂时无解)
// todo: 把这段代码转移到外面的pMap上面去
// todo: 明明extract图片链接的时候已经把相对和已存在图片滤掉了,为啥还会显示在命令行
if (
!options.cover &&
hasExtName &&
fs.existsSync(path.join(downloadPath, basename))
)
return;
const res = await request({
method: "GET",
uri: imgUrl,
resolveWithFullResponse: true,
encoding: null // 返回binary格式数据
});
if (res.body && (res.statusCode === 200 || res.statusCode === 201)) {
// 如果url没有文件后缀名,则加上后缀
if (!hasExtName) basename = basename + "." + imageType(res.body).ext;
// 如果存在同名图片,则改名
if (fs.existsSync(path.join(downloadPath, basename)))
basename = shortid.generate() + "-" + basename;
fs.writeFileSync(
path.join(downloadPath, basename),
res.body,
"binary",
err => {
if (err) {
throw new Error(`图片写入失败. URL: ${imgUrl}`);
}
// if (typeof done === "function") {
// done(false, options.dest, res.body);
// }
}
);
return basename;
} else {
if (!res.body) {
throw new Error(`图片下载失败,empty body. URL: ${imgUrl}`);
}
throw new Error(`图片下载失败,${res.statusCode}. URL: ${imgUrl}`);
}
};