示例#1
0
        public async Task <Stream> GetIcon(Int32 iconid = 0, string iconhash = "")
        {
            string sico = iconhash;

            if (iconid > 0)
            {
                sico = iconid.ToString();
            }

            //Stream bResult;
            byte[] bCache;

            //Try to get value from Memory
            if (_cache.TryGetValue("ico-" + sico, out bCache))
            {
                var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(95)); //cache icon for other 90 Minutes
                _cache.Set("ico-" + sico, bCache, cacheEntryOptions);

                return(new MemoryStream(bCache));
            }

            //Try to load Icon from Disk
            if (File.Exists(Path.Combine(Settings["icons"], sico + ".jpg")))
            {
                bCache = File.ReadAllBytes(Path.Combine(Settings["icons"], sico + ".jpg"));

                var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(95)); //cache icon for other 90 Minutes
                _cache.Set("ico-" + sico, bCache, cacheEntryOptions);

                return(new MemoryStream(bCache));
            }

            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    Stream x = await RZRestAPIv2.GetIcon(iconid, iconhash);

                    await x.CopyToAsync(ms);

                    bCache = ms.ToArray();
                }

                if (bCache.Length > 0)
                {
                    var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(95)); //cache icon for 90 Minutes
                    _cache.Set("ico-" + sico, bCache, cacheEntryOptions);

                    try
                    {
                        File.WriteAllBytes(Path.Combine(Settings["icons"], sico + ".jpg"), bCache);
                    }
                    catch { }
                }

                return(new MemoryStream(bCache));
            }
            catch { }

            return(null);
        }
        public async Task <Stream> GetIcon(Int32 iconid = 0, string iconhash = "", string customerid = "", int size = 0)
        {
            string sico = iconhash;

            if (iconid > 0)
            {
                sico = iconid.ToString();
            }

            //Stream bResult;
            byte[] bCache;

            //Try to get value from Memory
            if (_cache.TryGetValue("ico-" + size.ToString() + sico, out bCache))
            {
                var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(95)); //cache icon for other 90 Minutes
                _cache.Set("ico-" + size.ToString() + sico, bCache, cacheEntryOptions);

                return(new MemoryStream(bCache));
            }

            //Try to load Icon from Disk
            if (File.Exists(Path.Combine(Settings["icons"], sico + "_" + size.ToString() + ".jpg")))
            {
                bCache = File.ReadAllBytes(Path.Combine(Settings["icons"], sico + "_" + size.ToString() + ".jpg"));

                var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(95)); //cache icon for other 90 Minutes
                _cache.Set("ico-" + size.ToString() + sico, bCache, cacheEntryOptions);

                return(new MemoryStream(bCache));
            }

            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    Stream x = await RZRestAPIv2.GetIcon(iconid, iconhash, size);

                    await x.CopyToAsync(ms);

                    bCache = ms.ToArray();

                    if (size > 0)
                    {
                        using (Image image = Image.Load(bCache))
                        {
                            image.Mutate(i => i.Resize(new ResizeOptions {
                                Size = new Size(size, size)
                            }));
                            using (var imgs = new MemoryStream())
                            {
                                var imageEncoder = image.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance);
                                image.Save(imgs, imageEncoder);
                                bCache = imgs.ToArray();
                            }
                        }
                    }
                }

                if (bCache.Length > 0)
                {
                    var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(95)); //cache icon for 90 Minutes
                    _cache.Set("ico-" + size.ToString() + sico, bCache, cacheEntryOptions);

                    try
                    {
                        File.WriteAllBytes(Path.Combine(Settings["icons"], sico + "_" + size.ToString() + ".jpg"), bCache);
                    }
                    catch { }
                }

                return(new MemoryStream(bCache));
            }
            catch { }

            return(null);
        }