Skip to content

Commit bac2ea0

Browse files
committed
Return SVG of Image not available when trying to view a missing ComfyUI image
1 parent 11dae61 commit bac2ea0

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

MyApp.ServiceInterface/FileServices.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,38 @@ public object Any(DeleteFiles request)
156156

157157
public async Task<object> Any(GetVariant request)
158158
{
159+
// Missing ComfyUI Image, e.g: view?filename=ComfyUI_00001_.png&type=output&subfolder=
160+
if (request.Path == "view")
161+
{
162+
// Return SVG of Unknown Image with given dimensions (for darkmode)
163+
var (width, height) = GetVariantDimensions(request.Variant);
164+
if (height == null)
165+
height = (int)(width.GetValueOrDefault() * 1344/768m);
166+
if (width == null)
167+
width = (int)(height.GetValueOrDefault() * 768/1344m);
168+
var svg =
169+
$"""
170+
<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}" viewBox="0 0 {width} {height}">
171+
<rect width="100%" height="100%" fill="#364050" />
172+
<text x="50%" y="50%" text-anchor="middle" fill="#9ca3af" font-family="Arial, sans-serif" font-size="14">Image not available</text>
173+
</svg>
174+
""";
175+
return new HttpResult(svg, MimeTypes.ImageSvg);
176+
}
177+
159178
var filePath = request.Path.StartsWith("pub")
160179
? appData.Config.FilesPath.CombineWith(request.Path)
161180
: appData.Config.ArtifactsPath.CombineWith(request.Path[..2], request.Path);
162181
var file = new FileInfo(filePath);
163182
if (!file.Exists)
164-
throw HttpError.NotFound("Artifact not found");
183+
throw HttpError.NotFound("Artifact not found: " + request.Path);
165184

166185
return await GetImageVariant(request, filePath, file);
167186
}
168-
169-
private static async Task<object> GetImageVariant(GetVariant request, string filePath, FileInfo file)
187+
188+
public static (int? width, int? height) GetVariantDimensions(string variant)
170189
{
171-
var options = request.Variant.Split(',');
190+
var options = variant.Split(',');
172191
int? width = null;
173192
int? height = null;
174193
foreach (var option in options)
@@ -190,7 +209,13 @@ private static async Task<object> GetImageVariant(GetVariant request, string fil
190209

191210
if (width == null && height == null)
192211
throw new NotSupportedException("width or height is required");
212+
213+
return (width, height);
214+
}
193215

216+
private static async Task<object> GetImageVariant(GetVariant request, string filePath, FileInfo file)
217+
{
218+
var (width, height) = GetVariantDimensions(request.Variant);
194219
var variantPath = filePath.WithoutExtension();
195220
variantPath += (width != null && height != null
196221
? $"_{width}w{height}h"

0 commit comments

Comments
 (0)