2
0

PackageCreator.cs 6.7 KB

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