PackageCreator.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Controller;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Model.IO;
  10. using Microsoft.Extensions.Logging;
  11. namespace MediaBrowser.WebDashboard.Api
  12. {
  13. public class PackageCreator
  14. {
  15. private readonly IFileSystem _fileSystem;
  16. private readonly ILogger _logger;
  17. private readonly IServerConfigurationManager _config;
  18. private readonly string _basePath;
  19. private IResourceFileManager _resourceFileManager;
  20. public PackageCreator(string basePath, IFileSystem fileSystem, ILogger logger, IServerConfigurationManager config, IResourceFileManager resourceFileManager)
  21. {
  22. _fileSystem = fileSystem;
  23. _logger = logger;
  24. _config = config;
  25. _basePath = basePath;
  26. _resourceFileManager = resourceFileManager;
  27. }
  28. public async Task<Stream> GetResource(string virtualPath,
  29. string mode,
  30. string localizationCulture,
  31. string appVersion)
  32. {
  33. var resourceStream = GetRawResourceStream(virtualPath);
  34. if (resourceStream != null)
  35. {
  36. if (IsFormat(virtualPath, "html"))
  37. {
  38. if (IsCoreHtml(virtualPath))
  39. {
  40. resourceStream = await ModifyHtml(virtualPath, resourceStream, mode, appVersion, localizationCulture).ConfigureAwait(false);
  41. }
  42. }
  43. }
  44. return resourceStream;
  45. }
  46. /// <summary>
  47. /// Determines whether the specified path is HTML.
  48. /// </summary>
  49. /// <param name="path">The path.</param>
  50. /// <param name="format">The format.</param>
  51. /// <returns><c>true</c> if the specified path is HTML; otherwise, <c>false</c>.</returns>
  52. private static bool IsFormat(string path, string format)
  53. {
  54. return Path.GetExtension(path).EndsWith(format, StringComparison.OrdinalIgnoreCase);
  55. }
  56. public bool IsCoreHtml(string path)
  57. {
  58. if (path.IndexOf(".template.html", StringComparison.OrdinalIgnoreCase) != -1)
  59. {
  60. return false;
  61. }
  62. return IsFormat(path, "html");
  63. }
  64. /// <summary>
  65. /// Modifies the HTML by adding common meta tags, css and js.
  66. /// </summary>
  67. /// <returns>Task{Stream}.</returns>
  68. public async Task<Stream> ModifyHtml(string path, Stream sourceStream, string mode, string appVersion, string localizationCulture)
  69. {
  70. var isMainIndexPage = string.Equals(path, "index.html", StringComparison.OrdinalIgnoreCase);
  71. using (sourceStream)
  72. {
  73. string html;
  74. using (var memoryStream = new MemoryStream())
  75. {
  76. await sourceStream.CopyToAsync(memoryStream).ConfigureAwait(false);
  77. var originalBytes = memoryStream.ToArray();
  78. html = Encoding.UTF8.GetString(originalBytes, 0, originalBytes.Length);
  79. if (isMainIndexPage)
  80. {
  81. if (!string.IsNullOrWhiteSpace(localizationCulture))
  82. {
  83. var lang = localizationCulture.Split('-').FirstOrDefault();
  84. html = html.Replace("<html", "<html data-culture=\"" + localizationCulture + "\" lang=\"" + lang + "\"");
  85. }
  86. }
  87. }
  88. if (isMainIndexPage)
  89. {
  90. html = html.Replace("<head>", "<head>" + GetMetaTags(mode));
  91. }
  92. // Disable embedded scripts from plugins. We'll run them later once resources have loaded
  93. if (html.IndexOf("<script", StringComparison.OrdinalIgnoreCase) != -1)
  94. {
  95. html = html.Replace("<script", "<!--<script");
  96. html = html.Replace("</script>", "</script>-->");
  97. }
  98. if (isMainIndexPage)
  99. {
  100. html = html.Replace("</body>", GetCommonJavascript(mode, appVersion) + "</body>");
  101. }
  102. var bytes = Encoding.UTF8.GetBytes(html);
  103. return new MemoryStream(bytes);
  104. }
  105. }
  106. /// <summary>
  107. /// Gets the meta tags.
  108. /// </summary>
  109. /// <returns>System.String.</returns>
  110. private string GetMetaTags(string mode)
  111. {
  112. var sb = new StringBuilder();
  113. if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase) ||
  114. string.Equals(mode, "android", StringComparison.OrdinalIgnoreCase))
  115. {
  116. sb.Append("<meta http-equiv=\"Content-Security-Policy\" content=\"default-src * 'self' 'unsafe-inline' 'unsafe-eval' data: gap: file: filesystem: ws: wss:;\">");
  117. }
  118. return sb.ToString();
  119. }
  120. /// <summary>
  121. /// Gets the common javascript.
  122. /// </summary>
  123. /// <param name="mode">The mode.</param>
  124. /// <param name="version">The version.</param>
  125. /// <returns>System.String.</returns>
  126. private string GetCommonJavascript(string mode, string version)
  127. {
  128. var builder = new StringBuilder();
  129. builder.Append("<script>");
  130. if (!string.IsNullOrWhiteSpace(mode))
  131. {
  132. builder.AppendFormat("window.appMode='{0}';", mode);
  133. }
  134. else
  135. {
  136. builder.AppendFormat("window.dashboardVersion='{0}';", version);
  137. }
  138. builder.Append("</script>");
  139. var versionString = string.IsNullOrWhiteSpace(mode) ? "?v=" + version : string.Empty;
  140. var files = new List<string>();
  141. files.Add("scripts/apploader.js" + versionString);
  142. if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
  143. {
  144. files.Insert(0, "cordova.js");
  145. }
  146. var tags = files.Select(s => string.Format("<script src=\"{0}\" defer></script>", s)).ToArray();
  147. builder.Append(string.Join(string.Empty, tags));
  148. return builder.ToString();
  149. }
  150. /// <summary>
  151. /// Gets the raw resource stream.
  152. /// </summary>
  153. private Stream GetRawResourceStream(string virtualPath)
  154. {
  155. return _resourceFileManager.GetResourceFileStream(_basePath, virtualPath);
  156. }
  157. }
  158. }