Extension.OtherAssets
This commit is contained in:
@@ -15,6 +15,10 @@ public abstract class Extension
|
||||
/// <summary>Optional, filenames (relative to extension directory) of additional CSS files to use, eg "Assets/my_ext.css". You should populate this during <see cref="OnInit"/> or earlier.</summary>
|
||||
public List<string> StyleSheetFiles = [];
|
||||
|
||||
/// <summary>Optional, filenames (relative to extension directory) of additional asset files to use, eg "Assets/my_image.png". You should populate this during <see cref="OnInit"/> or earlier.
|
||||
/// You can link these as in HTML/JS/CSS as "/Extensions/(YourExtName)/(file)", eg "/Extensions/MyExtension/Assets/my_image.png"</summary>
|
||||
public List<string> OtherAssets = [];
|
||||
|
||||
/// <summary>Called when the extension is initialized for the first time, before settings or anything else is loaded, very early in the extension cycle.</summary>
|
||||
public virtual void OnFirstInit()
|
||||
{
|
||||
|
||||
@@ -48,6 +48,9 @@ public class WebServer
|
||||
/// <summary>Extra file content added by extensions.</summary>
|
||||
public Dictionary<string, string> ExtensionSharedFiles = [];
|
||||
|
||||
/// <summary>Extra binary file content added by extensions.</summary>
|
||||
public Dictionary<string, Lazy<byte[]>> ExtensionAssets = [];
|
||||
|
||||
/// <summary>Extra content for the page header. Automatically set based on extensions.</summary>
|
||||
public static HtmlString PageHeaderExtra = new("");
|
||||
|
||||
@@ -220,6 +223,7 @@ public class WebServer
|
||||
{
|
||||
StringBuilder scripts = new(), stylesheets = new(), tabHeader = new(), tabFooter = new();
|
||||
ExtensionSharedFiles.Clear();
|
||||
ExtensionAssets.Clear();
|
||||
Program.RunOnAllExtensions(e =>
|
||||
{
|
||||
foreach (string script in e.ScriptFiles)
|
||||
@@ -234,6 +238,12 @@ public class WebServer
|
||||
ExtensionSharedFiles.Add(fname, File.ReadAllText($"{e.FilePath}{css}"));
|
||||
stylesheets.Append($"<link rel=\"stylesheet\" href=\"{fname}?vary={Utilities.VaryID}\" />");
|
||||
}
|
||||
foreach (string file in e.OtherAssets)
|
||||
{
|
||||
string fname = $"/ExtensionFile/{e.ExtensionName}/{file}";
|
||||
string toRead = $"{e.FilePath}{file}";
|
||||
ExtensionAssets.Add(fname, new(() => File.ReadAllBytes(toRead)));
|
||||
}
|
||||
if (Directory.Exists($"{e.FilePath}/Tabs/Text2Image/"))
|
||||
{
|
||||
foreach (string file in Directory.EnumerateFiles($"{e.FilePath}/Tabs/Text2Image/", "*.html"))
|
||||
@@ -327,6 +337,12 @@ public class WebServer
|
||||
context.Response.StatusCode = 200;
|
||||
await context.Response.WriteAsync(script);
|
||||
}
|
||||
else if (ExtensionAssets.TryGetValue(context.Request.Path.Value, out Lazy<byte[]> data))
|
||||
{
|
||||
context.Response.ContentType = Utilities.GuessContentType(context.Request.Path.Value);
|
||||
context.Response.StatusCode = 200;
|
||||
await context.Response.Body.WriteAsync(data.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
|
||||
Reference in New Issue
Block a user