This commit is contained in:
2024-05-18 17:06:47 -07:00
parent abbe2feac0
commit 30b8e2f5a9
10 changed files with 326 additions and 170 deletions

View File

@ -3,75 +3,97 @@ using System.Text.Json.Serialization;
namespace View_by_Distance.Shared.Models;
public class FileHolder
public record FileHolder(DateTime? CreationTime,
string? DirectoryName,
bool Exists,
string ExtensionLowered,
string FullName,
int? Id,
DateTime? LastWriteTime,
long? Length,
string Name,
string NameWithoutExtension)
{
protected readonly DateTime? _CreationTime;
protected readonly string? _DirectoryName;
protected readonly bool _Exists;
protected readonly string _ExtensionLowered;
protected readonly string _FullName;
protected readonly int? _Id;
protected readonly DateTime? _LastWriteTime;
protected readonly long? _Length;
protected readonly string _Name;
protected readonly string _NameWithoutExtension;
public DateTime? CreationTime => _CreationTime;
public string? DirectoryName => _DirectoryName;
public bool Exists => _Exists;
public string ExtensionLowered => _ExtensionLowered;
public string FullName => _FullName;
public int? Id => _Id;
public DateTime? LastWriteTime => _LastWriteTime;
public long? Length => _Length;
public string Name => _Name;
public string NameWithoutExtension => _NameWithoutExtension;
public FileHolder(DateTime? creationTime, string? directoryName, bool exists, string extensionLowered, string fullName, int? id, DateTime? lastWriteTime, long? length, string name, string nameWithoutExtension)
{
_CreationTime = creationTime;
_DirectoryName = directoryName;
_Exists = exists;
_ExtensionLowered = extensionLowered;
_FullName = fullName;
_Id = id;
_LastWriteTime = lastWriteTime;
_Length = length;
_Name = name;
_NameWithoutExtension = nameWithoutExtension;
}
public FileHolder(FileInfo fileInfo, int? id)
{
if (fileInfo.Exists)
{
_CreationTime = fileInfo.CreationTime;
_LastWriteTime = fileInfo.LastWriteTime;
_Length = fileInfo.Length;
}
_DirectoryName = fileInfo.DirectoryName;
_Exists = fileInfo.Exists;
_ExtensionLowered = fileInfo.Extension.ToLower();
_Id = id;
_FullName = fileInfo.FullName;
_Name = fileInfo.Name;
_NameWithoutExtension = Path.GetFileNameWithoutExtension(fileInfo.FullName);
}
public FileHolder(string fileName) :
this(new FileInfo(fileName), null)
{ }
public FileHolder(string fileName, int? id) :
this(new FileInfo(fileName), id)
{ }
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;
result = new(new(filePath.CreationTicks),
filePath.DirectoryName,
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.DirectoryName,
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.DirectoryName,
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)]