From c8a38c139f2d7e854c8dfc9c38641e1ceeff6d9e Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 16 May 2015 15:09:02 -0400 Subject: [PATCH] lazy load scripts --- MediaBrowser.Api/PackageService.cs | 7 + MediaBrowser.Api/PluginService.cs | 20 ++- MediaBrowser.Model/Updates/PackageInfo.cs | 6 + .../Api/DashboardService.cs | 8 + .../Api/PackageCreator.cs | 146 ++++++++++++++++-- .../MediaBrowser.WebDashboard.csproj | 6 + 6 files changed, 180 insertions(+), 13 deletions(-) diff --git a/MediaBrowser.Api/PackageService.cs b/MediaBrowser.Api/PackageService.cs index 1d792fbc16..5ef8b09871 100644 --- a/MediaBrowser.Api/PackageService.cs +++ b/MediaBrowser.Api/PackageService.cs @@ -56,6 +56,8 @@ namespace MediaBrowser.Api [ApiMember(Name = "IsAdult", Description = "Optional. Filter by package that contain adult content.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")] public bool? IsAdult { get; set; } + + public bool? IsAppStoreEnabled { get; set; } } /// @@ -207,6 +209,11 @@ namespace MediaBrowser.Api packages = packages.Where(p => p.adult == request.IsAdult.Value); } + if (request.IsAppStoreEnabled.HasValue) + { + packages = packages.Where(p => p.enableInAppStore == request.IsAppStoreEnabled.Value); + } + return ToOptimizedResult(packages.ToList()); } diff --git a/MediaBrowser.Api/PluginService.cs b/MediaBrowser.Api/PluginService.cs index 4bd78f1f55..2fbf65f3fd 100644 --- a/MediaBrowser.Api/PluginService.cs +++ b/MediaBrowser.Api/PluginService.cs @@ -25,6 +25,7 @@ namespace MediaBrowser.Api [Authenticated] public class GetPlugins : IReturn> { + public bool? IsAppStoreEnabled { get; set; } } /// @@ -181,6 +182,7 @@ namespace MediaBrowser.Api public async Task Get(GetPlugins request) { var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList(); + var requireAppStoreEnabled = request.IsAppStoreEnabled.HasValue && request.IsAppStoreEnabled.Value; // Don't fail just on account of image url's try @@ -197,10 +199,26 @@ namespace MediaBrowser.Api plugin.ImageUrl = pkg.thumbImage; } } + + if (requireAppStoreEnabled) + { + result = result + .Where(plugin => + { + var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid))); + return pkg != null && pkg.enableInAppStore; + + }) + .ToList(); + } } catch { - + // Play it safe here + if (requireAppStoreEnabled) + { + result = new List(); + } } return ToOptimizedSerializedResultUsingCache(result); diff --git a/MediaBrowser.Model/Updates/PackageInfo.cs b/MediaBrowser.Model/Updates/PackageInfo.cs index 429144f36d..5945f87adb 100644 --- a/MediaBrowser.Model/Updates/PackageInfo.cs +++ b/MediaBrowser.Model/Updates/PackageInfo.cs @@ -153,6 +153,12 @@ namespace MediaBrowser.Model.Updates /// The versions. public List versions { get; set; } + /// + /// Gets or sets a value indicating whether [enable in application store]. + /// + /// true if [enable in application store]; otherwise, false. + public bool enableInAppStore { get; set; } + /// /// Initializes a new instance of the class. /// diff --git a/MediaBrowser.WebDashboard/Api/DashboardService.cs b/MediaBrowser.WebDashboard/Api/DashboardService.cs index 6639494592..07d9ed4979 100644 --- a/MediaBrowser.WebDashboard/Api/DashboardService.cs +++ b/MediaBrowser.WebDashboard/Api/DashboardService.cs @@ -295,6 +295,14 @@ namespace MediaBrowser.WebDashboard.Api var mode = request.Mode; + if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase)) + { + // Overwrite certain files with cordova specific versions + var cordovaVersion = Path.Combine(path, "thirdparty", "cordova", "registrationservices.js"); + File.Copy(cordovaVersion, Path.Combine(path, "scripts", "registrationservices.js"), true); + File.Delete(cordovaVersion); + } + await DumpHtml(creator.DashboardUIPath, path, mode, culture, appVersion); await DumpJs(creator.DashboardUIPath, path, mode, culture, appVersion); diff --git a/MediaBrowser.WebDashboard/Api/PackageCreator.cs b/MediaBrowser.WebDashboard/Api/PackageCreator.cs index f058b421d2..52f96e371e 100644 --- a/MediaBrowser.WebDashboard/Api/PackageCreator.cs +++ b/MediaBrowser.WebDashboard/Api/PackageCreator.cs @@ -38,17 +38,17 @@ namespace MediaBrowser.WebDashboard.Api string appVersion, bool enableMinification) { - var isHtml = IsHtml(path); - Stream resourceStream; if (path.Equals("scripts/all.js", StringComparison.OrdinalIgnoreCase)) { resourceStream = await GetAllJavascript(mode, localizationCulture, appVersion, enableMinification).ConfigureAwait(false); + enableMinification = false; } else if (path.Equals("css/all.css", StringComparison.OrdinalIgnoreCase)) { resourceStream = await GetAllCss(enableMinification).ConfigureAwait(false); + enableMinification = false; } else { @@ -59,7 +59,7 @@ namespace MediaBrowser.WebDashboard.Api { // Don't apply any caching for html pages // jQuery ajax doesn't seem to handle if-modified-since correctly - if (isHtml && path.IndexOf("cordovaindex.html", StringComparison.OrdinalIgnoreCase) == -1) + if (IsHtml(path) && path.IndexOf("cordovaindex.html", StringComparison.OrdinalIgnoreCase) == -1) { resourceStream = await ModifyHtml(resourceStream, mode, localizationCulture, enableMinification).ConfigureAwait(false); } @@ -115,6 +115,86 @@ namespace MediaBrowser.WebDashboard.Api return fullPath; } + public async Task 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 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); + } + } + /// /// Modifies the HTML by adding common meta tags, css and js. /// @@ -135,6 +215,11 @@ namespace MediaBrowser.WebDashboard.Api html = Encoding.UTF8.GetString(memoryStream.ToArray()); + if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase)) + { + html = ModifyForCordova(html); + } + if (!string.IsNullOrWhiteSpace(localizationCulture)) { var lang = localizationCulture.Split('-').FirstOrDefault(); @@ -182,6 +267,22 @@ namespace MediaBrowser.WebDashboard.Api } } + private string ModifyForCordova(string html) + { + // Strip everything between CORDOVA_EXCLUDE_START and CORDOVA_EXCLUDE_END + html = ReplaceBetween(html, "CORDOVA_EXCLUDE_START", "CORDOVA_EXCLUDE_END", string.Empty); + + // Replace CORDOVA_REPLACE_SUPPORTER_SUBMIT_START + html = ReplaceBetween(html, "CORDOVA_REPLACE_SUPPORTER_SUBMIT_START", "CORDOVA_REPLACE_SUPPORTER_SUBMIT_END", "${ButtonDonate}"); + + return html; + } + + private string ReplaceBetween(string html, string startToken, string endToken, string newHtml) + { + return html; + } + private string GetLocalizationToken(string phrase) { return "${" + phrase + "}"; @@ -293,7 +394,14 @@ namespace MediaBrowser.WebDashboard.Api await AppendResource(memoryStream, "thirdparty/jquery.unveil-custom.js", newLineBytes).ConfigureAwait(false); - await AppendLocalization(memoryStream, culture).ConfigureAwait(false); + var excludePhrases = new List(); + + if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase)) + { + excludePhrases.Add("paypal"); + } + + await AppendLocalization(memoryStream, culture, excludePhrases).ConfigureAwait(false); await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false); if (!string.IsNullOrWhiteSpace(mode)) @@ -424,13 +532,11 @@ namespace MediaBrowser.WebDashboard.Api "taskbutton.js", "ratingdialog.js", - "aboutpage.js", "alphapicker.js", "addpluginpage.js", "metadataadvanced.js", "autoorganizetv.js", "autoorganizelog.js", - "channels.js", "channelslatest.js", "channelitems.js", "channelsettings.js", @@ -489,10 +595,6 @@ namespace MediaBrowser.WebDashboard.Api "moviepeople.js", "moviestudios.js", "movietrailers.js", - "musicalbums.js", - "musicalbumartists.js", - "musicartists.js", - "musicrecommended.js", "mypreferencesdisplay.js", "mypreferenceslanguages.js", @@ -538,9 +640,29 @@ namespace MediaBrowser.WebDashboard.Api }; } - private async Task AppendLocalization(Stream stream, string culture) + private async Task AppendLocalization(Stream stream, string culture, List excludePhrases) { - var js = "window.localizationGlossary=" + _jsonSerializer.SerializeToString(_localization.GetJavaScriptLocalizationDictionary(culture)); + var dictionary = _localization.GetJavaScriptLocalizationDictionary(culture); + + if (excludePhrases.Count > 0) + { + var removes = new List(); + + foreach (var pair in dictionary) + { + if (excludePhrases.Any(i => pair.Key.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1 || pair.Value.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1)) + { + removes.Add(pair.Key); + } + } + + foreach (var remove in removes) + { + dictionary.Remove(remove); + } + } + + var js = "window.localizationGlossary=" + _jsonSerializer.SerializeToString(dictionary); var bytes = Encoding.UTF8.GetBytes(js); await stream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); diff --git a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj index b45b3dc3a2..8d52f178fa 100644 --- a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj +++ b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj @@ -156,6 +156,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest @@ -222,6 +225,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest