aa/Shared/Models/FileHolder.cs
Mike Phares 0215e838e7 AA.Compare Project to Match not runToDoCollectionFirst
Removed Layered AppSettings with Nested Objects at First Level
2024-12-28 19:34:09 -07:00

104 lines
3.5 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Shared.Models;
public record FileHolder(DateTime? CreationTime,
string? DirectoryFullPath,
bool Exists,
string ExtensionLowered,
string FullName,
int? Id,
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, int? id) =>
new(fileInfo.CreationTime,
fileInfo.DirectoryName,
fileInfo.Exists,
fileInfo.Extension.ToLower(),
fileInfo.FullName,
id,
fileInfo.LastWriteTime,
fileInfo.Length,
fileInfo.Name,
Path.GetFileNameWithoutExtension(fileInfo.FullName));
private static FileHolder GetNonExisting(FileInfo fileInfo, int? id) =>
new(null,
fileInfo.DirectoryName,
fileInfo.Exists,
fileInfo.Extension.ToLower(),
fileInfo.FullName,
id,
null,
null,
fileInfo.Name,
Path.GetFileNameWithoutExtension(fileInfo.FullName));
public static FileHolder Get(FileInfo fileInfo, int? id) =>
fileInfo.Exists ? GetExisting(fileInfo, id) : GetNonExisting(fileInfo, id);
public static FileHolder Get(FilePath filePath, int? id)
{
FileHolder result;
DateTime dateTime = new(filePath.CreationTicks);
result = new(dateTime,
filePath.DirectoryFullPath,
true,
filePath.ExtensionLowered,
filePath.FullName,
id,
new(filePath.LastWriteTicks),
filePath.Length,
filePath.Name,
Path.GetFileNameWithoutExtension(filePath.FullName));
return result;
}
private static FileHolder GetExisting(FileHolder fileHolder) =>
new(fileHolder.CreationTime,
fileHolder.DirectoryFullPath,
fileHolder.Exists,
fileHolder.ExtensionLowered,
fileHolder.FullName,
Id: null,
fileHolder.LastWriteTime,
fileHolder.Length,
fileHolder.Name,
Path.GetFileNameWithoutExtension(fileHolder.FullName));
private static FileHolder GetNonExisting(FileHolder fileHolder) =>
new(null,
fileHolder.DirectoryFullPath,
fileHolder.Exists,
fileHolder.ExtensionLowered,
fileHolder.FullName,
Id: null,
null,
null,
fileHolder.Name,
Path.GetFileNameWithoutExtension(fileHolder.FullName));
public static FileHolder Get(FileHolder fileHolder) =>
fileHolder.Exists ? GetExisting(fileHolder) : GetNonExisting(fileHolder);
public static FileHolder Get(string file) =>
Get(new FileInfo(file), id: null);
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(FileHolder))]
internal partial class FileHolderSourceGenerationContext : JsonSerializerContext
{
}