2013-03-29 23:00:38 +00:00
|
|
|
using System;
|
2013-06-28 19:19:40 +00:00
|
|
|
using System.IO;
|
2013-02-18 02:10:21 +00:00
|
|
|
using Nancy;
|
2013-06-28 19:19:40 +00:00
|
|
|
using Nancy.Responses;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Common.Cache;
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
using NzbDrone.Api.Extensions;
|
2013-02-18 02:10:21 +00:00
|
|
|
|
2013-03-29 23:00:38 +00:00
|
|
|
namespace NzbDrone.Api.Frontend
|
2013-02-18 02:10:21 +00:00
|
|
|
{
|
|
|
|
public class IndexModule : NancyModule
|
|
|
|
{
|
2013-06-28 19:19:40 +00:00
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
private readonly ICached<string> _indexCache;
|
|
|
|
|
|
|
|
private readonly string _indexPath;
|
|
|
|
|
2013-07-05 04:43:28 +00:00
|
|
|
public IndexModule(IDiskProvider diskProvider, ICacheManger cacheManger, IAppFolderInfo appFolder)
|
2013-02-18 02:10:21 +00:00
|
|
|
{
|
2013-06-28 19:19:40 +00:00
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
2013-07-05 04:43:28 +00:00
|
|
|
_indexPath = Path.Combine(appFolder.StartUpFolder, "UI", "index.html");
|
2013-06-28 19:19:40 +00:00
|
|
|
|
|
|
|
_indexCache = cacheManger.GetCache<string>(typeof(IndexModule));
|
2013-02-18 03:04:28 +00:00
|
|
|
//Serve anything that doesn't have an extension
|
2013-07-06 23:29:01 +00:00
|
|
|
Get[@"^(?:.*)$"] = x => Index();
|
|
|
|
Get[@"/"] = x => Index();
|
2013-02-18 03:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private object Index()
|
|
|
|
{
|
2013-06-28 19:19:40 +00:00
|
|
|
if (
|
2013-02-18 03:04:28 +00:00
|
|
|
Request.Path.Contains(".")
|
2013-06-28 19:19:40 +00:00
|
|
|
|| Request.Path.StartsWith("/static", StringComparison.CurrentCultureIgnoreCase)
|
2013-05-05 21:24:33 +00:00
|
|
|
|| Request.Path.StartsWith("/api", StringComparison.CurrentCultureIgnoreCase)
|
|
|
|
|| Request.Path.StartsWith("/signalr", StringComparison.CurrentCultureIgnoreCase))
|
2013-02-18 03:04:28 +00:00
|
|
|
{
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-28 19:19:40 +00:00
|
|
|
var htmlResponse = new HtmlResponse();
|
|
|
|
|
|
|
|
htmlResponse.Contents = stream =>
|
|
|
|
{
|
|
|
|
var lastWrite = _diskProvider.GetLastFileWrite(_indexPath);
|
|
|
|
var text = _indexCache.Get(lastWrite.Ticks.ToString(), GetIndexText);
|
|
|
|
|
|
|
|
var streamWriter = new StreamWriter(stream);
|
|
|
|
|
|
|
|
streamWriter.Write(text);
|
|
|
|
streamWriter.Flush();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
htmlResponse.Headers.DisableCache();
|
|
|
|
|
|
|
|
return htmlResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2013-02-18 02:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|