替换下载模块

This commit is contained in:
luchenqun 2017-03-09 10:55:16 +08:00
parent 79c24a146e
commit 5591426a92
3 changed files with 20 additions and 86 deletions

View File

@ -1,32 +0,0 @@
var request = require('request');
var fs = require('fs');
var download = function(url, dest, cb) {
var file = fs.createWriteStream(dest);
var sendReq = request.get(url);
var error = null;
sendReq.on('response', function(response) {
if (response.statusCode !== 200) {
error = 'Response status was ' + response.statusCode;
}
});
sendReq.on('error', function(err) {
fs.unlink(dest);
error = err
});
sendReq.pipe(file);
file.on('finish', function() {
file.close(cb(error));
});
file.on('error', function(err) {
fs.unlink(dest);
error = err.message;
});
};
module.exports = download;

View File

@ -11,6 +11,7 @@
"cookie-parser": "~1.4.3", "cookie-parser": "~1.4.3",
"crypto": "0.0.3", "crypto": "0.0.3",
"debug": "~2.2.0", "debug": "~2.2.0",
"download": "^5.0.3",
"ejs": "~2.4.1", "ejs": "~2.4.1",
"express": "~4.13.4", "express": "~4.13.4",
"express-session": "^1.14.1", "express-session": "^1.14.1",

View File

@ -4,7 +4,7 @@ var crypto = require('crypto');
var read = require('node-readability'); var read = require('node-readability');
var db = require('../database/db.js'); var db = require('../database/db.js');
var parseHtml = require('../common/parse_html.js'); var parseHtml = require('../common/parse_html.js');
var download = require('../common/download.js'); var download = require('download');
var multer = require('multer'); var multer = require('multer');
var webshot = require('webshot'); var webshot = require('webshot');
var fs = require('fs'); var fs = require('fs');
@ -935,57 +935,22 @@ api.getSnapFaviconByTimer = function() {
} }
}); });
db.updateBookmarkFaviconState(id, today + 31); db.updateBookmarkFaviconState(id, today + 31);
return; }else{
}
var faviconUrl = "http://g.soz.im/" + url + "/cdn.ico" var faviconUrl = "http://g.soz.im/" + url + "/cdn.ico"
if (faviconUrl) { download(faviconUrl).then(data => {
download(faviconUrl, faviconPath, function(err) { fs.writeFileSync(faviconPath, data);
db.updateBookmarkFaviconState(id, -1);
}).catch((err) => {
var newFaviconState = -1; var newFaviconState = -1;
if (err) {
console.log("boomarkid = " + id + ", download over", err) console.log("boomarkid = " + id + ", download over", err)
if (faviconState == 0 || faviconState == 1) { if (faviconState == 0 || faviconState == 1) {
newFaviconState = faviconState + 1; newFaviconState = faviconState + 1;
} else if (faviconState == 2) { } else if (faviconState == 2) {
newFaviconState = today + 31; newFaviconState = today + 31;
} }
}
db.updateBookmarkFaviconState(id, newFaviconState); db.updateBookmarkFaviconState(id, newFaviconState);
}); });
} else {
db.updateBookmarkFaviconState(id, today + 31);
} }
// 获取favicon(由于获取不到有些图标,暂时放弃)
// fs.exists(faviconPath, function(exists) {
// if (exists) {
// if (faviconState != -1) {
// db.updateBookmarkFaviconState(id, -1);
// }
// } else {
// if (!/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url)) {
// db.updateBookmarkFaviconState(id, today + 31);
// return;
// }
// favicon(url, function(err, faviconUrl) {
// if (faviconUrl) {
// download(faviconUrl, faviconPath, function(err) {
// var newFaviconState = -1;
// if (err) {
// console.log("boomarkid = " + id + ", download over", err)
// if (faviconState == 0 || faviconState == 1) {
// newFaviconState = faviconState + 1;
// } else if (faviconState == 2) {
// newFaviconState = today + 31;
// }
// }
// db.updateBookmarkFaviconState(id, newFaviconState);
// });
// } else {
// db.updateBookmarkFaviconState(id, today + 31);
// }
// });
// }
// });
} }
}) })
.catch((err) => console.log('getBookmarkWaitSnap err', err)); .catch((err) => console.log('getBookmarkWaitSnap err', err));