2011-12-09 07:57:23 +03:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Web.Mvc;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
|
|
|
namespace NzbDrone.Web.Helpers
|
|
|
|
{
|
|
|
|
public static class HtmlIncludeExtentions
|
|
|
|
{
|
2011-12-09 10:59:34 +03:00
|
|
|
private static readonly string versionString;
|
|
|
|
private static readonly bool isProduction;
|
2011-12-09 07:57:23 +03:00
|
|
|
|
|
|
|
static HtmlIncludeExtentions()
|
|
|
|
{
|
2012-03-07 05:59:43 +03:00
|
|
|
versionString = new EnvironmentProvider().Version.ToString().Replace('.', '_');
|
|
|
|
isProduction = EnvironmentProvider.IsProduction;
|
2011-12-09 07:57:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static MvcHtmlString IncludeScript(this HtmlHelper helper, string filename)
|
|
|
|
{
|
|
|
|
var relativePath = "/Scripts/" + filename;
|
|
|
|
VerifyFile(helper, relativePath);
|
2011-12-09 10:59:34 +03:00
|
|
|
return MvcHtmlString.Create(String.Format("<script type='text/javascript' src='{0}?{1}'></script>", relativePath, versionString));
|
2011-12-09 07:57:23 +03:00
|
|
|
}
|
2011-12-09 10:59:34 +03:00
|
|
|
|
2011-12-09 07:57:23 +03:00
|
|
|
public static MvcHtmlString IncludeCss(this HtmlHelper helper, string filename)
|
|
|
|
{
|
|
|
|
var relativePath = "/Content/" + filename;
|
|
|
|
VerifyFile(helper, relativePath);
|
2011-12-09 10:59:34 +03:00
|
|
|
return MvcHtmlString.Create(String.Format("<link type='text/css' rel='stylesheet' href='{0}?{1}'/>", relativePath, versionString));
|
2011-12-09 07:57:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void VerifyFile(HtmlHelper helper, string filename)
|
|
|
|
{
|
2011-12-09 10:59:34 +03:00
|
|
|
if (isProduction)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var path = helper.ViewContext.RequestContext.HttpContext.Server.MapPath(filename);
|
2011-12-09 07:57:23 +03:00
|
|
|
|
2011-12-09 10:59:34 +03:00
|
|
|
if (!File.Exists(path))
|
|
|
|
{
|
2012-02-11 03:48:20 +03:00
|
|
|
throw new FileNotFoundException("Included static resource was not found.", path);
|
2011-12-09 07:57:23 +03:00
|
|
|
}
|
2011-12-09 10:59:34 +03:00
|
|
|
|
2011-12-09 07:57:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|