using LiteDB;
using StabilityMatrix.Core.Extensions;
namespace StabilityMatrix.Core.Models.Database;
///
/// Represents a locally indexed model file.
///
public record LocalModelFile
{
///
/// Relative path to the file from the root model directory.
///
[BsonId]
public required string RelativePath { get; init; }
///
/// Type of the model file.
///
public required SharedFolderType SharedFolderType { get; set; }
///
/// Optional connected model information.
///
public ConnectedModelInfo? ConnectedModelInfo { get; set; }
///
/// Optional preview image relative path.
///
public string? PreviewImageRelativePath { get; set; }
///
/// Optional preview image full path. Takes priority over .
///
public string? PreviewImageFullPath { get; set; }
///
/// Whether or not an update is available for this model
///
public bool HasUpdate { get; set; }
///
/// Last time this model was checked for an update
///
public DateTimeOffset LastUpdateCheck { get; set; }
///
/// File name of the relative path.
///
[BsonIgnore]
public string FileName => Path.GetFileName(RelativePath);
///
/// File name of the relative path without extension.
///
[BsonIgnore]
public string FileNameWithoutExtension => Path.GetFileNameWithoutExtension(RelativePath);
///
/// Relative file path from the shared folder type model directory.
///
[BsonIgnore]
public string RelativePathFromSharedFolder =>
Path.GetRelativePath(SharedFolderType.GetStringValue(), RelativePath);
///
/// Blake3 hash of the file.
///
public string? HashBlake3 => ConnectedModelInfo?.Hashes.BLAKE3;
[BsonIgnore]
public string FullPathGlobal => GetFullPath(GlobalConfig.LibraryDir.JoinDir("Models"));
[BsonIgnore]
public string? PreviewImageFullPathGlobal =>
PreviewImageFullPath ?? GetPreviewImageFullPath(GlobalConfig.LibraryDir.JoinDir("Models"));
[BsonIgnore]
public Uri? PreviewImageUriGlobal =>
PreviewImageFullPathGlobal == null ? null : new Uri(PreviewImageFullPathGlobal);
[BsonIgnore]
public string DisplayModelName => ConnectedModelInfo?.ModelName ?? FileNameWithoutExtension;
[BsonIgnore]
public string DisplayModelVersion => ConnectedModelInfo?.VersionName ?? string.Empty;
[BsonIgnore]
public string DisplayModelFileName => FileName;
public string GetFullPath(string rootModelDirectory)
{
return Path.Combine(rootModelDirectory, RelativePath);
}
public string? GetPreviewImageFullPath(string rootModelDirectory)
{
if (PreviewImageFullPath != null)
return PreviewImageFullPath;
return PreviewImageRelativePath == null
? null
: Path.Combine(rootModelDirectory, PreviewImageRelativePath);
}
/*protected bool Equals(LocalModelFile other)
{
return RelativePath == other.RelativePath;
}
///
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != this.GetType())
return false;
return Equals((LocalModelFile)obj);
}
///
public override int GetHashCode()
{
return RelativePath.GetHashCode();
}*/
public static readonly HashSet SupportedCheckpointExtensions =
[
".safetensors",
".pt",
".ckpt",
".pth",
".bin"
];
public static readonly HashSet SupportedImageExtensions = [".png", ".jpg", ".jpeg", ".webp"];
public static readonly HashSet SupportedMetadataExtensions = [".json"];
}