module installer

This commit is contained in:
Shaun Walker
2019-09-07 23:26:19 -04:00
parent a84eee8782
commit f60898dbc7
12 changed files with 170 additions and 6 deletions

View File

@ -2,6 +2,13 @@
using Microsoft.AspNetCore.Mvc;
using Oqtane.Repository;
using Oqtane.Models;
using Oqtane.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Hosting;
using System.IO.Compression;
using Microsoft.AspNetCore.Hosting;
using System.IO;
using System.Reflection;
namespace Oqtane.Controllers
{
@ -9,10 +16,14 @@ namespace Oqtane.Controllers
public class ModuleDefinitionController : Controller
{
private readonly IModuleDefinitionRepository ModuleDefinitions;
private readonly IHostApplicationLifetime HostApplicationLifetime;
private readonly IWebHostEnvironment environment;
public ModuleDefinitionController(IModuleDefinitionRepository ModuleDefinitions)
public ModuleDefinitionController(IModuleDefinitionRepository ModuleDefinitions, IHostApplicationLifetime HostApplicationLifetime, IWebHostEnvironment environment)
{
this.ModuleDefinitions = ModuleDefinitions;
this.HostApplicationLifetime = HostApplicationLifetime;
this.environment = environment;
}
// GET: api/<controller>
@ -21,5 +32,42 @@ namespace Oqtane.Controllers
{
return ModuleDefinitions.GetModuleDefinitions();
}
[HttpGet("install")]
[Authorize(Roles = Constants.HostRole)]
public void InstallModules()
{
bool install = false;
string modulefolder = Path.Combine(environment.WebRootPath, "Modules");
string binfolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
// iterate through module packages
foreach (string packagename in Directory.GetFiles(modulefolder, "*.nupkg"))
{
// iterate through files and deploy to appropriate locations
using (ZipArchive archive = ZipFile.OpenRead(packagename))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
string filename = Path.GetFileName(entry.FullName);
switch (Path.GetExtension(filename))
{
case ".dll":
entry.ExtractToFile(Path.Combine(binfolder, filename));
break;
}
}
}
// remove module package
System.IO.File.Delete(packagename);
install = true;
}
if (install)
{
// restart application
HostApplicationLifetime.StopApplication();
}
}
}
}