2024-08-31 08:32:06 -07:00

69 lines
2.2 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Shared.Models;
public record FileHolder(DateTime? CreationTime,
string? DirectoryName,
bool Exists,
string ExtensionLowered,
string FullName,
DateTime? LastWriteTime,
long? Length,
string Name,
string NameWithoutExtension)
{
public override string ToString()
{
string result = JsonSerializer.Serialize(this, FileHolderSourceGenerationContext.Default.FileHolder);
return result;
}
private static FileHolder GetExisting(FileInfo fileInfo) =>
new(fileInfo.CreationTime,
fileInfo.DirectoryName,
fileInfo.Exists,
fileInfo.Extension.ToLower(),
fileInfo.FullName,
fileInfo.LastWriteTime,
fileInfo.Length,
fileInfo.Name,
Path.GetFileNameWithoutExtension(fileInfo.FullName));
private static FileHolder GetNonExisting(FileInfo fileInfo) =>
new(null,
fileInfo.DirectoryName,
fileInfo.Exists,
fileInfo.Extension.ToLower(),
fileInfo.FullName,
null,
null,
fileInfo.Name,
Path.GetFileNameWithoutExtension(fileInfo.FullName));
public static FileHolder Get(FileInfo fileInfo) =>
fileInfo.Exists ? GetExisting(fileInfo) : GetNonExisting(fileInfo);
public static FileHolder Get(FilePath filePath)
{
FileHolder result;
result = new(new(filePath.CreationTicks),
filePath.DirectoryName,
true,
filePath.ExtensionLowered,
filePath.FullName,
new(filePath.LastWriteTicks),
filePath.Length,
filePath.Name,
Path.GetFileNameWithoutExtension(filePath.FullName));
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(FileHolder))]
public partial class FileHolderSourceGenerationContext : JsonSerializerContext
{
}