Added cache headers to static resources.

This commit is contained in:
Keivan Beigi 2013-06-28 12:19:40 -07:00
commit b56da336c0
5 changed files with 73 additions and 8 deletions

View file

@ -1,22 +1,37 @@
using System;
using System.IO;
using Nancy;
using Nancy.Security;
using Nancy.Responses;
using NzbDrone.Common;
using NzbDrone.Common.Cache;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Api.Extensions;
namespace NzbDrone.Api.Frontend
{
public class IndexModule : NancyModule
{
public IndexModule()
private readonly IDiskProvider _diskProvider;
private readonly ICached<string> _indexCache;
private readonly string _indexPath;
public IndexModule(IDiskProvider diskProvider, ICacheManger cacheManger, IAppDirectoryInfo appDirectory)
{
_diskProvider = diskProvider;
_indexPath = Path.Combine(appDirectory.StartUpPath, "UI", "index.html");
_indexCache = cacheManger.GetCache<string>(typeof(IndexModule));
//Serve anything that doesn't have an extension
Get[@"/(.*)"] = x => Index();
}
private object Index()
{
if(
if (
Request.Path.Contains(".")
|| Request.Path.StartsWith("/static", StringComparison.CurrentCultureIgnoreCase)
|| Request.Path.StartsWith("/static", StringComparison.CurrentCultureIgnoreCase)
|| Request.Path.StartsWith("/api", StringComparison.CurrentCultureIgnoreCase)
|| Request.Path.StartsWith("/signalr", StringComparison.CurrentCultureIgnoreCase))
{
@ -24,7 +39,34 @@ namespace NzbDrone.Api.Frontend
}
return View["UI/index.html"];
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;
}
}
}

View file

@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Linq;
using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo;
namespace NzbDrone.Api.Frontend

View file

@ -5,6 +5,7 @@ using NLog;
using Nancy;
using Nancy.Responses;
using NzbDrone.Common;
using NzbDrone.Api.Extensions;
namespace NzbDrone.Api.Frontend
{
@ -43,7 +44,10 @@ namespace NzbDrone.Api.Frontend
if (_diskProvider.FileExists(filePath))
{
return new StreamResponse(() => File.OpenRead(filePath), MimeTypes.GetMimeType(filePath));
var response = new StreamResponse(() => File.OpenRead(filePath), MimeTypes.GetMimeType(filePath));
response.Headers.EnableCache();
return response;
}
_logger.Warn("File {0} not found", filePath);