From 79c24a146e41302cffd198c397e95d104e2e5199 Mon Sep 17 00:00:00 2001 From: luchenqun Date: Thu, 9 Mar 2017 09:44:19 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E8=8E=B7=E5=8F=96favicon?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/api.js | 88 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 29 deletions(-) diff --git a/routes/api.js b/routes/api.js index 2b290ac..ebf51b4 100644 --- a/routes/api.js +++ b/routes/api.js @@ -925,37 +925,67 @@ api.getSnapFaviconByTimer = function() { } }); - // 获取favicon - fs.exists(faviconPath, function(exists) { - if (exists) { - if (faviconState != -1) { - db.updateBookmarkFaviconState(id, -1); + if (!/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url)) { + fs.exists(faviconPath, function(exists) { + if (!exists) { + var sourceFile = './public/images/favicon/default.ico'; + var readStream = fs.createReadStream(sourceFile); + var writeStream = fs.createWriteStream(faviconPath); + readStream.pipe(writeStream); } - } 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); + }); + db.updateBookmarkFaviconState(id, today + 31); + return; + } + var faviconUrl = "http://g.soz.im/"+ url +"/cdn.ico" + 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); + } + + // 获取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));