sonarr-repo-only/NzbDrone.Api/Frontend/Mappers/IndexHtmlMapper.cs

77 lines
2.3 KiB
C#
Raw Normal View History

2013-09-20 07:31:02 +00:00
using System;
using System.IO;
2013-09-17 22:03:16 +00:00
using Nancy;
2013-09-20 07:31:02 +00:00
using Nancy.Responses;
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo;
2013-09-20 07:31:02 +00:00
using NzbDrone.Core.Configuration;
namespace NzbDrone.Api.Frontend.Mappers
{
public class IndexHtmlMapper : StaticResourceMapperBase
{
private readonly IDiskProvider _diskProvider;
2013-09-20 07:31:02 +00:00
private readonly IConfigFileProvider _configFileProvider;
private readonly string _indexPath;
2013-09-20 07:31:02 +00:00
public IndexHtmlMapper(IAppFolderInfo appFolderInfo,
IDiskProvider diskProvider,
IConfigFileProvider configFileProvider,
Logger logger)
: base(diskProvider, logger)
{
_diskProvider = diskProvider;
2013-09-20 07:31:02 +00:00
_configFileProvider = configFileProvider;
_indexPath = Path.Combine(appFolderInfo.StartUpFolder, "UI", "index.html");
}
protected override string Map(string resourceUrl)
{
return _indexPath;
}
public override bool CanHandle(string resourceUrl)
{
return !resourceUrl.Contains(".");
}
2013-09-17 22:03:16 +00:00
public override Response GetResponse(string resourceUrl)
{
2013-09-20 07:31:02 +00:00
string content;
2013-09-17 22:03:16 +00:00
var response = base.GetResponse(resourceUrl);
2013-09-20 07:31:02 +00:00
var stream = new MemoryStream();
response.Contents.Invoke(stream);
stream.Position = 0;
using (var reader = new StreamReader(stream))
{
content = reader.ReadToEnd();
}
content = content.Replace("API_KEY", _configFileProvider.ApiKey);
response = new StreamResponse(() => StringToStream(content), response.ContentType);
2013-09-17 22:03:16 +00:00
response.Headers["X-UA-Compatible"] = "IE=edge";
return response;
}
protected override Stream GetContentStream(string filePath)
{
return StringToStream(GetIndexText());
}
private string GetIndexText()
{
var text = _diskProvider.ReadAllText(_indexPath);
text = text.Replace(".css", ".css?v=" + BuildInfo.Version);
text = text.Replace(".js", ".js?v=" + BuildInfo.Version);
return text;
}
}
}