public class ScriptHelper
{
protected StringCollection includeScripts = new StringCollection();
public void Include( String filename )
{
filename = filename.ToLower();
StringCollection container;
switch ( System.IO.Path.GetExtension( filename ) )
{
case ".js" : container = includeScripts; break ;
default : throw new ArgumentException( "Not supported include file: " + filename, "filename" );
}
if ( !container.Contains( filename ) ) container.Add( filename );
}
public void RegisterScripts( Page page )
{
StringBuilder clientScript = new StringBuilder ();
foreach ( String filename in includeScripts )
clientScript.AppendFormat( includeJS, prefix + FileSystemWatcherManager.GetModifiedName( "Scripts/" + filename ) );
page.ClientScript.RegisterClientScriptBlock( page.GetType(), "clientscripts" , clientScript.ToString(), false );
}
}
* This source code was highlighted with Source Code Highlighter .
using System;
using System.IO;
using System.Collections. Generic ;
public class FileSystemWatcherManager
{
private static String physicalAppPath;
private static SortedList< String , Int64 > lastModifiedFiles = new SortedList< String , Int64 >();
public static void StartDirectoryWatcher( String directory, String filter )
{
#if DEBUG
return ;
#endif
if ( physicalAppPath == null && System.Web. HttpContext .Current.Request != null )
physicalAppPath = System.Web. HttpContext .Current.Request.PhysicalApplicationPath;
foreach ( String pattern in filter.Split( ',' ) )
{
FileSystemWatcher dirWatcher = new FileSystemWatcher( directory, pattern );
dirWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
dirWatcher.IncludeSubdirectories = true ;
dirWatcher.EnableRaisingEvents = true ;
dirWatcher.Changed += new FileSystemEventHandler( OnFileSystemChanged );
dirWatcher.Created += new FileSystemEventHandler( OnFileSystemChanged );
dirWatcher.Renamed += new RenamedEventHandler( OnFileSystemRenamed );
UpdateLastModifiedFiles( directory, pattern, true );
}
}
private static void OnFileSystemRenamed( object sender, RenamedEventArgs e )
{
UpdateLastModifiedFiles( Path.GetDirectoryName( e.FullPath ), ( (FileSystemWatcher)sender ).Filter, true );
}
private static void OnFileSystemChanged( object sender, FileSystemEventArgs e )
{
UpdateLastModifiedFiles( Path.GetDirectoryName( e.FullPath ), ((FileSystemWatcher)sender).Filter, true );
}
public static void UpdateLastModifiedFiles( String directory, String filter, Boolean logAction )
{
lock ( lastModifiedFiles )
{
if ( logAction ) WL.Logger.Instance.Log( String .Format( "Update modified files {1} at \"{0}\"" , directory, filter ) );
foreach ( String subDir in Directory .GetDirectories( directory ) )
UpdateLastModifiedFiles( subDir, filter, false );
foreach ( String file in Directory .GetFiles( directory, filter ) )
lastModifiedFiles[file.Substring( physicalAppPath.Length ).ToLower().Replace( '\\' , '/' )] = File .GetLastWriteTimeUtc( file ).Ticks / 1000000;
}
}
public static String GetModifiedName( String clientPath )
{
#if DEBUG
return clientPath;
#endif
lock ( lastModifiedFiles )
{
Int64 ticks;
if ( !lastModifiedFiles.TryGetValue( clientPath.ToLower(), out ticks ) ) return clientPath;
return String .Format( "{0}/{1}.{2}{3}" , Path.GetDirectoryName( clientPath ).Replace( '\\' , '/' ), Path.GetFileNameWithoutExtension( clientPath ), ticks, Path.GetExtension( clientPath ) );
}
}
}
* This source code was highlighted with Source Code Highlighter .
void Application_Start( object sender, EventArgs e )
{
FileSystemWatcherManager.StartDirectoryWatcher( HttpContext .Current.Request.PhysicalApplicationPath, "*.js,*.css" );
}
* This source code was highlighted with Source Code Highlighter .
<httpHandlers>
<add verb="GET" path="*.js" type="WL.JSHandler"/>
</httpHandlers>
public class JSHandler : IHttpHandler
{
public void ProcessRequest( HttpContext context )
{
try
{
String filepath = context.Request.PhysicalPath;
String [] parts = filepath.Split( '.' );
Int64 modifiedTicks = 0;
if ( parts.Length >= 2 )
{
if ( Int64 .TryParse( parts[parts.Length - 2], out modifiedTicks ) )
{
List < String > parts2 = new List < String >( parts );
parts2.RemoveAt( parts2.Count - 2 );
filepath = String .Join( "." , parts2.ToArray() );
}
}
FileInfo fileInfo = new FileInfo( filepath );
if ( !fileInfo.Exists )
{
context.Response.StatusCode = 404;
context.Response.StatusDescription = "Not found" ;
}
else
{
DateTime lastModTime = new DateTime ( fileInfo.LastWriteTime.Year, fileInfo.LastWriteTime.Month, fileInfo.LastWriteTime.Day, fileInfo.LastWriteTime.Hour, fileInfo.LastWriteTime.Minute, fileInfo.LastWriteTime.Second, 0 ).ToUniversalTime();
String ETag = String .Format( "\"{0}\"" , lastModTime.ToFileTime().ToString( "X8" , System.Globalization.CultureInfo.InvariantCulture ) );
if ( ETag == context.Request.Headers[ "If-None-Match" ] )
{
context.Response.StatusCode = 304;
context.Response.StatusDescription = "Not Modified" ;
}
else
if ( context.Request.Headers[ "If-Modified-Since" ] != null )
{
String modifiedSince = context.Request.Headers[ "If-Modified-Since" ];
Int32 sepIndex = modifiedSince.IndexOf( ';' );
if ( sepIndex > 0 ) modifiedSince = modifiedSince.Substring( 0, sepIndex );
DateTime sinceDate;
if ( DateTime .TryParseExact( modifiedSince, "R" , null , System.Globalization.DateTimeStyles.AssumeUniversal, out sinceDate ) &&
lastModTime.CompareTo( sinceDate.ToUniversalTime() ) == 0 )
{
context.Response.StatusCode = 304;
context.Response.StatusDescription = "Not Modified" ;
}
}
if ( context.Response.StatusCode != 304 )
{
String file = fileInfo.FullName;
/* String encoding = context.Request.Headers["Accept-Encoding"];
if( encoding != null && encoding.IndexOf( "gzip", StringComparison.InvariantCultureIgnoreCase ) >= 0 &&
File.Exists( file + ".jsgz" ) )
{
file = file + ".jsgz";
context.Response.AppendHeader( "Content-Encoding", "gzip" );
}
else*/
if ( File .Exists( file + ".jsmin" ) ) file = file + ".jsmin" ;
if ( context.Request.HttpMethod == "GET" )
{
context.Response.TransmitFile( file );
}
context.Response.Cache.SetCacheability( HttpCacheability.Public );
context.Response.Cache.SetLastModified( lastModTime );
context.Response.Cache.SetETag( ETag );
if ( modifiedTicks != 0 )
context.Response.Cache.SetExpires( DateTime .UtcNow.AddYears( 1 ) );
context.Response.AppendHeader( "Content-Type" , "text/javascript" );
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK" ;
}
}
}
catch ( Exception ex )
{
WL.Logger.Instance.Error( ex );
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error" ;
}
}
public bool IsReusable { get { return true ; } }
}
* This source code was highlighted with Source Code Highlighter .
<httpHandlers>
<add verb="*" path=".jsmin" type="System.Web.HttpForbiddenHandler"/>
<add verb="*" path=".jsgz" type="System.Web.HttpForbiddenHandler"/>
</httpHandlers>
StringBuilder clientScript = new StringBuilder ();
if ( includeScripts.Count > 0 )
{
clientScript.Append( @"<div id=" "preloader" " style=" "display:none" "><div></div>Loading Scripts...</div>" );
}
clientScript.Append( scriptStart );
if ( includeScripts.Count > 0 )
{
clientScript.Append( @"var pl=document.getElementById(" "preloader" ");pl.style.display=" "" ";pl=pl.firstChild;" );
}
clientScript.Append( scriptEnd );
if ( includeScripts.Count > 0 )
{
Single dx = 100f / includeScripts.Count;
Single pos = 0f;
foreach ( String filename in includeScripts )
{
clientScript.AppendFormat( includeJS, prefix + FileSystemWatcherManager.GetModifiedName( "Scripts/" + filename ) );
clientScript.AppendFormat( @"<script type=" "text/javascript" ">pl.style.width=" "{0}%" ";</script>" , ( Int32 )pos );
pos += dx;
}
}
* This source code was highlighted with Source Code Highlighter .
#preloader { width:218px;height:92px;background:transparent url("../images/preload.jpg") no-repeat scroll left top;position:relative;text-align:right;color:#383922;font-weight:bold;margin-left:20px;margin-right:auto; }
#preloader div { width:0px;height:92px;background:transparent url("../images/preload.jpg") no-repeat scroll left bottom;position:absolute;left:0px;top:0px; }
Source: https://habr.com/ru/post/51258/
All Articles