refactor Output path fill handling

so i can reuse it elsewhere
This commit is contained in:
Alex "mcmonkey" Goodwin
2024-09-14 14:09:36 +09:00
parent c69546016a
commit b8c6ade2eb

View File

@@ -232,68 +232,86 @@ public class User
/// <summary>Simplified keynames for things commonly used in ExtraMeta, to allow for OutputPath builder to treat these cases as empty and not errors.</summary> /// <summary>Simplified keynames for things commonly used in ExtraMeta, to allow for OutputPath builder to treat these cases as empty and not errors.</summary>
public static HashSet<string> KnownExtraMetaVals = ["debugbackend", "scoring", "usedembeddings", "generationtime", "intermediate", "usedwildcards", "swarmversion", "date", "originalprompt", "originalnegativeprompt", "presetsused"]; public static HashSet<string> KnownExtraMetaVals = ["debugbackend", "scoring", "usedembeddings", "generationtime", "intermediate", "usedwildcards", "swarmversion", "date", "originalprompt", "originalnegativeprompt", "presetsused"];
/// <summary>Converts the user's output path setting to a real path for the given parameters. Note that the path is partially cleaned, but not completely.</summary> /// <summary>Helper for filling output path data.</summary>
public string BuildImageOutputPath(T2IParamInput user_input, int batchIndex) public class OutpathFillHelper
{ {
int maxLen = Settings.OutPathBuilder.MaxLenPerPart; public DateTimeOffset Time = DateTimeOffset.Now;
DateTimeOffset time = DateTimeOffset.Now;
string simplifyModel(string model) public T2IParamInput UserInput;
public Dictionary<string, object> ExtraMetaSimplified;
public User LinkedUser;
public bool SkipFolders;
public string BatchID;
public OutpathFillHelper(T2IParamInput user_input, User user, string batchId)
{
UserInput = user_input;
ExtraMetaSimplified = user_input.ExtraMeta.ToDictionary(p => T2IParamTypes.CleanTypeName(p.Key), p => p.Value);
LinkedUser = user;
BatchID = batchId;
SkipFolders = LinkedUser?.Settings?.OutPathBuilder?.ModelPathsSkipFolders ?? false;
}
public string SimplifyModel(string model)
{ {
model = model.Replace('\\', '/').Trim(); model = model.Replace('\\', '/').Trim();
if (model.EndsWith(".safetensors") || model.EndsWith(".sft") || model.EndsWith(".ckpt")) if (model.EndsWith(".safetensors") || model.EndsWith(".sft") || model.EndsWith(".ckpt"))
{ {
model = model.BeforeLast('.'); model = model.BeforeLast('.');
} }
if (Settings.OutPathBuilder.ModelPathsSkipFolders) if (SkipFolders)
{ {
model = model.AfterLast('/'); model = model.AfterLast('/');
} }
return model; return model;
} }
Dictionary<string, object> extraMetaSimplified = user_input.ExtraMeta.ToDictionary(p => T2IParamTypes.CleanTypeName(p.Key), p => p.Value);
string buildPathPart(string part) public string FillPartUnformatted(string part)
{ {
string data = part switch string data = part switch
{ {
"year" => $"{time.Year:0000}", "year" => $"{Time.Year:0000}",
"month" => $"{time.Month:00}", "month" => $"{Time.Month:00}",
"month_name" => $"{time:MMMM}", "month_name" => $"{Time:MMMM}",
"day" => $"{time.Day:00}", "day" => $"{Time.Day:00}",
"day_name" => $"{time:dddd}", "day_name" => $"{Time:dddd}",
"hour" => $"{time.Hour:00}", "hour" => $"{Time.Hour:00}",
"minute" => $"{time.Minute:00}", "minute" => $"{Time.Minute:00}",
"second" => $"{time.Second:00}", "second" => $"{Time.Second:00}",
"prompt" => user_input.Get(T2IParamTypes.Prompt), "prompt" => UserInput.Get(T2IParamTypes.Prompt),
"negative_prompt" => user_input.Get(T2IParamTypes.NegativePrompt), "negative_prompt" => UserInput.Get(T2IParamTypes.NegativePrompt),
"seed" => $"{user_input.Get(T2IParamTypes.Seed)}", "seed" => $"{UserInput.Get(T2IParamTypes.Seed)}",
"cfg_scale" => $"{user_input.Get(T2IParamTypes.CFGScale)}", "cfg_scale" => $"{UserInput.Get(T2IParamTypes.CFGScale)}",
"width" => $"{user_input.GetImageWidth()}", "width" => $"{UserInput.GetImageWidth()}",
"height" => $"{user_input.GetImageHeight()}", "height" => $"{UserInput.GetImageHeight()}",
"steps" => $"{user_input.Get(T2IParamTypes.Steps)}", "steps" => $"{UserInput.Get(T2IParamTypes.Steps)}",
"model" => simplifyModel(user_input.Get(T2IParamTypes.Model)?.Name ?? "unknown"), "model" => SimplifyModel(UserInput.Get(T2IParamTypes.Model)?.Name ?? "unknown"),
"model_title" => user_input.Get(T2IParamTypes.Model)?.Metadata?.Title ?? "unknown", "model_title" => UserInput.Get(T2IParamTypes.Model)?.Metadata?.Title ?? "unknown",
"loras" => user_input.TryGet(T2IParamTypes.Loras, out List<string> loras) ? loras.Select(simplifyModel).JoinString("-") : "", "loras" => UserInput.TryGet(T2IParamTypes.Loras, out List<string> loras) ? loras.Select(SimplifyModel).JoinString("-") : "",
"batch_id" => $"{batchIndex}", "batch_id" => BatchID,
"user_name" => UserID, "user_name" => LinkedUser?.UserID ?? "None",
"number" => "[number]", "number" => "[number]",
_ => null _ => null
}; };
if (data is null) if (data is null)
{ {
data = $"[{part}]"; if (T2IParamTypes.TryGetType(part, out T2IParamType type, UserInput))
if (T2IParamTypes.TryGetType(part, out T2IParamType type, user_input))
{ {
data = ""; data = "";
if (user_input.TryGetRaw(type, out object val)) if (UserInput.TryGetRaw(type, out object val))
{ {
data = $"{T2IParamInput.SimplifyParamVal(val)}"; data = $"{T2IParamInput.SimplifyParamVal(val)}";
if (val is T2IModel model) if (val is T2IModel model)
{ {
data = simplifyModel(data); data = SimplifyModel(data);
} }
} }
} }
else if (extraMetaSimplified.TryGetValue(T2IParamTypes.CleanTypeName(part), out object extraVal)) else if (ExtraMetaSimplified.TryGetValue(T2IParamTypes.CleanTypeName(part), out object extraVal))
{ {
data = $"{T2IParamInput.SimplifyParamVal(extraVal)}"; data = $"{T2IParamInput.SimplifyParamVal(extraVal)}";
} }
@@ -302,6 +320,19 @@ public class User
data = ""; data = "";
} }
} }
return data;
}
}
/// <summary>Converts the user's output path setting to a real path for the given parameters. Note that the path is partially cleaned, but not completely.</summary>
public string BuildImageOutputPath(T2IParamInput user_input, int batchIndex)
{
int maxLen = Settings.OutPathBuilder.MaxLenPerPart;
DateTimeOffset time = DateTimeOffset.Now;
OutpathFillHelper helper = new(user_input, this, $"{batchIndex}");
string buildPathPart(string part)
{
string data = helper.FillPartUnformatted(part) ?? $"[{part}]";
if (data.Length > maxLen) if (data.Length > maxLen)
{ {
data = data[..maxLen]; data = data[..maxLen];