Skip to content

Commit

Permalink
favicons loading amended
Browse files Browse the repository at this point in the history
  • Loading branch information
szwacz committed Nov 15, 2013
1 parent 52ae76d commit 9b65f77
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
9 changes: 5 additions & 4 deletions app/controllers/appCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ function AppCtrl($scope, $location, config, feedsService, articlesService, favic

$scope.$on('feedSiteUrlSpecified', function (evt, feed) {
// is siteUrl first time specified try to get its favicon
faviconsService.updateOne(feed)
.then(function () {
$scope.$apply();
});
faviconsService.updateOne(feed);
});

$scope.$on('feedRemoved', function (evt, feed) {
Expand All @@ -70,6 +67,10 @@ function AppCtrl($scope, $location, config, feedsService, articlesService, favic
$scope.allTags = articlesService.allTags;
});

$scope.$on('faviconUpdated', function (evt) {
$scope.$apply();
});

//-----------------------------------------------------
// Preserving window size
//-----------------------------------------------------
Expand Down
18 changes: 14 additions & 4 deletions app/services/faviconsService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

sputnik.factory('faviconsService', function (config, net) {
sputnik.factory('faviconsService', function (config, net, $rootScope) {

var cheerio = require('cheerio');
var urlUtil = require('url');
Expand All @@ -16,6 +16,9 @@ sputnik.factory('faviconsService', function (config, net) {
if (!href) {
href = dom('link[rel="shortcut icon"]').attr('href');
}
if (!href) {
href = dom('link[rel="Shortcut Icon"]').attr('href');
}
if (href && !href.match(/^http/)) { // is relative, so make absolute
href = urlUtil.resolve(siteUrl, href);
}
Expand Down Expand Up @@ -109,6 +112,7 @@ sputnik.factory('faviconsService', function (config, net) {
var filePath = storeDir + '/' + filename;
fs.writeFile(filePath, result.faviconBytes, function (err) {
feed.favicon = filePath;
$rootScope.$broadcast('faviconUpdated');
deferred.resolve();
});
}, function () {
Expand All @@ -121,9 +125,15 @@ sputnik.factory('faviconsService', function (config, net) {
}

function updateMany(feeds) {
feeds.forEach(function (feed) {
updateOne(feed);
});
feeds = feeds.concat();

function next() {
if (feeds.length > 0) {
updateOne(feeds.pop()).then(next);
}
}

next();
}

return {
Expand Down

0 comments on commit 9b65f77

Please sign in to comment.