Пример #1
0
        public async Task <IActionResult> Get(string hostname)
        {
            if (string.IsNullOrWhiteSpace(hostname) || !hostname.Contains("."))
            {
                return(new BadRequestResult());
            }

            var url = $"http://{hostname}";

            if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
            {
                return(new BadRequestResult());
            }

            var domain = uri.Host;
            // Convert sub.domain.com => domain.com
            //if(DomainName.TryParseBaseDomain(domain, out var baseDomain))
            //{
            //    domain = baseDomain;
            //}

            var mappedDomain = _domainMappingService.MapDomain(domain);

            if (!_iconsSettings.CacheEnabled || !_memoryCache.TryGetValue(mappedDomain, out Icon icon))
            {
                var result = await _iconFetchingService.GetIconAsync(domain);

                if (result == null)
                {
                    _logger.LogWarning("Null result returned for {0}.", domain);
                    icon = null;
                }
                else
                {
                    icon = result.Icon;
                }

                // Only cache not found and smaller images (<= 50kb)
                if (_iconsSettings.CacheEnabled && (icon == null || icon.Image.Length <= 50012))
                {
                    _logger.LogInformation("Cache icon for {0}.", domain);
                    _memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0),
                        Size     = icon?.Image.Length ?? 0,
                        Priority = icon == null ? CacheItemPriority.High : CacheItemPriority.Normal
                    });
                }
            }

            if (icon == null)
            {
                return(new FileContentResult(_notFoundImage, "image/png"));
            }

            return(new FileContentResult(icon.Image, icon.Format));
        }
Пример #2
0
        public async Task <IActionResult> Get(string hostname)
        {
            if (string.IsNullOrWhiteSpace(hostname) || !hostname.Contains("."))
            {
                return(new BadRequestResult());
            }

            var url = $"http://{hostname}";

            if (!Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
            {
                return(new BadRequestResult());
            }

            var mappedDomain = _domainMappingService.MapDomain(uri.Host);

            if (!_memoryCache.TryGetValue(mappedDomain, out Icon icon))
            {
                var result = await _iconFetchingService.GetIconAsync(mappedDomain);

                if (result == null)
                {
                    return(new NotFoundResult());
                }

                icon = result.Icon;

                // Only cache smaller images (<= 50kb)
                if (icon.Image.Length <= 50012)
                {
                    _memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0)
                    });
                }
            }

            return(new FileContentResult(icon.Image, icon.Format));
        }