2013-03-30 02:00:38 +03:00
|
|
|
using System;
|
2013-06-28 22:19:40 +03:00
|
|
|
using System.IO;
|
2013-02-18 05:10:21 +03:00
|
|
|
using Nancy;
|
2013-06-28 22:19:40 +03:00
|
|
|
using Nancy.Responses;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Common.Cache;
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
using NzbDrone.Api.Extensions;
|
2013-02-18 05:10:21 +03:00
|
|
|
|
2013-03-30 02:00:38 +03:00
|
|
|
namespace NzbDrone.Api.Frontend
|
2013-02-18 05:10:21 +03:00
|
|
|
{
|
|
|
|
public class IndexModule : NancyModule
|
|
|
|
{
|
2013-06-28 22:19:40 +03:00
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
private readonly ICached<string> _indexCache;
|
|
|
|
|
|
|
|
private readonly string _indexPath;
|
|
|
|
|
2013-07-05 07:43:28 +03:00
|
|
|
public IndexModule(IDiskProvider diskProvider, ICacheManger cacheManger, IAppFolderInfo appFolder)
|
2013-02-18 05:10:21 +03:00
|
|
|
{
|
2013-06-28 22:19:40 +03:00
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
2013-07-05 07:43:28 +03:00
|
|
|
_indexPath = Path.Combine(appFolder.StartUpFolder, "UI", "index.html");
|
2013-06-28 22:19:40 +03:00
|
|
|
|
|
|
|
_indexCache = cacheManger.GetCache<string>(typeof(IndexModule));
|
2013-02-18 06:04:28 +03:00
|
|
|
//Serve anything that doesn't have an extension
|
2013-07-07 07:36:37 +03:00
|
|
|
Get[@"/(.*)"] = x => Index();
|
2013-02-18 06:04:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private object Index()
|
|
|
|
{
|
2013-06-28 22:19:40 +03:00
|
|
|
if (
|
2013-02-18 06:04:28 +03:00
|
|
|
Request.Path.Contains(".")
|
2013-06-28 22:19:40 +03:00
|
|
|
|| Request.Path.StartsWith("/static", StringComparison.CurrentCultureIgnoreCase)
|
2013-05-06 00:24:33 +03:00
|
|
|
|| Request.Path.StartsWith("/api", StringComparison.CurrentCultureIgnoreCase)
|
|
|
|
|| Request.Path.StartsWith("/signalr", StringComparison.CurrentCultureIgnoreCase))
|
2013-02-18 06:04:28 +03:00
|
|
|
{
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-28 22:19:40 +03: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 05:10:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|