diff --git a/src/Icons/Controllers/IconController.cs b/src/Icons/Controllers/IconController.cs index a7713ca22d..f0ba3ee173 100644 --- a/src/Icons/Controllers/IconController.cs +++ b/src/Icons/Controllers/IconController.cs @@ -1,57 +1,65 @@ using System; using System.Net.Http; using System.Threading.Tasks; -using Icons.Models; -using Microsoft.AspNetCore.Hosting; +using Bit.Icons.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; -namespace Icons.Controllers +namespace Bit.Icons.Controllers { - [Route("[controller]")] + [Route("")] public class IconController : Controller { - private readonly IMemoryCache _cache; + private readonly IMemoryCache _memoryCache; public IconController(IMemoryCache memoryCache) { - this._cache = memoryCache; + _memoryCache = memoryCache; } - [HttpGet] + [HttpGet("")] public async Task Get([FromQuery] string domain) { - var uri = BuildUrl(domain); + if(!domain.StartsWith("http://") || !domain.StartsWith("https://")) + { + domain = "http://" + domain; + } - Icon icon = await _cache.GetOrCreateAsync(domain, async entry => + if(!Uri.TryCreate(domain, UriKind.Absolute, out Uri uri)) + { + return new BadRequestResult(); + } + + var iconUrl = BuildIconUrl(uri); + var icon = await _memoryCache.GetOrCreateAsync(domain, async entry => { entry.AbsoluteExpiration = DateTime.Now.AddDays(1); var httpClient = new HttpClient(); - var response = await httpClient.GetAsync(uri); - - if (!response.IsSuccessStatusCode) + var response = await httpClient.GetAsync(iconUrl); + if(!response.IsSuccessStatusCode) { return null; } - return new Icon( - await response.Content.ReadAsByteArrayAsync(), - response.Content.Headers.ContentType.MediaType - ); + return new Icon + { + Image = await response.Content.ReadAsByteArrayAsync(), + Format = response.Content.Headers.ContentType.MediaType + }; }); - if (icon == null) + if(icon == null) { - return NotFound("Cannot load the icon."); + return new NotFoundResult(); } return new FileContentResult(icon.Image, icon.Format); } - private static string BuildUrl(string domain) + private static string BuildIconUrl(Uri uri) { - return $"https://icons.bitwarden.com/icon?url={domain}&size=16..24..200"; + return $"https://icons.bitwarden.com/icon?url={uri.Host}&size=16..24..200"; } } } diff --git a/src/Icons/Icons.csproj b/src/Icons/Icons.csproj index 89f574c340..171d8b6267 100644 --- a/src/Icons/Icons.csproj +++ b/src/Icons/Icons.csproj @@ -2,6 +2,7 @@ netcoreapp2.0 + Bit.Icons diff --git a/src/Icons/Models/Icon.cs b/src/Icons/Models/Icon.cs index 37c8733e0e..cca6d78d55 100644 --- a/src/Icons/Models/Icon.cs +++ b/src/Icons/Models/Icon.cs @@ -1,18 +1,8 @@ -using System; - -namespace Icons.Models +namespace Bit.Icons.Models { - [Serializable] public class Icon { - public byte[] Image { get; } - - public string Format { get; } - - public Icon(byte[] image, string format) - { - this.Image = image; - this.Format = format; - } + public byte[] Image { get; set; } + public string Format { get; set; } } } diff --git a/src/Icons/Program.cs b/src/Icons/Program.cs index bfcce738a0..91299f6e73 100644 --- a/src/Icons/Program.cs +++ b/src/Icons/Program.cs @@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; -namespace Icons +namespace Bit.Icons { public class Program { diff --git a/src/Icons/Properties/launchSettings.json b/src/Icons/Properties/launchSettings.json index 7f34cca0e7..61929c7b71 100644 --- a/src/Icons/Properties/launchSettings.json +++ b/src/Icons/Properties/launchSettings.json @@ -11,7 +11,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "icon?domain=bitwarden.com", + "launchUrl": "?domain=bitwarden.com", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -19,7 +19,7 @@ "Icons": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "icon?domain=bitwarden.com", + "launchUrl": "?domain=bitwarden.com", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, diff --git a/src/Icons/Startup.cs b/src/Icons/Startup.cs index 6f599f9ab9..bd5302930d 100644 --- a/src/Icons/Startup.cs +++ b/src/Icons/Startup.cs @@ -1,15 +1,10 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -namespace Icons +namespace Bit.Icons { public class Startup { @@ -20,21 +15,18 @@ namespace Icons public IConfiguration Configuration { get; } - // 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(); } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { - if (env.IsDevelopment()) + if(env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } - app.UseMvc(); } }