mirror of
https://github.com/bitwarden/server.git
synced 2025-05-16 17:15:40 -05:00
some cleanup on icons
This commit is contained in:
parent
b84473f9eb
commit
164d4e1fb4
@ -1,57 +1,65 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Icons.Models;
|
using Bit.Icons.Models;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
|
||||||
namespace Icons.Controllers
|
namespace Bit.Icons.Controllers
|
||||||
{
|
{
|
||||||
[Route("[controller]")]
|
[Route("")]
|
||||||
public class IconController : Controller
|
public class IconController : Controller
|
||||||
{
|
{
|
||||||
private readonly IMemoryCache _cache;
|
private readonly IMemoryCache _memoryCache;
|
||||||
|
|
||||||
public IconController(IMemoryCache memoryCache)
|
public IconController(IMemoryCache memoryCache)
|
||||||
{
|
{
|
||||||
this._cache = memoryCache;
|
_memoryCache = memoryCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet("")]
|
||||||
public async Task<IActionResult> Get([FromQuery] string domain)
|
public async Task<IActionResult> 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);
|
entry.AbsoluteExpiration = DateTime.Now.AddDays(1);
|
||||||
|
|
||||||
var httpClient = new HttpClient();
|
var httpClient = new HttpClient();
|
||||||
var response = await httpClient.GetAsync(uri);
|
var response = await httpClient.GetAsync(iconUrl);
|
||||||
|
|
||||||
if(!response.IsSuccessStatusCode)
|
if(!response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Icon(
|
return new Icon
|
||||||
await response.Content.ReadAsByteArrayAsync(),
|
{
|
||||||
response.Content.Headers.ContentType.MediaType
|
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);
|
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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||||
|
<RootNamespace>Bit.Icons</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -1,18 +1,8 @@
|
|||||||
using System;
|
namespace Bit.Icons.Models
|
||||||
|
|
||||||
namespace Icons.Models
|
|
||||||
{
|
{
|
||||||
[Serializable]
|
|
||||||
public class Icon
|
public class Icon
|
||||||
{
|
{
|
||||||
public byte[] Image { get; }
|
public byte[] Image { get; set; }
|
||||||
|
public string Format { get; set; }
|
||||||
public string Format { get; }
|
|
||||||
|
|
||||||
public Icon(byte[] image, string format)
|
|
||||||
{
|
|
||||||
this.Image = image;
|
|
||||||
this.Format = format;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Hosting;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace Icons
|
namespace Bit.Icons
|
||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
"IIS Express": {
|
"IIS Express": {
|
||||||
"commandName": "IISExpress",
|
"commandName": "IISExpress",
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"launchUrl": "icon?domain=bitwarden.com",
|
"launchUrl": "?domain=bitwarden.com",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
}
|
}
|
||||||
@ -19,7 +19,7 @@
|
|||||||
"Icons": {
|
"Icons": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"launchUrl": "icon?domain=bitwarden.com",
|
"launchUrl": "?domain=bitwarden.com",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
},
|
},
|
||||||
|
@ -1,15 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
|
|
||||||
namespace Icons
|
namespace Bit.Icons
|
||||||
{
|
{
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
@ -20,21 +15,18 @@ namespace Icons
|
|||||||
|
|
||||||
public IConfiguration Configuration { get; }
|
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)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddMemoryCache();
|
services.AddMemoryCache();
|
||||||
services.AddMvc();
|
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)
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
||||||
{
|
{
|
||||||
if(env.IsDevelopment())
|
if(env.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
app.UseMvc();
|
app.UseMvc();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user