You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.5 KiB
69 lines
2.5 KiB
1 year ago
|
using System.Text.Json;
|
||
1 year ago
|
using System.Text.Json.Serialization;
|
||
1 year ago
|
using StabilityMatrix.Core.Models.Api;
|
||
1 year ago
|
|
||
1 year ago
|
namespace StabilityMatrix.Core.Models;
|
||
1 year ago
|
|
||
|
public class ConnectedModelInfo
|
||
|
{
|
||
1 year ago
|
[JsonIgnore]
|
||
|
public const string FileExtension = ".cm-info.json";
|
||
|
|
||
1 year ago
|
public int ModelId { get; set; }
|
||
|
public string ModelName { get; set; }
|
||
|
public string ModelDescription { get; set; }
|
||
|
public bool Nsfw { get; set; }
|
||
|
public string[] Tags { get; set; }
|
||
|
public CivitModelType ModelType { get; set; }
|
||
|
public int VersionId { get; set; }
|
||
|
public string VersionName { get; set; }
|
||
|
public string VersionDescription { get; set; }
|
||
|
public string? BaseModel { get; set; }
|
||
|
public CivitFileMetadata FileMetadata { get; set; }
|
||
1 year ago
|
public DateTimeOffset ImportedAt { get; set; }
|
||
1 year ago
|
public CivitFileHashes Hashes { get; set; }
|
||
|
|
||
|
// User settings
|
||
|
public string? UserTitle { get; set; }
|
||
|
public string? ThumbnailImageUrl { get; set; }
|
||
|
|
||
1 year ago
|
public ConnectedModelInfo()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public ConnectedModelInfo(CivitModel civitModel, CivitModelVersion civitModelVersion, CivitFile civitFile, DateTimeOffset importedAt)
|
||
1 year ago
|
{
|
||
|
ModelId = civitModel.Id;
|
||
|
ModelName = civitModel.Name;
|
||
|
ModelDescription = civitModel.Description;
|
||
|
Nsfw = civitModel.Nsfw;
|
||
|
Tags = civitModel.Tags;
|
||
|
ModelType = civitModel.Type;
|
||
|
VersionId = civitModelVersion.Id;
|
||
|
VersionName = civitModelVersion.Name;
|
||
|
VersionDescription = civitModelVersion.Description;
|
||
|
ImportedAt = importedAt;
|
||
|
BaseModel = civitModelVersion.BaseModel;
|
||
|
FileMetadata = civitFile.Metadata;
|
||
|
Hashes = civitFile.Hashes;
|
||
|
}
|
||
|
|
||
|
public static ConnectedModelInfo? FromJson(string json)
|
||
|
{
|
||
1 year ago
|
return JsonSerializer.Deserialize<ConnectedModelInfo>(json, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull } );
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Saves the model info to a json file in the specified directory.
|
||
|
/// Overwrites existing files.
|
||
|
/// </summary>
|
||
|
/// <param name="directoryPath">Path of directory to save file</param>
|
||
|
/// <param name="modelFileName">Model file name without extensions</param>
|
||
|
public async Task SaveJsonToDirectory(string directoryPath, string modelFileName)
|
||
|
{
|
||
1 year ago
|
var name = modelFileName + FileExtension;
|
||
1 year ago
|
var json = JsonSerializer.Serialize(this);
|
||
|
await File.WriteAllTextAsync(Path.Combine(directoryPath, name), json);
|
||
1 year ago
|
}
|
||
|
}
|