-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimg-download.js
124 lines (100 loc) · 3.21 KB
/
img-download.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var async = require('async'),
path = require('path'),
mkdirp = require('mkdirp'),
http = require('http'),
fs = require('fs');
/**
* 补全域名
* @param {String} host 域名
* @return {String} 补全后的域名
*/
function completeHost(host) {
if (!/^https?:\/\//.test(host)) {
host = 'http://' + host;
}
host += '/wp-content/uploads/';
return host;
}
/**
* 匹配文字内容所包含的图片地址
* @param {String} content 文章内容
* @param {String} host 域名
* @return {Array} 匹配到的图片地址
*/
function matchImgs(content, host) {
var uniqueImgs = {};
var exts = 'jpg|jpeg|png|gif|webp';
var reg = new RegExp(host + '[^('+ exts +')]*\.('+ exts +')', 'g');
return content.match(reg) || [];
}
/**
* [getClearDir description]
* @param {[type]} title [description]
* @return {[type]} [description]
*/
function getClearDir(title) {
return title.replace(/[&\/\\/\:\?\.]/g, '');
}
/**
* 下载图片
* @param {[type]} url [description]
* @return {[type]} [description]
*/
function downloadImg(url, data, next) {
var pathname = 'imgs/' + getClearDir(data.title);
var filename = url.match(/[^/]+\.\w+$/);
var dir = path.join(__dirname, '../../source/', pathname);
filename = filename ? filename.toString() : '';
async.waterfall([
function(callback) {
if (!fs.existsSync(dir)) {
mkdirp(dir, function(err) {
callback();
});
} else {
callback();
}
},
function() {
var filepath = path.join(dir, filename);
if (fs.existsSync(filepath)) {
console.log('file exists: ' + filepath);
data.content = data.content.replace(url, path.join(pathname, filename));
next(null);
} else {
http.get(url, function(res) {
var imgData = "";
res.setEncoding("binary"); //一定要设置response的编码为binary否则会下载下来的图片打不开
res.on("data", function(chunk){
imgData += chunk;
});
res.on("end", function() {
fs.writeFile(filepath, imgData, "binary", function(err){
if(err){
console.log("download fail: "+ url);
} else {
data.content = data.content.replace(url, path.join(pathname, filename));
console.log("download success: "+ url);
}
next(err);
});
});
});
}
}
]);
}
module.exports = function(data, host, callback) {
host = completeHost(host);
var list = matchImgs(data.content, host);
var queue = [];
list.forEach(function (url) {
queue.push(function(next) {
downloadImg(url, data, next);
});
});
async.parallel(queue, function(err, results) {
callback(data);
});
return data;
}