diff --git a/src/Icons/Controllers/IconController.cs b/src/Icons/Controllers/IconController.cs index 1ba802cd0..a7713ca22 100644 --- a/src/Icons/Controllers/IconController.cs +++ b/src/Icons/Controllers/IconController.cs @@ -1,70 +1,52 @@ using System; -using System.IO; using System.Net.Http; -using System.Runtime.Serialization.Formatters.Binary; using System.Threading.Tasks; using Icons.Models; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Caching.Memory; namespace Icons.Controllers { [Route("[controller]")] public class IconController : Controller { - private readonly IHostingEnvironment _hostingEnvironment; + private readonly IMemoryCache _cache; - public IconController(IHostingEnvironment hostingEnvironment) + public IconController(IMemoryCache memoryCache) { - _hostingEnvironment = hostingEnvironment; + this._cache = memoryCache; } [HttpGet] - public async Task Get([FromQuery] string domain) + public async Task Get([FromQuery] string domain) { var uri = BuildUrl(domain); - var fileName = $"{_hostingEnvironment.ContentRootPath}/cache/{domain}.cache"; - // Attempt to load the icon from the cache. - if (FileExists(fileName)) + Icon icon = await _cache.GetOrCreateAsync(domain, async entry => { - using (Stream stream = System.IO.File.Open(fileName, FileMode.Open)) + entry.AbsoluteExpiration = DateTime.Now.AddDays(1); + + var httpClient = new HttpClient(); + var response = await httpClient.GetAsync(uri); + + if (!response.IsSuccessStatusCode) { - var binaryFormatter = new BinaryFormatter(); - var icon = (Icon)binaryFormatter.Deserialize(stream); - - if (icon.HasNotExpired()) - { - return new FileContentResult(icon.Image, icon.Format); - } + return null; } - } - var httpClient = new HttpClient(); - var response = await httpClient.GetAsync(uri); - - if (!response.IsSuccessStatusCode) - { - throw new Exception("Cannot load the image"); - } - - // Serialize the icon. - using (Stream stream = System.IO.File.Open(fileName, FileMode.Create)) - { - var icon = new Icon( + return new Icon( await response.Content.ReadAsByteArrayAsync(), response.Content.Headers.ContentType.MediaType ); + }); - var binaryFormatter = new BinaryFormatter(); - binaryFormatter.Serialize(stream, icon); - return new FileContentResult(icon.Image, icon.Format); + if (icon == null) + { + return NotFound("Cannot load the icon."); } - } - private static bool FileExists(string fileName) - { - return System.IO.File.Exists(fileName); + return new FileContentResult(icon.Image, icon.Format); } private static string BuildUrl(string domain) diff --git a/src/Icons/Models/Icon.cs b/src/Icons/Models/Icon.cs index 91b24c6ac..37c8733e0 100644 --- a/src/Icons/Models/Icon.cs +++ b/src/Icons/Models/Icon.cs @@ -1,31 +1,18 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Serialization; -using System.Threading.Tasks; namespace Icons.Models { [Serializable] public class Icon { - public byte[] Image { get; } public string Format { get; } - - public DateTime CreatedAt { get; } public Icon(byte[] image, string format) { this.Image = image; this.Format = format; - this.CreatedAt = DateTime.Now; - } - - public bool HasNotExpired() - { - return CreatedAt > DateTime.Now.AddDays(-1); } } } diff --git a/src/Icons/Startup.cs b/src/Icons/Startup.cs index 9b0a1bd19..6f599f9ab 100644 --- a/src/Icons/Startup.cs +++ b/src/Icons/Startup.cs @@ -23,6 +23,7 @@ namespace Icons // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + services.AddMemoryCache(); services.AddMvc(); }