optimize assembly loading for MAUI to use client storage

This commit is contained in:
Shaun Walker
2022-09-11 10:48:40 -04:00
parent b880207f61
commit 2d306e8fda
3 changed files with 267 additions and 129 deletions

View File

@ -18,6 +18,9 @@ using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Diagnostics;
using static System.Net.WebRequestMethods;
namespace Oqtane.Controllers
{
@ -49,7 +52,7 @@ namespace Oqtane.Controllers
[HttpPost]
public async Task<Installation> Post([FromBody] InstallConfig config)
{
var installation = new Installation {Success = false, Message = ""};
var installation = new Installation { Success = false, Message = "" };
if (ModelState.IsValid && (User.IsInRole(RoleNames.Host) || string.IsNullOrEmpty(_configManager.GetSetting("ConnectionStrings:" + SettingKeys.ConnectionStringKey, ""))))
{
@ -85,7 +88,7 @@ namespace Oqtane.Controllers
[Authorize(Roles = RoleNames.Host)]
public Installation Upgrade()
{
var installation = new Installation {Success = true, Message = ""};
var installation = new Installation { Success = true, Message = "" };
_installationManager.UpgradeFramework();
return installation;
}
@ -98,107 +101,169 @@ namespace Oqtane.Controllers
_installationManager.RestartApplication();
}
// GET api/<controller>/load
[HttpGet("load")]
public IActionResult Load()
// GET api/<controller>/list
[HttpGet("list")]
public List<string> List()
{
return File(GetAssemblies(), System.Net.Mime.MediaTypeNames.Application.Octet, "oqtane.dll");
return GetAssemblyList();
}
private byte[] GetAssemblies()
{
return _cache.GetOrCreate("assemblies", entry =>
// GET api/<controller>/load?list=x,y
[HttpGet("load")]
public IActionResult Load(string list = "*")
{
return File(GetAssemblies(list), System.Net.Mime.MediaTypeNames.Application.Octet, "oqtane.dll");
}
private List<string> GetAssemblyList()
{
// get list of assemblies which should be downloaded to client
var binFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var assemblies = AppDomain.CurrentDomain.GetOqtaneClientAssemblies();
var list = assemblies.Select(a => a.GetName().Name).ToList();
// include version numbers
for (int i = 0; i < list.Count; i++)
{
// get list of assemblies which should be downloaded to client
var assemblies = AppDomain.CurrentDomain.GetOqtaneClientAssemblies();
var list = assemblies.Select(a => a.GetName().Name).ToList();
var binFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
list[i] = Path.GetFileName(AddFileDate(Path.Combine(binFolder, list[i] + ".dll")));
}
// insert satellite assemblies at beginning of list
foreach (var culture in _localizationManager.GetInstalledCultures())
// insert satellite assemblies at beginning of list
foreach (var culture in _localizationManager.GetInstalledCultures())
{
var assembliesFolderPath = Path.Combine(binFolder, culture);
if (culture == Constants.DefaultCulture)
{
var assembliesFolderPath = Path.Combine(binFolder, culture);
if (culture == Constants.DefaultCulture)
{
continue;
}
if (Directory.Exists(assembliesFolderPath))
{
foreach (var resourceFile in Directory.EnumerateFiles(assembliesFolderPath))
{
list.Insert(0, Path.Combine(culture, Path.GetFileNameWithoutExtension(resourceFile)));
}
}
else
{
_filelogger.LogError(Utilities.LogMessage(this, $"The Satellite Assembly Folder For {culture} Does Not Exist"));
}
continue;
}
// insert module and theme dependencies at beginning of list
foreach (var assembly in assemblies)
if (Directory.Exists(assembliesFolderPath))
{
foreach (var type in assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(IModule))))
foreach (var resourceFile in Directory.EnumerateFiles(assembliesFolderPath))
{
var instance = Activator.CreateInstance(type) as IModule;
foreach (string name in instance.ModuleDefinition.Dependencies.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
if (System.IO.File.Exists(Path.Combine(binFolder, name + ".dll")))
{
if (!list.Contains(name)) list.Insert(0, name);
}
else
{
_filelogger.LogError(Utilities.LogMessage(this, $"Module {instance.ModuleDefinition.ModuleDefinitionName} Dependency {name}.dll Does Not Exist"));
}
}
list.Insert(0, culture + "/" + Path.GetFileName(AddFileDate(resourceFile)));
}
foreach (var type in assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(ITheme))))
}
else
{
_filelogger.LogError(Utilities.LogMessage(this, $"The Satellite Assembly Folder For {culture} Does Not Exist"));
}
}
// insert module and theme dependencies at beginning of list
foreach (var assembly in assemblies)
{
foreach (var type in assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(IModule))))
{
var instance = Activator.CreateInstance(type) as IModule;
foreach (string name in instance.ModuleDefinition.Dependencies.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
var instance = Activator.CreateInstance(type) as ITheme;
foreach (string name in instance.Theme.Dependencies.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
var path = Path.Combine(binFolder, name + ".dll");
if (System.IO.File.Exists(path))
{
if (System.IO.File.Exists(Path.Combine(binFolder, name + ".dll")))
{
if (!list.Contains(name)) list.Insert(0, name);
}
else
{
_filelogger.LogError(Utilities.LogMessage(this, $"Theme {instance.Theme.ThemeName} Dependency {name}.dll Does Not Exist"));
}
path = Path.GetFileName(AddFileDate(path));
if (!list.Contains(path)) list.Insert(0, path);
}
else
{
_filelogger.LogError(Utilities.LogMessage(this, $"Module {instance.ModuleDefinition.ModuleDefinitionName} Dependency {name}.dll Does Not Exist"));
}
}
}
// create zip file containing assemblies and debug symbols
using (var memoryStream = new MemoryStream())
foreach (var type in assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(ITheme))))
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
var instance = Activator.CreateInstance(type) as ITheme;
foreach (string name in instance.Theme.Dependencies.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
foreach (string file in list)
var path = Path.Combine(binFolder, name + ".dll");
if (System.IO.File.Exists(path))
{
using (var filestream = new FileStream(Path.Combine(binFolder, file + ".dll"), FileMode.Open, FileAccess.Read))
using (var entrystream = archive.CreateEntry(file + ".dll").Open())
path = Path.GetFileName(AddFileDate(path));
if (!list.Contains(path)) list.Insert(0, path);
}
else
{
_filelogger.LogError(Utilities.LogMessage(this, $"Theme {instance.Theme.ThemeName} Dependency {name}.dll Does Not Exist"));
}
}
}
}
return list;
}
private byte[] GetAssemblies(string list)
{
if (list == "*")
{
return _cache.GetOrCreate("assemblies", entry =>
{
return GetZIP(list);
});
}
else
{
return GetZIP(list);
}
}
private byte[] GetZIP(string list)
{
var binFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
// get list of assemblies which should be downloaded to client
List<string> assemblies;
if (list == "*")
{
assemblies = GetAssemblyList();
}
else
{
assemblies = list.Split(',').ToList();
}
// create zip file containing assemblies and debug symbols
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
foreach (string file in assemblies)
{
var filename = RemoveFileDate(file);
if (System.IO.File.Exists(Path.Combine(binFolder, filename)))
{
using (var filestream = new FileStream(Path.Combine(binFolder, filename), FileMode.Open, FileAccess.Read))
using (var entrystream = archive.CreateEntry(file).Open())
{
filestream.CopyTo(entrystream);
}
// include debug symbols
if (System.IO.File.Exists(Path.Combine(binFolder, file + ".pdb")))
}
filename = filename.Replace(".dll", ".pdb");
if (System.IO.File.Exists(Path.Combine(binFolder, filename)))
{
using (var filestream = new FileStream(Path.Combine(binFolder, filename), FileMode.Open, FileAccess.Read))
using (var entrystream = archive.CreateEntry(file.Replace(".dll", ".pdb")).Open())
{
using (var filestream = new FileStream(Path.Combine(binFolder, file + ".pdb"), FileMode.Open, FileAccess.Read))
using (var entrystream = archive.CreateEntry(file + ".pdb").Open())
{
filestream.CopyTo(entrystream);
}
filestream.CopyTo(entrystream);
}
}
}
return memoryStream.ToArray();
}
});
return memoryStream.ToArray();
}
}
private string AddFileDate(string filepath)
{
DateTime lastwritetime = System.IO.File.GetLastWriteTime(filepath);
return Path.GetFileNameWithoutExtension(filepath) + "." + lastwritetime.ToString("yyyyMMddHHmmss") + Path.GetExtension(filepath);
}
private string RemoveFileDate(string filepath)
{
var segments = filepath.Split(".");
return string.Join(".", segments, 0, segments.Length - 2) + Path.GetExtension(filepath);
}
private async Task RegisterContact(string email)