mirror of
https://github.com/bitwarden/server.git
synced 2025-02-26 03:31:34 +01:00
some cleanup on icons
This commit is contained in:
parent
b84473f9eb
commit
164d4e1fb4
@ -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<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);
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RootNamespace>Bit.Icons</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Icons
|
||||
namespace Bit.Icons
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
|
@ -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"
|
||||
},
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user