diff options
Diffstat (limited to 'MediaBrowser.WebDashboard/Api/PackageCreator.cs')
| -rw-r--r-- | MediaBrowser.WebDashboard/Api/PackageCreator.cs | 193 |
1 files changed, 21 insertions, 172 deletions
diff --git a/MediaBrowser.WebDashboard/Api/PackageCreator.cs b/MediaBrowser.WebDashboard/Api/PackageCreator.cs index eaac3e2a0..f2df01976 100644 --- a/MediaBrowser.WebDashboard/Api/PackageCreator.cs +++ b/MediaBrowser.WebDashboard/Api/PackageCreator.cs @@ -1,5 +1,4 @@ using MediaBrowser.Controller.Configuration; -using MediaBrowser.Controller.Localization; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; using System; @@ -8,41 +7,37 @@ using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; -using CommonIO; using MediaBrowser.Controller.Net; -using WebMarkupMin.Core; +using MediaBrowser.Model.Globalization; +using MediaBrowser.Model.IO; namespace MediaBrowser.WebDashboard.Api { public class PackageCreator { private readonly IFileSystem _fileSystem; - private readonly ILocalizationManager _localization; private readonly ILogger _logger; private readonly IServerConfigurationManager _config; - private readonly IJsonSerializer _jsonSerializer; + private readonly IMemoryStreamFactory _memoryStreamFactory; - public PackageCreator(IFileSystem fileSystem, ILocalizationManager localization, ILogger logger, IServerConfigurationManager config, IJsonSerializer jsonSerializer) + public PackageCreator(IFileSystem fileSystem, ILogger logger, IServerConfigurationManager config, IMemoryStreamFactory memoryStreamFactory) { _fileSystem = fileSystem; - _localization = localization; _logger = logger; _config = config; - _jsonSerializer = jsonSerializer; + _memoryStreamFactory = memoryStreamFactory; } public async Task<Stream> GetResource(string path, string mode, string localizationCulture, - string appVersion, - bool enableMinification) + string appVersion) { Stream resourceStream; if (path.Equals("css/all.css", StringComparison.OrdinalIgnoreCase)) { - resourceStream = await GetAllCss(enableMinification).ConfigureAwait(false); - enableMinification = false; + resourceStream = await GetAllCss().ConfigureAwait(false); } else { @@ -57,21 +52,7 @@ namespace MediaBrowser.WebDashboard.Api { if (IsCoreHtml(path)) { - resourceStream = await ModifyHtml(path, resourceStream, mode, appVersion, localizationCulture, enableMinification).ConfigureAwait(false); - } - } - else if (IsFormat(path, "js")) - { - if (path.IndexOf(".min.", StringComparison.OrdinalIgnoreCase) == -1 && path.IndexOf("bower_components", StringComparison.OrdinalIgnoreCase) == -1) - { - resourceStream = await ModifyJs(resourceStream, enableMinification).ConfigureAwait(false); - } - } - else if (IsFormat(path, "css")) - { - if (path.IndexOf(".min.", StringComparison.OrdinalIgnoreCase) == -1 && path.IndexOf("bower_components", StringComparison.OrdinalIgnoreCase) == -1) - { - resourceStream = await ModifyCss(resourceStream, enableMinification).ConfigureAwait(false); + resourceStream = await ModifyHtml(path, resourceStream, mode, appVersion, localizationCulture).ConfigureAwait(false); } } } @@ -116,11 +97,11 @@ namespace MediaBrowser.WebDashboard.Api { var rootPath = DashboardUIPath; - var fullPath = Path.Combine(rootPath, virtualPath.Replace('/', Path.DirectorySeparatorChar)); + var fullPath = Path.Combine(rootPath, virtualPath.Replace('/', _fileSystem.DirectorySeparatorChar)); try { - fullPath = Path.GetFullPath(fullPath); + fullPath = _fileSystem.GetFullPath(fullPath); } catch (Exception ex) { @@ -136,86 +117,6 @@ namespace MediaBrowser.WebDashboard.Api return fullPath; } - public async Task<Stream> ModifyCss(Stream sourceStream, bool enableMinification) - { - using (sourceStream) - { - string content; - - using (var memoryStream = new MemoryStream()) - { - await sourceStream.CopyToAsync(memoryStream).ConfigureAwait(false); - - content = Encoding.UTF8.GetString(memoryStream.ToArray()); - - if (enableMinification) - { - try - { - var result = new KristensenCssMinifier().Minify(content, false, Encoding.UTF8); - - if (result.Errors.Count > 0) - { - _logger.Error("Error minifying css: " + result.Errors[0].Message); - } - else - { - content = result.MinifiedContent; - } - } - catch (Exception ex) - { - _logger.ErrorException("Error minifying css", ex); - } - } - } - - var bytes = Encoding.UTF8.GetBytes(content); - - return new MemoryStream(bytes); - } - } - - public async Task<Stream> ModifyJs(Stream sourceStream, bool enableMinification) - { - using (sourceStream) - { - string content; - - using (var memoryStream = new MemoryStream()) - { - await sourceStream.CopyToAsync(memoryStream).ConfigureAwait(false); - - content = Encoding.UTF8.GetString(memoryStream.ToArray()); - - if (enableMinification) - { - try - { - var result = new CrockfordJsMinifier().Minify(content, false, Encoding.UTF8); - - if (result.Errors.Count > 0) - { - _logger.Error("Error minifying javascript: " + result.Errors[0].Message); - } - else - { - content = result.MinifiedContent; - } - } - catch (Exception ex) - { - _logger.ErrorException("Error minifying javascript", ex); - } - } - } - - var bytes = Encoding.UTF8.GetBytes(content); - - return new MemoryStream(bytes); - } - } - public bool IsCoreHtml(string path) { if (path.IndexOf(".template.html", StringComparison.OrdinalIgnoreCase) != -1) @@ -235,24 +136,20 @@ namespace MediaBrowser.WebDashboard.Api /// <summary> /// Modifies the HTML by adding common meta tags, css and js. /// </summary> - /// <param name="path">The path.</param> - /// <param name="sourceStream">The source stream.</param> - /// <param name="mode">The mode.</param> - /// <param name="appVersion">The application version.</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(string path, Stream sourceStream, string mode, string appVersion, string localizationCulture, bool enableMinification) + public async Task<Stream> ModifyHtml(string path, Stream sourceStream, string mode, string appVersion, string localizationCulture) { using (sourceStream) { string html; - using (var memoryStream = new MemoryStream()) + using (var memoryStream = _memoryStreamFactory.CreateNew()) { await sourceStream.CopyToAsync(memoryStream).ConfigureAwait(false); - html = Encoding.UTF8.GetString(memoryStream.ToArray()); + var originalBytes = memoryStream.ToArray(); + + html = Encoding.UTF8.GetString(originalBytes, 0, originalBytes.Length); if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase)) { @@ -269,7 +166,7 @@ namespace MediaBrowser.WebDashboard.Api html = html.Substring(0, index+7); } } - var mainFile = File.ReadAllText(GetDashboardResourcePath("index.html")); + var mainFile = _fileSystem.ReadAllText(GetDashboardResourcePath("index.html")); html = ReplaceFirst(mainFile, "<div class=\"mainAnimatedPages skinBody\"></div>", "<div class=\"mainAnimatedPages skinBody hide\">" + html + "</div>"); } @@ -280,33 +177,6 @@ namespace MediaBrowser.WebDashboard.Api html = html.Replace("<html", "<html data-culture=\"" + localizationCulture + "\" lang=\"" + lang + "\""); } - - if (enableMinification) - { - try - { - var minifier = new HtmlMinifier(new HtmlMinificationSettings - { - AttributeQuotesRemovalMode = HtmlAttributeQuotesRemovalMode.KeepQuotes, - RemoveOptionalEndTags = false, - RemoveTagsWithoutContent = false - }); - var result = minifier.Minify(html, false); - - 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); - } - } } html = html.Replace("<head>", "<head>" + GetMetaTags(mode) + GetCommonCss(mode, appVersion)); @@ -322,7 +192,7 @@ namespace MediaBrowser.WebDashboard.Api var bytes = Encoding.UTF8.GetBytes(html); - return new MemoryStream(bytes); + return _memoryStreamFactory.CreateNew(bytes); } } @@ -452,9 +322,9 @@ namespace MediaBrowser.WebDashboard.Api /// Gets all CSS. /// </summary> /// <returns>Task{Stream}.</returns> - private async Task<Stream> GetAllCss(bool enableMinification) + private async Task<Stream> GetAllCss() { - var memoryStream = new MemoryStream(); + var memoryStream = _memoryStreamFactory.CreateNew(); var files = new[] { @@ -470,7 +340,7 @@ namespace MediaBrowser.WebDashboard.Api { var path = GetDashboardResourcePath(file); - using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true)) + using (var fs = _fileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, true)) { using (var streamReader = new StreamReader(fs)) { @@ -483,27 +353,6 @@ namespace MediaBrowser.WebDashboard.Api var css = builder.ToString(); - if (enableMinification) - { - try - { - var result = new KristensenCssMinifier().Minify(builder.ToString(), false, Encoding.UTF8); - - 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 bytes = Encoding.UTF8.GetBytes(css); memoryStream.Write(bytes, 0, bytes.Length); @@ -518,7 +367,7 @@ namespace MediaBrowser.WebDashboard.Api /// <returns>Task{Stream}.</returns> private Stream GetRawResourceStream(string path) { - return _fileSystem.GetFileStream(GetDashboardResourcePath(path), FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true); + return _fileSystem.GetFileStream(GetDashboardResourcePath(path), FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, true); } } |
