parent
44f8022fa2
commit
498368e8c5
@ -0,0 +1,55 @@
|
|||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.StaticFiles;
|
||||||
|
using NzbDrone.Core.MediaCover;
|
||||||
|
|
||||||
|
namespace Lidarr.Http.Frontend.Mappers
|
||||||
|
{
|
||||||
|
public class MediaCoverProxyMapper : IMapHttpRequestsToDisk
|
||||||
|
{
|
||||||
|
private readonly Regex _regex = new Regex(@"/MediaCoverProxy/(?<hash>\w+)/(?<filename>(.+)\.(jpg|png|gif))");
|
||||||
|
|
||||||
|
private readonly IMediaCoverProxy _mediaCoverProxy;
|
||||||
|
private readonly IContentTypeProvider _mimeTypeProvider;
|
||||||
|
|
||||||
|
public MediaCoverProxyMapper(IMediaCoverProxy mediaCoverProxy)
|
||||||
|
{
|
||||||
|
_mediaCoverProxy = mediaCoverProxy;
|
||||||
|
_mimeTypeProvider = new FileExtensionContentTypeProvider();
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Map(string resourceUrl)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CanHandle(string resourceUrl)
|
||||||
|
{
|
||||||
|
return resourceUrl.StartsWith("/MediaCoverProxy/", StringComparison.InvariantCultureIgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult GetResponse(string resourceUrl)
|
||||||
|
{
|
||||||
|
var match = _regex.Match(resourceUrl);
|
||||||
|
|
||||||
|
if (!match.Success)
|
||||||
|
{
|
||||||
|
return new StatusCodeResult((int)HttpStatusCode.NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
var hash = match.Groups["hash"].Value;
|
||||||
|
var filename = match.Groups["filename"].Value;
|
||||||
|
|
||||||
|
var imageData = _mediaCoverProxy.GetImage(hash);
|
||||||
|
|
||||||
|
if (!_mimeTypeProvider.TryGetContentType(filename, out var contentType))
|
||||||
|
{
|
||||||
|
contentType = "application/octet-stream";
|
||||||
|
}
|
||||||
|
|
||||||
|
return new FileContentResult(imageData, contentType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue