diff options
Diffstat (limited to 'MediaBrowser.WebDashboard/Api/DashboardService.cs')
| -rw-r--r-- | MediaBrowser.WebDashboard/Api/DashboardService.cs | 125 |
1 files changed, 89 insertions, 36 deletions
diff --git a/MediaBrowser.WebDashboard/Api/DashboardService.cs b/MediaBrowser.WebDashboard/Api/DashboardService.cs index 4850b6fe0..7fcfbfb13 100644 --- a/MediaBrowser.WebDashboard/Api/DashboardService.cs +++ b/MediaBrowser.WebDashboard/Api/DashboardService.cs @@ -1,23 +1,23 @@ using MediaBrowser.Common.Extensions; using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; -using MediaBrowser.Controller.Localization; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Plugins; using MediaBrowser.Model.Extensions; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Net; using MediaBrowser.Model.Serialization; -using ServiceStack; -using ServiceStack.Web; using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; using System.Threading.Tasks; -using CommonIO; -using WebMarkupMin.Core; +using MediaBrowser.Common.Plugins; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Globalization; +using MediaBrowser.Model.Plugins; +using MediaBrowser.Model.Reflection; +using MediaBrowser.Model.Services; namespace MediaBrowser.WebDashboard.Api { @@ -79,19 +79,19 @@ namespace MediaBrowser.WebDashboard.Api /// <summary> /// Class DashboardService /// </summary> - public class DashboardService : IRestfulService, IHasResultFactory + public class DashboardService : IService, IRequiresRequest { /// <summary> /// Gets or sets the logger. /// </summary> /// <value>The logger.</value> - public ILogger Logger { get; set; } + private readonly ILogger _logger; /// <summary> /// Gets or sets the HTTP result factory. /// </summary> /// <value>The HTTP result factory.</value> - public IHttpResultFactory ResultFactory { get; set; } + private readonly IHttpResultFactory _resultFactory; /// <summary> /// Gets or sets the request context. @@ -112,6 +112,8 @@ namespace MediaBrowser.WebDashboard.Api private readonly IFileSystem _fileSystem; private readonly ILocalizationManager _localization; private readonly IJsonSerializer _jsonSerializer; + private readonly IAssemblyInfo _assemblyInfo; + private readonly IMemoryStreamFactory _memoryStreamFactory; /// <summary> /// Initializes a new instance of the <see cref="DashboardService" /> class. @@ -119,13 +121,17 @@ namespace MediaBrowser.WebDashboard.Api /// <param name="appHost">The app host.</param> /// <param name="serverConfigurationManager">The server configuration manager.</param> /// <param name="fileSystem">The file system.</param> - public DashboardService(IServerApplicationHost appHost, IServerConfigurationManager serverConfigurationManager, IFileSystem fileSystem, ILocalizationManager localization, IJsonSerializer jsonSerializer) + public DashboardService(IServerApplicationHost appHost, IServerConfigurationManager serverConfigurationManager, IFileSystem fileSystem, ILocalizationManager localization, IJsonSerializer jsonSerializer, IAssemblyInfo assemblyInfo, ILogger logger, IHttpResultFactory resultFactory, IMemoryStreamFactory memoryStreamFactory) { _appHost = appHost; _serverConfigurationManager = serverConfigurationManager; _fileSystem = fileSystem; _localization = localization; _jsonSerializer = jsonSerializer; + _assemblyInfo = assemblyInfo; + _logger = logger; + _resultFactory = resultFactory; + _memoryStreamFactory = memoryStreamFactory; } /// <summary> @@ -135,9 +141,32 @@ namespace MediaBrowser.WebDashboard.Api /// <returns>System.Object.</returns> public Task<object> Get(GetDashboardConfigurationPage request) { - var page = ServerEntryPoint.Instance.PluginConfigurationPages.First(p => p.Name.Equals(request.Name, StringComparison.OrdinalIgnoreCase)); + IPlugin plugin = null; + Stream stream = null; - return ResultFactory.GetStaticResult(Request, page.Plugin.Version.ToString().GetMD5(), null, null, MimeTypes.GetMimeType("page.html"), () => GetPackageCreator().ModifyHtml("dummy.html", page.GetHtmlStream(), null, _appHost.ApplicationVersion.ToString(), null, false)); + var page = ServerEntryPoint.Instance.PluginConfigurationPages.FirstOrDefault(p => string.Equals(p.Name, request.Name, StringComparison.OrdinalIgnoreCase)); + if (page != null) + { + plugin = page.Plugin; + stream = page.GetHtmlStream(); + } + + if (plugin == null) + { + var altPage = GetPluginPages().FirstOrDefault(p => string.Equals(p.Item1.Name, request.Name, StringComparison.OrdinalIgnoreCase)); + if (altPage != null) + { + plugin = altPage.Item2; + stream = _assemblyInfo.GetManifestResourceStream(plugin.GetType(), altPage.Item1.EmbeddedResourcePath); + } + } + + if (plugin != null && stream != null) + { + return _resultFactory.GetStaticResult(Request, plugin.Version.ToString().GetMD5(), null, null, MimeTypes.GetMimeType("page.html"), () => GetPackageCreator().ModifyHtml("dummy.html", stream, null, _appHost.ApplicationVersion.ToString(), null)); + } + + throw new ResourceNotFoundException(); } /// <summary> @@ -165,7 +194,7 @@ namespace MediaBrowser.WebDashboard.Api if (request.PageType.HasValue) { - pages = pages.Where(p => p.ConfigurationPageType == request.PageType.Value); + pages = pages.Where(p => p.ConfigurationPageType == request.PageType.Value).ToList(); } // Don't allow a failing plugin to fail them all @@ -178,14 +207,38 @@ namespace MediaBrowser.WebDashboard.Api } catch (Exception ex) { - Logger.ErrorException("Error getting plugin information from {0}", ex, p.GetType().Name); + _logger.ErrorException("Error getting plugin information from {0}", ex, p.GetType().Name); return null; } }) .Where(i => i != null) .ToList(); - return ResultFactory.GetOptimizedResult(Request, configPages); + configPages.AddRange(_appHost.Plugins.SelectMany(GetConfigPages)); + + return _resultFactory.GetOptimizedResult(Request, configPages); + } + + private IEnumerable<Tuple<PluginPageInfo, IPlugin>> GetPluginPages() + { + return _appHost.Plugins.SelectMany(GetPluginPages); + } + + private IEnumerable<Tuple<PluginPageInfo, IPlugin>> GetPluginPages(IPlugin plugin) + { + var hasConfig = plugin as IHasWebPages; + + if (hasConfig == null) + { + return new List<Tuple<PluginPageInfo, IPlugin>>(); + } + + return hasConfig.GetPages().Select(i => new Tuple<PluginPageInfo, IPlugin>(i, plugin)); + } + + private IEnumerable<ConfigurationPageInfo> GetConfigPages(IPlugin plugin) + { + return GetPluginPages(plugin).Select(i => new ConfigurationPageInfo(plugin, i.Item1)); } public object Get(GetRobotsTxt request) @@ -231,7 +284,7 @@ namespace MediaBrowser.WebDashboard.Api !contentType.StartsWith("font/", StringComparison.OrdinalIgnoreCase)) { var stream = await GetResourceStream(path, localizationCulture).ConfigureAwait(false); - return ResultFactory.GetResult(stream, contentType); + return _resultFactory.GetResult(stream, contentType); } TimeSpan? cacheDuration = null; @@ -243,11 +296,9 @@ namespace MediaBrowser.WebDashboard.Api cacheDuration = TimeSpan.FromDays(365); } - var assembly = GetType().Assembly.GetName(); - - var cacheKey = (assembly.Version + (localizationCulture ?? string.Empty) + path).GetMD5(); + var cacheKey = (_appHost.ApplicationVersion + (localizationCulture ?? string.Empty) + path).GetMD5(); - return await ResultFactory.GetStaticResult(Request, cacheKey, null, cacheDuration, contentType, () => GetResourceStream(path, localizationCulture)).ConfigureAwait(false); + return await _resultFactory.GetStaticResult(Request, cacheKey, null, cacheDuration, contentType, () => GetResourceStream(path, localizationCulture)).ConfigureAwait(false); } private string GetLocalizationCulture() @@ -263,15 +314,13 @@ namespace MediaBrowser.WebDashboard.Api /// <returns>Task{Stream}.</returns> private Task<Stream> GetResourceStream(string path, string localizationCulture) { - var minify = _serverConfigurationManager.Configuration.EnableDashboardResourceMinification; - return GetPackageCreator() - .GetResource(path, null, localizationCulture, _appHost.ApplicationVersion.ToString(), minify); + .GetResource(path, null, localizationCulture, _appHost.ApplicationVersion.ToString()); } private PackageCreator GetPackageCreator() { - return new PackageCreator(_fileSystem, _localization, Logger, _serverConfigurationManager, _jsonSerializer); + return new PackageCreator(_fileSystem, _logger, _serverConfigurationManager, _memoryStreamFactory); } private List<string> GetDeployIgnoreExtensions() @@ -339,11 +388,17 @@ namespace MediaBrowser.WebDashboard.Api // Try to trim the output size a bit var bowerPath = Path.Combine(path, "bower_components"); - GetDeployIgnoreExtensions().ForEach(i => DeleteFilesByExtension(bowerPath, i)); + foreach (var ext in GetDeployIgnoreExtensions()) + { + DeleteFilesByExtension(bowerPath, ext); + } DeleteFilesByExtension(bowerPath, ".json", "strings\\"); - GetDeployIgnoreFilenames().ForEach(i => DeleteFilesByName(bowerPath, i.Item1, i.Item2)); + foreach (var ignore in GetDeployIgnoreFilenames()) + { + DeleteFilesByName(bowerPath, ignore.Item1, ignore.Item2); + } DeleteFoldersByName(bowerPath, "demo"); DeleteFoldersByName(bowerPath, "test"); @@ -442,9 +497,9 @@ namespace MediaBrowser.WebDashboard.Api private async Task DumpHtml(string source, string destination, string mode, string culture, string appVersion) { - foreach (var file in Directory.GetFiles(source, "*", SearchOption.TopDirectoryOnly)) + foreach (var file in _fileSystem.GetFiles(source)) { - var filename = Path.GetFileName(file); + var filename = file.Name; await DumpFile(filename, Path.Combine(destination, filename), mode, culture, appVersion).ConfigureAwait(false); } @@ -452,9 +507,9 @@ namespace MediaBrowser.WebDashboard.Api private async Task DumpFile(string resourceVirtualPath, string destinationFilePath, string mode, string culture, string appVersion) { - using (var stream = await GetPackageCreator().GetResource(resourceVirtualPath, mode, culture, appVersion, false).ConfigureAwait(false)) + using (var stream = await GetPackageCreator().GetResource(resourceVirtualPath, mode, culture, appVersion).ConfigureAwait(false)) { - using (var fs = _fileSystem.GetFileStream(destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.Read)) + using (var fs = _fileSystem.GetFileStream(destinationFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read)) { stream.CopyTo(fs); } @@ -466,14 +521,12 @@ namespace MediaBrowser.WebDashboard.Api _fileSystem.CreateDirectory(destination); //Now Create all of the directories - foreach (string dirPath in Directory.GetDirectories(source, "*", - SearchOption.AllDirectories)) - _fileSystem.CreateDirectory(dirPath.Replace(source, destination)); + foreach (var dirPath in _fileSystem.GetDirectories(source, true)) + _fileSystem.CreateDirectory(dirPath.FullName.Replace(source, destination)); //Copy all the files & Replaces any files with the same name - foreach (string newPath in Directory.GetFiles(source, "*.*", - SearchOption.AllDirectories)) - _fileSystem.CopyFile(newPath, newPath.Replace(source, destination), true); + foreach (var newPath in _fileSystem.GetFiles(source, true)) + _fileSystem.CopyFile(newPath.FullName, newPath.FullName.Replace(source, destination), true); } } |
