You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/src/NzbDrone.Core/MediaCover/MediaCoverProxy.cs

73 lines
1.9 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
namespace NzbDrone.Core.MediaCover
{
public interface IMediaCoverProxy
{
string RegisterUrl(string url);
string GetUrl(string hash);
Task<byte[]> GetImage(string hash);
}
public class MediaCoverProxy : IMediaCoverProxy
{
private readonly IHttpClient _httpClient;
private readonly IConfigFileProvider _configFileProvider;
private readonly ICached<string> _cache;
public MediaCoverProxy(IHttpClient httpClient, IConfigFileProvider configFileProvider, ICacheManager cacheManager)
{
_httpClient = httpClient;
_configFileProvider = configFileProvider;
_cache = cacheManager.GetCache<string>(GetType());
}
public string RegisterUrl(string url)
{
if (url.IsNullOrWhiteSpace())
{
return null;
}
var hash = url.SHA256Hash();
_cache.Set(hash, url, TimeSpan.FromHours(24));
_cache.ClearExpired();
var fileName = Path.GetFileName(url);
return _configFileProvider.UrlBase + @"/MediaCoverProxy/" + hash + "/" + fileName;
}
public string GetUrl(string hash)
{
var result = _cache.Find(hash);
if (result == null)
{
throw new KeyNotFoundException("Url no longer in cache");
}
return result;
}
public async Task<byte[]> GetImage(string hash)
{
var url = GetUrl(hash);
var request = new HttpRequest(url);
var response = await _httpClient.GetAsync(request);
return response.ResponseData;
}
}
}