Skip to content

Commit 20309ea

Browse files
committed
= comfiuinodes function '/run' endpoint seems to be working
1 parent ef11136 commit 20309ea

33 files changed

+1052
-450
lines changed

Cli/Options.cs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public void Usage(StringBuilder sb, IRegister _)
5656
sb.ND(1, "-lf / --formats", "List engine supported image formats");
5757
sb.ND(1, "-ln / --namespace (name)", "List registered items in given namespace (specify 'all' to list everything)");
5858
sb.ND(1, "--", "Pass all remaining options to the function");
59+
// sb.ND(1, "-n / --name (name)", "Name of the function to run"); //Note: reserving this in-case it must be a named input
5960
}
6061

6162
public bool ParseArgs(string[] args, IRegister _)

Cli/Program.cs

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ImageFunctions.Core;
2+
using ImageFunctions.Core.FileIO;
23
using ImageFunctions.Core.Logging;
34
using System.Diagnostics;
45

@@ -20,8 +21,7 @@ static int Main(string[] args)
2021
using var register = new CoreRegister(log);
2122
var options = new Options(register, log);
2223
using var layers = new Layers();
23-
using var clerk = new FileClerk();
24-
var main = new Program(register, options, layers, clerk, log);
24+
var main = new Program(register, options, layers, log);
2525
return main.Run(args);
2626
}
2727
catch(Exception e) {
@@ -35,12 +35,11 @@ static int Main(string[] args)
3535
}
3636
}
3737

38-
internal Program(IRegister register, Options options, ILayers layers, IFileClerk clerk, ICoreLog log)
38+
internal Program(IRegister register, Options options, ILayers layers, ICoreLog log)
3939
{
4040
Register = register;
4141
Options = options;
4242
Layers = layers;
43-
Clerk = clerk;
4443
Log = log;
4544
}
4645

@@ -63,9 +62,9 @@ int Run(string[] args)
6362

6463
//save the layers to one or more images
6564
Log.Info($"Saving image {Options.OutputName}");
66-
Clerk.Location = Options.OutputName;
65+
using var clerk = new FileClerk(FileIO, Options.OutputName);
6766
if(Layers.Count > 0) {
68-
Options.Engine.Item.Value.SaveImage(Layers, Clerk, Options.ImageFormat);
67+
Options.Engine.Item.Value.SaveImage(Layers, clerk, Options.ImageFormat);
6968
}
7069
else {
7170
Log.Warning(Note.NoLayersToSave());
@@ -130,21 +129,21 @@ bool LoadImages()
130129
//we're reversing the images since we're using a stack
131130
// so the first image specified should stay on top
132131
// and the last one on the bottom.
133-
foreach(var i in Options.ImageFileNames.Reverse()) {
134-
if(!File.Exists(i)) {
135-
Log.Error(Note.CannotFindInputImage(i));
132+
foreach(var file in Options.ImageFileNames.Reverse()) {
133+
using var clerk = new FileClerk(FileIO, file);
134+
if(!File.Exists(file)) {
135+
Log.Error(Note.CannotFindInputImage(file));
136136
return false;
137137
}
138-
Clerk.Location = i;
139-
Options.Engine.Item.Value.LoadImage(Layers, Clerk);
138+
Options.Engine.Item.Value.LoadImage(Layers, clerk);
140139
}
141140

142141
return true;
143142
}
144143

145-
readonly IFileClerk Clerk;
146144
internal ILayers Layers;
147145
internal IRegister Register;
148146
internal Options Options; //not using ICoreOptions interface to allow access to extra methods
149147
internal ICoreLog Log;
148+
readonly SimpleFileIO FileIO = new();
150149
}

ComfiUINodes/Handlers/FunctionInfo.cs

+15-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace ImageFunctions.ComfiUINodes;
1010

1111
internal static partial class Handlers
1212
{
13+
[HttpRoute("/info")]
1314
public static void FunctionInfo(HttpListenerContext ctx)
1415
{
1516
if(!ctx.EnsureMethodIs(HttpMethod.Get)) { return; }
@@ -51,27 +52,26 @@ public static void FunctionInfo(HttpListenerContext ctx)
5152
}
5253
}
5354

54-
var json = JsonSerializer.Serialize(data, data.GetType(), options: JOptions);
55-
resp.WriteText(json);
55+
resp.WriteJson(data, JOptions);
5656
}
5757

5858
static readonly JsonSerializerOptions JOptions = new() {
5959
IncludeFields = true,
6060
};
6161

62-
static string GetParamDescription(IUsageProvider provider)
63-
{
64-
var usage = provider.GetUsageInfo();
65-
var ud = usage.Description;
66-
67-
StringBuilder description = new();
68-
if((ud?.Descriptions?.Any()).GetValueOrDefault(false)) {
69-
foreach(var txt in usage.Description.Descriptions) {
70-
description.AppendLine(txt);
71-
}
72-
}
73-
return description.ToString();
74-
}
62+
// static string GetParamDescription(IUsageProvider provider)
63+
// {
64+
// var usage = provider.GetUsageInfo();
65+
// var ud = usage.Description;
66+
67+
// StringBuilder description = new();
68+
// if((ud?.Descriptions?.Any()).GetValueOrDefault(false)) {
69+
// foreach(var txt in usage.Description.Descriptions) {
70+
// description.AppendLine(txt);
71+
// }
72+
// }
73+
// return description.ToString();
74+
// }
7575

7676
static UsageParam DetermineParamList(Usage usage, IUsageParameter iup)
7777
{

ComfiUINodes/Handlers/HandleNotFound.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ internal static partial class Handlers
66
{
77
public static void HandleNotFound(HttpListenerContext ctx)
88
{
9-
using HttpListenerResponse resp = ctx.Response;
10-
resp.ContentType = "text/plain";
9+
using var resp = ctx.Response;
1110
resp.StatusCode = (int)HttpStatusCode.NotFound;
12-
1311
string err = $"404 - Not Found '{ctx.Request.Url?.LocalPath}'";
14-
resp.WriteText(err);
12+
resp.WritePlainText(err);
1513
}
1614
}

ComfiUINodes/Handlers/Help.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Net;
2+
3+
namespace ImageFunctions.ComfiUINodes;
4+
5+
internal static partial class Handlers
6+
{
7+
[HttpRoute("/help")]
8+
public static void Help(HttpListenerContext ctx)
9+
{
10+
if(!ctx.EnsureMethodIs(HttpMethod.Get)) { return; }
11+
12+
var list = Program.Server.RoutesList();
13+
ctx.Response.WriteJson(new { routes = list });
14+
}
15+
}

ComfiUINodes/Handlers/JobResult.cs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using ImageFunctions.Core.FileIO;
2+
using System.Net;
3+
using System.Net.Http.Headers;
4+
5+
namespace ImageFunctions.ComfiUINodes;
6+
7+
internal static partial class Handlers
8+
{
9+
[HttpRoute("/result")]
10+
public static void JobResult(HttpListenerContext ctx)
11+
{
12+
if(!ctx.EnsureMethodIs(HttpMethod.Get)) { return; }
13+
using var resp = ctx.Response;
14+
var req = ctx.Request;
15+
16+
var sjob = req.QueryString.Get("job");
17+
if(!TryFindJob(req, resp, sjob, out var job, out var jobId)) {
18+
return;
19+
}
20+
21+
if(job.Status == JobStatusKind.Failed) {
22+
ErrorResponse(resp, HttpStatusCode.OK, (LoggerForJob)job.Context.Log);
23+
JobHoard.Remove(jobId, out _);
24+
return;
25+
}
26+
else if(job.Status != JobStatusKind.Finished) {
27+
resp.WritePlainText($"102 - Job '{jobId}' continues to be processed");
28+
return;
29+
}
30+
31+
//job.Context.Options.
32+
33+
//extract the binary images from layers
34+
var clerk = new RelayClerk("result");
35+
List<NamedMemory> binList = new();
36+
clerk.AqureWrite += (o, e) => {
37+
var nm = new NamedMemory {
38+
Memory = new MemoryStream(),
39+
Name = clerk.GetLabel(e.Name,e.Extension,e.Tag)
40+
};
41+
binList.Add(nm);
42+
e.Source = nm.Memory;
43+
};
44+
var eng = job.Context.Options.Engine.Item.Value;
45+
eng.SaveImage(job.Context.Layers, clerk);
46+
47+
var data = new MultipartContent();
48+
foreach(var nm in binList) {
49+
var content = new ByteArrayContent(nm.Memory.ToArray());
50+
var contentDisp = new ContentDispositionHeaderValue("form-data") { Name = nm.Name };
51+
content.Headers.ContentDisposition = contentDisp;
52+
data.Add(content);
53+
}
54+
55+
//write bins back to to http stream
56+
resp.ContentType = "multipart/form-data";
57+
var canSource = new CancellationTokenSource(TimeSpan.FromMinutes(60));
58+
data.CopyTo(resp.OutputStream, null, canSource.Token);
59+
}
60+
}

0 commit comments

Comments
 (0)