|
//--------------------------------------------------------------------------------------------------------------
// Created by : Gianluca Negrelli
// Copyright : 2010.05.15
//--------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
namespace EcommerceWebSite.Controls
{
/// <summary>
/// Base class for web site pages.
/// </summary>
public abstract class BasePage : Page
{
#region ClientVariables
/// <summary>
/// Label collection that is rendered to the client.
/// </summary>
private Dictionary<string, object> ScriptVariables = new Dictionary<string, object>();
/// <summary>
/// Prerender event.
/// </summary>
protected override void OnPreRender(System.EventArgs e)
{
if (ScriptVariables.Count > 0)
{
ClientVariableRenderer cv = new ClientVariableRenderer();
foreach (KeyValuePair<string, object> currKey in ScriptVariables)
{
cv.Add(currKey.Key, currKey.Value);
}
}
base.OnPreRender(e);
}
/// <summary>
/// Method to add object to script variable collection.
/// </summary>
public void AddClientVariable(string name, object value)
{
ScriptVariables.Add(name, value);
}
#endregion Client Variables
#region Version javascript
private static readonly string cacheFile;
private static object lockerExternalFile = new object();
/// <summary>
/// Resolves url and adds a query string with the file hash.
/// The hash is stored into a cache depending on a file.
/// </summary>
public string ResolveAndVersionUrl(string url)
{
url = this.ResolveUrl(url);
string fileName = this.Server.MapPath(url);
lock (lockerExternalFile)
{
// Verifies if the file hash is already in cache
object o = HttpContext.Current.Cache["BaseMasterPage::" + fileName];
if (o == null)
{
// Check if file exists or not
if (!File.Exists(fileName))
{
throw new Exception("The file " + url + " doesn't exist");
}
// Calculates the hash
o = GetMD5HashFromFile(fileName);
// Verifies if cache tracking file exists
CheckFileCache();
CacheDependency d = new CacheDependency(cacheFile);
HttpContext.Current.Cache.Insert("BasePage::" + fileName, o, d, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration);
}
return string.Format("{0}?{1}", url, o);
}
}
/// <summary>
/// Gets the hash of the file.
/// </summary>
private string GetMD5HashFromFile(string fileName)
{
byte[] retVal = null;
using (FileStream file = new FileStream(fileName, FileMode.Open))
{
using (MD5 md5 = MD5CryptoServiceProvider.Create())
{
retVal = md5.ComputeHash(file);
}
file.Close();
}
return Convert.ToBase64String(retVal);
}
/// <summary>
/// Checks if cache tracking file exists otherwise it creates it.
/// </summary>
private void CheckFileCache()
{
if (!File.Exists(cacheFile))
{
using (FileStream fs = new FileStream(cacheFile, FileMode.Create, FileAccess.Write))
{
fs.Close();
}
}
}
#endregion Version javascript
#region Constructor
static BasePage()
{
cacheFile = HttpContext.Current.Server.MapPath(@"~/cache\CacheFile_JsFileVersioner");
FileInfo cacheFolder = new FileInfo(cacheFile);
if (!cacheFolder.Directory.Exists)
{
throw new Exception("Cache folder doesn't exist.");
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.