Improved asset service. Removed now unused plugin asset streaming endpoint
This commit is contained in:
@@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Moonlight.ApiServer.Services;
|
||||
using Moonlight.Shared.Http.Responses.Assets;
|
||||
|
||||
namespace Moonlight.ApiServer.Http.Controllers;
|
||||
namespace Moonlight.ApiServer.Http.Controllers.Assets;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/assets")]
|
||||
@@ -20,8 +20,8 @@ public class AssetsController : Controller
|
||||
{
|
||||
return new FrontendAssetResponse()
|
||||
{
|
||||
CssFiles = AssetService.CssFiles.ToArray(),
|
||||
JavascriptFiles = AssetService.JavascriptFiles.ToArray(),
|
||||
CssFiles = AssetService.GetCssAssets(),
|
||||
JavascriptFiles = AssetService.GetJavascriptAssets(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,19 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Models;
|
||||
using Moonlight.ApiServer.Services;
|
||||
using Moonlight.Shared.Http.Responses.PluginsStream;
|
||||
|
||||
namespace Moonlight.ApiServer.Http.Controllers;
|
||||
namespace Moonlight.ApiServer.Http.Controllers.Assets;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/pluginsStream")]
|
||||
public class PluginsStreamController : Controller
|
||||
[Route("api/assets/plugins")]
|
||||
public class AssetsPluginsController : Controller
|
||||
{
|
||||
private readonly PluginService PluginService;
|
||||
private readonly IMemoryCache Cache;
|
||||
|
||||
public PluginsStreamController(PluginService pluginService, IMemoryCache cache)
|
||||
public AssetsPluginsController(PluginService pluginService)
|
||||
{
|
||||
PluginService = pluginService;
|
||||
Cache = cache;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@@ -29,7 +25,7 @@ public class PluginsStreamController : Controller
|
||||
[HttpGet("stream")]
|
||||
public async Task GetAssembly([FromQuery(Name = "assembly")] string assembly)
|
||||
{
|
||||
var assembliesMap = PluginService.AssemblyMap;
|
||||
var assembliesMap = PluginService.ClientAssemblyMap;
|
||||
|
||||
if (assembliesMap.ContainsKey(assembly))
|
||||
throw new HttpApiException("The requested assembly could not be found", 404);
|
||||
@@ -38,10 +34,4 @@ public class PluginsStreamController : Controller
|
||||
|
||||
await Results.File(path).ExecuteAsync(HttpContext);
|
||||
}
|
||||
|
||||
[HttpGet("assets")]
|
||||
public Task<PluginsAssetManifest> GetAssetManifest()
|
||||
{
|
||||
return Task.FromResult(PluginService.PluginsAssetManifest);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,63 @@ namespace Moonlight.ApiServer.Services;
|
||||
[Singleton]
|
||||
public class AssetService
|
||||
{
|
||||
public readonly List<string> CssFiles = new();
|
||||
public readonly List<string> JavascriptFiles = new();
|
||||
public string[] CssFiles { get; private set; }
|
||||
public string[] JavascriptFiles { get; private set; }
|
||||
|
||||
private bool HasBeenCollected = false;
|
||||
|
||||
private readonly List<string> AdditionalCssAssets = new();
|
||||
private readonly List<string> AdditionalJavascriptAssets = new();
|
||||
|
||||
private readonly PluginService PluginService;
|
||||
|
||||
public AssetService(PluginService pluginService)
|
||||
{
|
||||
PluginService = pluginService;
|
||||
}
|
||||
|
||||
public void CollectAssets()
|
||||
{
|
||||
// CSS
|
||||
var cssFiles = new List<string>();
|
||||
|
||||
cssFiles.AddRange(AdditionalCssAssets);
|
||||
cssFiles.AddRange(PluginService.AssetMap.Keys.Where(x => x.EndsWith(".css")));
|
||||
|
||||
CssFiles = cssFiles.ToArray();
|
||||
|
||||
// Javascript
|
||||
var jsFiles = new List<string>();
|
||||
|
||||
jsFiles.AddRange(AdditionalJavascriptAssets);
|
||||
jsFiles.AddRange(PluginService.AssetMap.Keys.Where(x => x.EndsWith(".js")));
|
||||
|
||||
JavascriptFiles = jsFiles.ToArray();
|
||||
}
|
||||
|
||||
public void AddCssAsset(string asset)
|
||||
=> AdditionalCssAssets.Add(asset);
|
||||
|
||||
public void AddJavascriptAsset(string asset)
|
||||
=> AdditionalJavascriptAssets.Add(asset);
|
||||
|
||||
public string[] GetCssAssets()
|
||||
{
|
||||
if (HasBeenCollected)
|
||||
return CssFiles;
|
||||
|
||||
CollectAssets();
|
||||
|
||||
return CssFiles;
|
||||
}
|
||||
|
||||
public string[] GetJavascriptAssets()
|
||||
{
|
||||
if (HasBeenCollected)
|
||||
return JavascriptFiles;
|
||||
|
||||
CollectAssets();
|
||||
|
||||
return JavascriptFiles;
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,10 @@ namespace Moonlight.ApiServer.Services;
|
||||
|
||||
public class PluginService
|
||||
{
|
||||
public readonly List<PluginMeta> Plugins = new();
|
||||
public readonly Dictionary<string, string> AssetMap = new();
|
||||
public HostedPluginsManifest HostedPluginsManifest;
|
||||
public PluginsAssetManifest PluginsAssetManifest;
|
||||
public Dictionary<string, string> AssemblyMap;
|
||||
public List<PluginMeta> Plugins { get; private set; } = new();
|
||||
public Dictionary<string, string> AssetMap { get; private set; } = new();
|
||||
public HostedPluginsManifest HostedPluginsManifest { get; private set; }
|
||||
public Dictionary<string, string> ClientAssemblyMap { get; private set; }
|
||||
|
||||
private static string PluginsFolder = PathBuilder.Dir("storage", "plugins");
|
||||
private readonly ILogger<PluginService> Logger;
|
||||
@@ -75,7 +74,7 @@ public class PluginService
|
||||
continue;
|
||||
|
||||
Logger.LogError(
|
||||
"Unable to load plugin '{id}' ({path}) because the dependency {dependency} is missing",
|
||||
"Unable to load plugin '{id}' ({path}) because the dependency '{dependency}' is missing",
|
||||
plugin.Manifest.Id,
|
||||
plugin.Path,
|
||||
dependency
|
||||
@@ -90,24 +89,17 @@ public class PluginService
|
||||
Plugins.RemoveAll(x => pluginsToNotLoad.Contains(x.Manifest.Id));
|
||||
|
||||
// Generate assembly map for client
|
||||
AssemblyMap = GetAssemblies("client");
|
||||
ClientAssemblyMap = GetAssemblies("client");
|
||||
|
||||
// Generate plugin stream manifest for client
|
||||
HostedPluginsManifest = new()
|
||||
{
|
||||
Assemblies = AssemblyMap.Keys.ToArray(),
|
||||
Assemblies = ClientAssemblyMap.Keys.ToArray(),
|
||||
Entrypoints = GetEntrypoints("client")
|
||||
};
|
||||
|
||||
// Generate asset map
|
||||
GenerateAssetMap();
|
||||
|
||||
// Generate asset manifest
|
||||
PluginsAssetManifest = new()
|
||||
{
|
||||
CssFiles = AssetMap.Keys.Where(x => x.EndsWith(".css")).ToArray(),
|
||||
JavascriptFiles = AssetMap.Keys.Where(x => x.EndsWith(".js")).ToArray(),
|
||||
};
|
||||
}
|
||||
|
||||
public Dictionary<string, string> GetAssemblies(string section)
|
||||
@@ -116,7 +108,12 @@ public class PluginService
|
||||
|
||||
foreach (var plugin in Plugins)
|
||||
{
|
||||
foreach (var file in Directory.EnumerateFiles(PathBuilder.Dir(plugin.Path, "bin", section)))
|
||||
var binaryPath = PathBuilder.Dir(plugin.Path, "bin", section);
|
||||
|
||||
if(!Directory.Exists(binaryPath))
|
||||
continue;
|
||||
|
||||
foreach (var file in Directory.EnumerateFiles(binaryPath))
|
||||
{
|
||||
if (!file.EndsWith(".dll"))
|
||||
continue;
|
||||
|
||||
@@ -176,10 +176,10 @@ public class Startup
|
||||
switch (extension)
|
||||
{
|
||||
case ".css":
|
||||
assetService.CssFiles.Add(nextArg);
|
||||
assetService.AddCssAsset(nextArg);
|
||||
break;
|
||||
case ".js":
|
||||
assetService.JavascriptFiles.Add(nextArg);
|
||||
assetService.AddJavascriptAsset(nextArg);
|
||||
break;
|
||||
default:
|
||||
Logger.LogWarning("Unknown asset extension {extension}. Ignoring it", extension);
|
||||
|
||||
Reference in New Issue
Block a user