Use async requests for media cover proxy

pull/6020/head
Bogdan 1 year ago committed by Mark McDowall
parent ddabe66262
commit ad1f185330

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading.Tasks;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
@ -12,7 +13,7 @@ namespace NzbDrone.Core.MediaCover
string RegisterUrl(string url); string RegisterUrl(string url);
string GetUrl(string hash); string GetUrl(string hash);
byte[] GetImage(string hash); Task<byte[]> GetImage(string hash);
} }
public class MediaCoverProxy : IMediaCoverProxy public class MediaCoverProxy : IMediaCoverProxy
@ -52,13 +53,14 @@ namespace NzbDrone.Core.MediaCover
return result; return result;
} }
public byte[] GetImage(string hash) public async Task<byte[]> GetImage(string hash)
{ {
var url = GetUrl(hash); var url = GetUrl(hash);
var request = new HttpRequest(url); var request = new HttpRequest(url);
var response = await _httpClient.GetAsync(request);
return _httpClient.Get(request).ResponseData; return response.ResponseData;
} }
} }
} }

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace Sonarr.Http.Frontend.Mappers namespace Sonarr.Http.Frontend.Mappers
{ {
@ -6,6 +7,6 @@ namespace Sonarr.Http.Frontend.Mappers
{ {
string Map(string resourceUrl); string Map(string resourceUrl);
bool CanHandle(string resourceUrl); bool CanHandle(string resourceUrl);
IActionResult GetResponse(string resourceUrl); Task<IActionResult> GetResponse(string resourceUrl);
} }
} }

@ -1,6 +1,7 @@
using System; using System;
using System.Net; using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles; using Microsoft.AspNetCore.StaticFiles;
using NzbDrone.Core.MediaCover; using NzbDrone.Core.MediaCover;
@ -9,7 +10,7 @@ namespace Sonarr.Http.Frontend.Mappers
{ {
public class MediaCoverProxyMapper : IMapHttpRequestsToDisk public class MediaCoverProxyMapper : IMapHttpRequestsToDisk
{ {
private readonly Regex _regex = new Regex(@"/MediaCoverProxy/(?<hash>\w+)/(?<filename>(.+)\.(jpg|png|gif))"); private readonly Regex _regex = new (@"/MediaCoverProxy/(?<hash>\w+)/(?<filename>(.+)\.(jpg|png|gif))");
private readonly IMediaCoverProxy _mediaCoverProxy; private readonly IMediaCoverProxy _mediaCoverProxy;
private readonly IContentTypeProvider _mimeTypeProvider; private readonly IContentTypeProvider _mimeTypeProvider;
@ -30,7 +31,7 @@ namespace Sonarr.Http.Frontend.Mappers
return resourceUrl.StartsWith("/MediaCoverProxy/", StringComparison.InvariantCultureIgnoreCase); return resourceUrl.StartsWith("/MediaCoverProxy/", StringComparison.InvariantCultureIgnoreCase);
} }
public IActionResult GetResponse(string resourceUrl) public async Task<IActionResult> GetResponse(string resourceUrl)
{ {
var match = _regex.Match(resourceUrl); var match = _regex.Match(resourceUrl);
@ -42,7 +43,7 @@ namespace Sonarr.Http.Frontend.Mappers
var hash = match.Groups["hash"].Value; var hash = match.Groups["hash"].Value;
var filename = match.Groups["filename"].Value; var filename = match.Groups["filename"].Value;
var imageData = _mediaCoverProxy.GetImage(hash); var imageData = await _mediaCoverProxy.GetImage(hash);
if (!_mimeTypeProvider.TryGetContentType(filename, out var contentType)) if (!_mimeTypeProvider.TryGetContentType(filename, out var contentType))
{ {

@ -1,6 +1,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles; using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
@ -30,7 +31,7 @@ namespace Sonarr.Http.Frontend.Mappers
public abstract bool CanHandle(string resourceUrl); public abstract bool CanHandle(string resourceUrl);
public IActionResult GetResponse(string resourceUrl) public Task<IActionResult> GetResponse(string resourceUrl)
{ {
var filePath = Map(resourceUrl); var filePath = Map(resourceUrl);
@ -41,10 +42,10 @@ namespace Sonarr.Http.Frontend.Mappers
contentType = "application/octet-stream"; contentType = "application/octet-stream";
} }
return new FileStreamResult(GetContentStream(filePath), new MediaTypeHeaderValue(contentType) return Task.FromResult<IActionResult>(new FileStreamResult(GetContentStream(filePath), new MediaTypeHeaderValue(contentType)
{ {
Encoding = contentType == "text/plain" ? Encoding.UTF8 : null Encoding = contentType == "text/plain" ? Encoding.UTF8 : null
}); }));
} }
_logger.Warn("File {0} not found", filePath); _logger.Warn("File {0} not found", filePath);

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -25,27 +26,27 @@ namespace Sonarr.Http.Frontend
[AllowAnonymous] [AllowAnonymous]
[HttpGet("login")] [HttpGet("login")]
public IActionResult LoginPage() public async Task<IActionResult> LoginPage()
{ {
return MapResource("login"); return await MapResource("login");
} }
[EnableCors("AllowGet")] [EnableCors("AllowGet")]
[AllowAnonymous] [AllowAnonymous]
[HttpGet("/content/{**path:regex(^(?!api/).*)}")] [HttpGet("/content/{**path:regex(^(?!api/).*)}")]
public IActionResult IndexContent([FromRoute] string path) public async Task<IActionResult> IndexContent([FromRoute] string path)
{ {
return MapResource("Content/" + path); return await MapResource("Content/" + path);
} }
[HttpGet("")] [HttpGet("")]
[HttpGet("/{**path:regex(^(?!(api|feed)/).*)}")] [HttpGet("/{**path:regex(^(?!(api|feed)/).*)}")]
public IActionResult Index([FromRoute] string path) public async Task<IActionResult> Index([FromRoute] string path)
{ {
return MapResource(path); return await MapResource(path);
} }
private IActionResult MapResource(string path) private async Task<IActionResult> MapResource(string path)
{ {
path = "/" + (path ?? ""); path = "/" + (path ?? "");
@ -53,7 +54,7 @@ namespace Sonarr.Http.Frontend
if (mapper != null) if (mapper != null)
{ {
var result = mapper.GetResponse(path); var result = await mapper.GetResponse(path);
if (result != null) if (result != null)
{ {

Loading…
Cancel
Save