Beispiel #1
0
        public static Image GetImage(FeedSource source, INewsFeed feed)
        {
            if (feed == null || source == null)
            {
                return(null);
            }

            if (!source.FeedHasFavicon(feed))
            {
                return(null);
            }

            var id = feed.favicon;
            Uri faviconUri;

            if (Uri.TryCreate(feed.link, UriKind.Absolute, out faviconUri))
            {
                id = "{0}/{1}".FormatWith(faviconUri.Authority, feed.favicon);
            }

            Image image;

            if (_favicons.TryGetValue(id, out image))
            {
                return(image);
            }

            try
            {
                byte[] imageData = source.GetFaviconForFeed(feed);

                if (ImageDataAreResized(ref imageData, feed.favicon, out image))
                {
                    // save resized image data to permanent store:
                    source.SetFaviconForFeed(feed, feed.favicon, imageData);
                }
            }
            catch (Exception ex)
            {
                _log.Error("Failed in GetImage({0}); id = {1}".FormatWith(feed.link, id), ex);
            }

            lock (_favicons)
            {
                if (!_favicons.ContainsKey(id))
                {
                    _favicons.Add(id, image);
                }
            }

            return(image);
        }
Beispiel #2
0
        public static Image GetImage(FeedSource source, INewsFeed feed)
        {
            if (feed == null || source == null)
            {
                return(null);
            }

            if (!source.FeedHasFavicon(feed))
            {
                return(null);
            }

            var id = feed.favicon;
            Uri faviconUri;

            if (Uri.TryCreate(feed.link, UriKind.Absolute, out faviconUri))
            {
                id = $"{faviconUri.Authority}/{feed.favicon}";
            }

            Image image = null;


            try
            {
                if (_favicons.TryGetValue(id, out image))
                {
                    // Ensure they're valid
                    var w = image.Width;
                    var h = image.Height;

                    return(image);
                }

                byte[] imageData = source.GetFaviconForFeed(feed);

                if (ImageDataAreResized(ref imageData, feed.favicon, out image))
                {
                    // save resized image data to permanent store:
                    source.SetFaviconForFeed(feed, feed.favicon, imageData);

                    // Ensure they're valid
                    var w = image.Width;
                    var h = image.Height;
                }
            }
            catch (Exception ex)
            {
                _log.Error($"Failed in GetImage({feed.link}); id = {id}", ex);
            }

            lock (_favicons)
            {
                if (!_favicons.ContainsKey(id) && image != null)
                {
                    _favicons.Add(id, image);
                }
            }

            return(image);
        }