using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using Oqtane.Models; using Microsoft.AspNetCore.Authorization; using Oqtane.Shared; using System.IO; using System.Reflection; using System.Linq; using Microsoft.AspNetCore.Hosting; using Oqtane.Enums; using Oqtane.Infrastructure; using Oqtane.Repository; using System.Text.Json; using System.Net; // ReSharper disable StringIndexOfIsCultureSpecific.1 namespace Oqtane.Controllers { [Route(ControllerRoutes.ApiRoute)] public class ThemeController : Controller { private readonly IThemeRepository _themes; private readonly IInstallationManager _installationManager; private readonly IWebHostEnvironment _environment; private readonly ILogManager _logger; public ThemeController(IThemeRepository themes, IInstallationManager installationManager, IWebHostEnvironment environment, ILogManager logger) { _themes = themes; _installationManager = installationManager; _environment = environment; _logger = logger; } // GET: api/ [HttpGet] [Authorize(Roles = RoleNames.Registered)] public IEnumerable Get() { return _themes.GetThemes(); } [HttpGet("install")] [Authorize(Roles = RoleNames.Host)] public void InstallThemes() { _logger.Log(LogLevel.Information, this, LogFunction.Create, "Themes Installed"); _installationManager.InstallPackages(); } // DELETE api//xxx [HttpDelete("{themename}")] [Authorize(Roles = RoleNames.Host)] public void Delete(string themename) { List themes = _themes.GetThemes().ToList(); Theme theme = themes.Where(item => item.ThemeName == themename).FirstOrDefault(); if (theme != null && Utilities.GetAssemblyName(theme.ThemeName) != Constants.ClientId) { // remove theme assets if (_installationManager.UninstallPackage(theme.PackageName)) { _logger.Log(LogLevel.Information, this, LogFunction.Delete, "Theme Assets Removed For {ThemeName}", theme.ThemeName); } else { // attempt to delete assemblies based on naming convention foreach (string asset in Directory.GetFiles(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), Utilities.GetTypeName(theme.ThemeName) + "*.*")) { System.IO.File.Delete(asset); } _logger.Log(LogLevel.Warning, this, LogFunction.Delete, "Theme Assets Removed For {ThemeName}. Please Note That Some Assets May Have Been Missed Due To A Missing Asset Manifest. An Asset Manifest Is Only Created If A Theme Is Installed From A Nuget Package.", theme.ThemeName); } // clean up theme static resource folder string assetpath = Path.Combine(_environment.WebRootPath, "Themes", Utilities.GetTypeName(theme.ThemeName)); if (Directory.Exists(assetpath)) { Directory.Delete(assetpath, true); _logger.Log(LogLevel.Information, this, LogFunction.Delete, "Theme Static Resource Folder Removed For {ThemeName}", theme.ThemeName); } // remove theme _themes.DeleteTheme(theme.ThemeName); _logger.Log(LogLevel.Information, this, LogFunction.Delete, "Theme Removed For {ThemeName}", theme.ThemeName); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Theme Delete Attempt {Themename}", themename); HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; } } // GET: api//templates [HttpGet("templates")] [Authorize(Roles = RoleNames.Host)] public List