You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.WebDashboard/Api/PackageCreator.cs

152 lines
5.1 KiB

#pragma warning disable CS1591
10 years ago
using System;
using System.Globalization;
10 years ago
using System.IO;
using System.Text;
using System.Threading.Tasks;
using MediaBrowser.Controller;
10 years ago
namespace MediaBrowser.WebDashboard.Api
{
public class PackageCreator
{
private readonly string _basePath;
private readonly IResourceFileManager _resourceFileManager;
10 years ago
public PackageCreator(string basePath, IResourceFileManager resourceFileManager)
10 years ago
{
_basePath = basePath;
_resourceFileManager = resourceFileManager;
10 years ago
}
public async Task<Stream> GetResource(
string virtualPath,
string mode,
10 years ago
string localizationCulture,
string appVersion)
10 years ago
{
var resourcePath = _resourceFileManager.GetResourcePath(_basePath, virtualPath);
Stream resourceStream = File.OpenRead(resourcePath);
10 years ago
if (resourceStream != null && IsCoreHtml(virtualPath))
10 years ago
{
resourceStream = await ModifyHtml(virtualPath, resourceStream, mode, appVersion, localizationCulture).ConfigureAwait(false);
10 years ago
}
return resourceStream;
}
public static bool IsCoreHtml(string path)
10 years ago
{
if (path.IndexOf(".template.html", StringComparison.OrdinalIgnoreCase) != -1)
{
return false;
}
return string.Equals(Path.GetExtension(path), ".html", StringComparison.OrdinalIgnoreCase);
10 years ago
}
5 years ago
// Modifies the HTML by adding common meta tags, css and js.
public async Task<Stream> ModifyHtml(
string path,
Stream sourceStream,
string mode,
string appVersion,
string localizationCulture)
10 years ago
{
var isMainIndexPage = string.Equals(path, "index.html", StringComparison.OrdinalIgnoreCase);
string html;
using (var reader = new StreamReader(sourceStream, Encoding.UTF8))
10 years ago
{
html = await reader.ReadToEndAsync().ConfigureAwait(false);
}
if (isMainIndexPage && !string.IsNullOrWhiteSpace(localizationCulture))
{
var lang = localizationCulture.Split('-')[0];
10 years ago
html = html.Replace("<html", "<html data-culture=\"" + localizationCulture + "\" lang=\"" + lang + "\"", StringComparison.Ordinal);
}
if (isMainIndexPage)
{
html = html.Replace("<head>", "<head>" + GetMetaTags(mode), StringComparison.Ordinal);
}
10 years ago
// Disable embedded scripts from plugins. We'll run them later once resources have loaded
if (html.IndexOf("<script", StringComparison.OrdinalIgnoreCase) != -1)
{
html = html.Replace("<script", "<!--<script", StringComparison.Ordinal);
html = html.Replace("</script>", "</script>-->", StringComparison.Ordinal);
}
9 years ago
if (isMainIndexPage)
{
html = html.Replace("</body>", GetCommonJavascript(mode, appVersion) + "</body>", StringComparison.Ordinal);
}
10 years ago
var bytes = Encoding.UTF8.GetBytes(html);
return new MemoryStream(bytes);
10 years ago
}
/// <summary>
/// Gets the meta tags.
/// </summary>
/// <returns>System.String.</returns>
private static string GetMetaTags(string mode)
10 years ago
{
var sb = new StringBuilder();
if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase)
|| string.Equals(mode, "android", StringComparison.OrdinalIgnoreCase))
{
sb.Append("<meta http-equiv=\"Content-Security-Policy\" content=\"default-src * 'self' 'unsafe-inline' 'unsafe-eval' data: gap: file: filesystem: ws: wss:;\">");
8 years ago
}
10 years ago
return sb.ToString();
}
/// <summary>
/// Gets the common javascript.
/// </summary>
/// <param name="mode">The mode.</param>
/// <param name="version">The version.</param>
/// <returns>System.String.</returns>
private static string GetCommonJavascript(string mode, string version)
{
var builder = new StringBuilder();
9 years ago
builder.Append("<script>");
if (!string.IsNullOrWhiteSpace(mode))
{
builder.AppendFormat(CultureInfo.InvariantCulture, "window.appMode='{0}';", mode);
9 years ago
}
else
9 years ago
{
builder.AppendFormat(CultureInfo.InvariantCulture, "window.dashboardVersion='{0}';", version);
9 years ago
}
9 years ago
builder.Append("</script>");
10 years ago
if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
{
builder.Append("<script src=\"cordova.js\" defer></script>");
}
builder.Append("<script src=\"scripts/apploader.js");
if (!string.IsNullOrWhiteSpace(version))
{
builder.Append("?v=");
builder.Append(version);
}
10 years ago
builder.Append("\" defer></script>");
10 years ago
9 years ago
return builder.ToString();
10 years ago
}
}
}