parent
863252d7e9
commit
77036ac3ef
@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.Configuration;
|
||||
|
||||
namespace NzbDrone.Core.MediaCover
|
||||
{
|
||||
public interface IMediaCoverProxy
|
||||
{
|
||||
string RegisterUrl(string url);
|
||||
|
||||
string GetUrl(string hash);
|
||||
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)
|
||||
{
|
||||
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 byte[] GetImage(string hash)
|
||||
{
|
||||
var url = GetUrl(hash);
|
||||
|
||||
var request = new HttpRequest(url);
|
||||
|
||||
return _httpClient.Get(request).ResponseData;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using Nancy;
|
||||
using Nancy.Responses;
|
||||
using NzbDrone.Core.MediaCover;
|
||||
|
||||
namespace Radarr.Http.Frontend.Mappers
|
||||
{
|
||||
public class MediaCoverProxyMapper : IMapHttpRequestsToDisk
|
||||
{
|
||||
private readonly Regex _regex = new Regex(@"/MediaCoverProxy/(?<hash>\w+)/(?<filename>(.+)\.(jpg|png|gif))");
|
||||
|
||||
private readonly IMediaCoverProxy _mediaCoverProxy;
|
||||
|
||||
public MediaCoverProxyMapper(IMediaCoverProxy mediaCoverProxy)
|
||||
{
|
||||
_mediaCoverProxy = mediaCoverProxy;
|
||||
}
|
||||
|
||||
public string Map(string resourceUrl)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool CanHandle(string resourceUrl)
|
||||
{
|
||||
return resourceUrl.StartsWith("/MediaCoverProxy/", StringComparison.InvariantCultureIgnoreCase);
|
||||
}
|
||||
|
||||
public Response GetResponse(string resourceUrl)
|
||||
{
|
||||
var match = _regex.Match(resourceUrl);
|
||||
|
||||
if (!match.Success)
|
||||
{
|
||||
return new NotFoundResponse();
|
||||
}
|
||||
|
||||
var hash = match.Groups["hash"].Value;
|
||||
var filename = match.Groups["filename"].Value;
|
||||
|
||||
var imageData = _mediaCoverProxy.GetImage(hash);
|
||||
|
||||
return new StreamResponse(() => new MemoryStream(imageData), MimeTypes.GetMimeType(filename));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue