@ -10,6 +10,7 @@ using System.Linq;
using System.Text ;
using System.Threading.Tasks ;
using WebMarkupMin.Core.Minifiers ;
using WebMarkupMin.Core.Settings ;
namespace MediaBrowser.WebDashboard.Api
{
@ -30,9 +31,9 @@ namespace MediaBrowser.WebDashboard.Api
_jsonSerializer = jsonSerializer ;
}
public async Task < Stream > GetResource ( string path ,
public async Task < Stream > GetResource ( string path ,
string localizationCulture ,
string appVersion )
string appVersion , bool enableMinification )
{
var isHtml = IsHtml ( path ) ;
@ -40,11 +41,11 @@ namespace MediaBrowser.WebDashboard.Api
if ( path . Equals ( "scripts/all.js" , StringComparison . OrdinalIgnoreCase ) )
{
resourceStream = await GetAllJavascript ( localizationCulture , appVersion ). ConfigureAwait ( false ) ;
resourceStream = await GetAllJavascript ( localizationCulture , appVersion , enableMinification ). ConfigureAwait ( false ) ;
}
else if ( path . Equals ( "css/all.css" , StringComparison . OrdinalIgnoreCase ) )
{
resourceStream = await GetAllCss ( ) . ConfigureAwait ( false ) ;
resourceStream = await GetAllCss ( enableMinification ) . ConfigureAwait ( false ) ;
}
else
{
@ -57,7 +58,7 @@ namespace MediaBrowser.WebDashboard.Api
// jQuery ajax doesn't seem to handle if-modified-since correctly
if ( isHtml )
{
resourceStream = await ModifyHtml ( resourceStream , localizationCulture ). ConfigureAwait ( false ) ;
resourceStream = await ModifyHtml ( resourceStream , localizationCulture , enableMinification ). ConfigureAwait ( false ) ;
}
}
@ -106,8 +107,9 @@ namespace MediaBrowser.WebDashboard.Api
/// </summary>
/// <param name="sourceStream">The source stream.</param>
/// <param name="localizationCulture">The localization culture.</param>
/// <param name="enableMinification">if set to <c>true</c> [enable minification].</param>
/// <returns>Task{Stream}.</returns>
public async Task < Stream > ModifyHtml ( Stream sourceStream , string localizationCulture )
public async Task < Stream > ModifyHtml ( Stream sourceStream , string localizationCulture , bool enableMinification )
{
using ( sourceStream )
{
@ -128,16 +130,27 @@ namespace MediaBrowser.WebDashboard.Api
html = html . Replace ( "<html>" , "<html lang=\"" + lang + "\">" ) ;
}
//try
//{
// var minifier = new HtmlMinifier(new HtmlMinificationSettings(true));
if ( enableMinification )
{
try
{
var minifier = new HtmlMinifier ( new HtmlMinificationSettings ( ) ) ;
var result = minifier . Minify ( html , false ) ;
// html = minifier.Minify(html).MinifiedContent;
//}
//catch (Exception ex)
//{
// Logger.ErrorException("Error minifying html", ex);
//}
if ( result . Errors . Count > 0 )
{
_logger . Error ( "Error minifying html: " + result . Errors [ 0 ] . Message ) ;
}
else
{
html = result . MinifiedContent ;
}
}
catch ( Exception ex )
{
_logger . ErrorException ( "Error minifying html" , ex ) ;
}
}
}
var version = GetType ( ) . Assembly . GetName ( ) . Version ;
@ -221,7 +234,6 @@ namespace MediaBrowser.WebDashboard.Api
var files = new [ ]
{
"scripts/all.js" + versionString ,
"thirdparty/jstree3.0.8/jstree.min.js" ,
"thirdparty/swipebox-master/js/jquery.swipebox.min.js" + versionString
} ;
@ -236,7 +248,7 @@ namespace MediaBrowser.WebDashboard.Api
/// Gets a stream containing all concatenated javascript
/// </summary>
/// <returns>Task{Stream}.</returns>
private async Task < Stream > GetAllJavascript ( string culture , string version )
private async Task < Stream > GetAllJavascript ( string culture , string version , bool enableMinification )
{
var memoryStream = new MemoryStream ( ) ;
var newLineBytes = Encoding . UTF8 . GetBytes ( Environment . NewLine ) ;
@ -250,6 +262,8 @@ namespace MediaBrowser.WebDashboard.Api
await AppendResource ( memoryStream , "thirdparty/cast_sender.js" , newLineBytes ) . ConfigureAwait ( false ) ;
await AppendResource ( memoryStream , "thirdparty/browser.js" , newLineBytes ) . ConfigureAwait ( false ) ;
await AppendResource ( memoryStream , "thirdparty/jstree3.0.8/jstree.js" , newLineBytes ) . ConfigureAwait ( false ) ;
await AppendLocalization ( memoryStream , culture ) . ConfigureAwait ( false ) ;
await memoryStream . WriteAsync ( newLineBytes , 0 , newLineBytes . Length ) . ConfigureAwait ( false ) ;
@ -304,15 +318,25 @@ namespace MediaBrowser.WebDashboard.Api
var js = builder . ToString ( ) ;
try
if ( enableMinification )
{
var result = new CrockfordJsMinifier ( ) . Minify ( js , false , Encoding . UTF8 ) ;
try
{
var result = new CrockfordJsMinifier ( ) . Minify ( js , false , Encoding . UTF8 ) ;
js = result . MinifiedContent ;
}
catch ( Exception ex )
{
_logger . ErrorException ( "Error minifying javascript" , ex ) ;
if ( result . Errors . Count > 0 )
{
_logger . Error ( "Error minifying javascript: " + result . Errors [ 0 ] . Message ) ;
}
else
{
js = result . MinifiedContent ;
}
}
catch ( Exception ex )
{
_logger . ErrorException ( "Error minifying javascript" , ex ) ;
}
}
var bytes = Encoding . UTF8 . GetBytes ( js ) ;
@ -518,7 +542,7 @@ namespace MediaBrowser.WebDashboard.Api
/// Gets all CSS.
/// </summary>
/// <returns>Task{Stream}.</returns>
private async Task < Stream > GetAllCss ( )
private async Task < Stream > GetAllCss ( bool enableMinification )
{
var files = new [ ]
{
@ -561,16 +585,26 @@ namespace MediaBrowser.WebDashboard.Api
var css = builder . ToString ( ) ;
//try
//{
// var result = new KristensenCssMinifier().Minify(builder.ToString(), false, Encoding.UTF8);
if ( enableMinification )
{
try
{
var result = new KristensenCssMinifier ( ) . Minify ( builder . ToString ( ) , false , Encoding . UTF8 ) ;
// css = result.MinifiedContent;
//}
//catch (Exception ex)
//{
// Logger.ErrorException("Error minifying css", ex);
//}
if ( result . Errors . Count > 0 )
{
_logger . Error ( "Error minifying css: " + result . Errors [ 0 ] . Message ) ;
}
else
{
css = result . MinifiedContent ;
}
}
catch ( Exception ex )
{
_logger . ErrorException ( "Error minifying css" , ex ) ;
}
}
var memoryStream = new MemoryStream ( Encoding . UTF8 . GetBytes ( css ) ) ;