Fixed: UI hanging after scrolling movie list too fast

[common]
pull/5624/head
ta264 4 years ago committed by Qstick
parent d1bebd3e5b
commit f90211b0d3

@ -0,0 +1,21 @@
using System.IO;
using Nancy;
namespace Radarr.Http
{
public class ByteArrayResponse : Response
{
public ByteArrayResponse(byte[] body, string contentType)
{
ContentType = contentType;
Contents = stream =>
{
using (var writer = new BinaryWriter(stream))
{
writer.Write(body);
}
};
}
}
}

@ -1,6 +1,7 @@
using System; using System;
using System.IO; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Nancy; using Nancy;
using NLog; using NLog;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
@ -28,21 +29,16 @@ namespace Radarr.Http.Frontend.Mappers
protected string HtmlPath; protected string HtmlPath;
protected string UrlBase; protected string UrlBase;
protected override Stream GetContentStream(string filePath) protected override Task<byte[]> GetContent(string filePath)
{ {
var text = GetHtmlText(); var text = GetHtmlText();
var data = Encoding.UTF8.GetBytes(text);
var stream = new MemoryStream(); return Task.FromResult(data);
var writer = new StreamWriter(stream);
writer.Write(text);
writer.Flush();
stream.Position = 0;
return stream;
} }
public override Response GetResponse(string resourceUrl) public async override Task<Response> GetResponse(string resourceUrl)
{ {
var response = base.GetResponse(resourceUrl); var response = await base.GetResponse(resourceUrl);
response.Headers["X-UA-Compatible"] = "IE=edge"; response.Headers["X-UA-Compatible"] = "IE=edge";
return response; return response;

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

@ -1,8 +1,7 @@
using System; using System;
using System.IO;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Nancy; using Nancy;
using Nancy.Responses;
using NzbDrone.Core.MediaCover; using NzbDrone.Core.MediaCover;
namespace Radarr.Http.Frontend.Mappers namespace Radarr.Http.Frontend.Mappers
@ -28,13 +27,13 @@ namespace Radarr.Http.Frontend.Mappers
return resourceUrl.StartsWith("/MediaCoverProxy/", StringComparison.InvariantCultureIgnoreCase); return resourceUrl.StartsWith("/MediaCoverProxy/", StringComparison.InvariantCultureIgnoreCase);
} }
public Response GetResponse(string resourceUrl) public Task<Response> GetResponse(string resourceUrl)
{ {
var match = _regex.Match(resourceUrl); var match = _regex.Match(resourceUrl);
if (!match.Success) if (!match.Success)
{ {
return new NotFoundResponse(); return Task.FromResult<Response>(new NotFoundResponse());
} }
var hash = match.Groups["hash"].Value; var hash = match.Groups["hash"].Value;
@ -42,7 +41,7 @@ namespace Radarr.Http.Frontend.Mappers
var imageData = _mediaCoverProxy.GetImage(hash); var imageData = _mediaCoverProxy.GetImage(hash);
return new StreamResponse(() => new MemoryStream(imageData), MimeTypes.GetMimeType(filename)); return Task.FromResult<Response>(new ByteArrayResponse(imageData, MimeTypes.GetMimeType(filename)));
} }
} }
} }

@ -1,7 +1,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading.Tasks;
using Nancy; using Nancy;
using Nancy.Responses;
using NLog; using NLog;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
@ -28,14 +28,15 @@ namespace Radarr.Http.Frontend.Mappers
public abstract bool CanHandle(string resourceUrl); public abstract bool CanHandle(string resourceUrl);
public virtual Response GetResponse(string resourceUrl) public async virtual Task<Response> GetResponse(string resourceUrl)
{ {
var filePath = Map(resourceUrl); var filePath = Map(resourceUrl);
if (_diskProvider.FileExists(filePath, _caseSensitive)) if (_diskProvider.FileExists(filePath, _caseSensitive))
{ {
var response = new StreamResponse(() => GetContentStream(filePath), MimeTypes.GetMimeType(filePath)); var data = await GetContent(filePath).ConfigureAwait(false);
return new MaterialisingResponse(response);
return new ByteArrayResponse(data, MimeTypes.GetMimeType(filePath));
} }
_logger.Warn("File {0} not found", filePath); _logger.Warn("File {0} not found", filePath);
@ -43,9 +44,17 @@ namespace Radarr.Http.Frontend.Mappers
return NotFoundResponse; return NotFoundResponse;
} }
protected virtual Stream GetContentStream(string filePath) protected async virtual Task<byte[]> GetContent(string filePath)
{ {
return File.OpenRead(filePath); using (var output = new MemoryStream())
{
using (var file = File.OpenRead(filePath))
{
await file.CopyToAsync(output).ConfigureAwait(false);
}
return output.ToArray();
}
} }
} }
} }

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Nancy; using Nancy;
using NLog; using NLog;
using Radarr.Http.Frontend.Mappers; using Radarr.Http.Frontend.Mappers;
@ -17,11 +18,11 @@ namespace Radarr.Http.Frontend
_requestMappers = requestMappers; _requestMappers = requestMappers;
_logger = logger; _logger = logger;
Get("/{resource*}", x => Index()); Get("/{resource*}", async (x, ct) => await Index());
Get("/", x => Index()); Get("/", async (x, ct) => await Index());
} }
private Response Index() private async Task<Response> Index()
{ {
var path = Request.Url.Path; var path = Request.Url.Path;
@ -37,7 +38,7 @@ namespace Radarr.Http.Frontend
if (mapper != null) if (mapper != null)
{ {
return mapper.GetResponse(path); return await mapper.GetResponse(path);
} }
_logger.Warn("Couldn't find handler for {0}", path); _logger.Warn("Couldn't find handler for {0}", path);

Loading…
Cancel
Save