2020-08-15 03:58:42 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2023-09-16 12:07:39 +00:00
|
|
|
using System.Threading.Tasks;
|
2020-08-15 03:58:42 +00:00
|
|
|
using NzbDrone.Common.Cache;
|
2023-10-04 23:42:18 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2020-08-15 03:58:42 +00:00
|
|
|
using NzbDrone.Common.Http;
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaCover
|
|
|
|
{
|
|
|
|
public interface IMediaCoverProxy
|
|
|
|
{
|
|
|
|
string RegisterUrl(string url);
|
|
|
|
|
|
|
|
string GetUrl(string hash);
|
2023-09-16 12:07:39 +00:00
|
|
|
Task<byte[]> GetImage(string hash);
|
2020-08-15 03:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2023-10-04 23:42:18 +00:00
|
|
|
if (url.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-08-15 03:58:42 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-09-16 12:07:39 +00:00
|
|
|
public async Task<byte[]> GetImage(string hash)
|
2020-08-15 03:58:42 +00:00
|
|
|
{
|
|
|
|
var url = GetUrl(hash);
|
|
|
|
|
|
|
|
var request = new HttpRequest(url);
|
2023-09-16 12:07:39 +00:00
|
|
|
var response = await _httpClient.GetAsync(request);
|
2020-08-15 03:58:42 +00:00
|
|
|
|
2023-09-16 12:07:39 +00:00
|
|
|
return response.ResponseData;
|
2020-08-15 03:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|