119 lines
4.9 KiB
C#
119 lines
4.9 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(NginxFileSystem nginxFileSystem, int? id) =>
|
|
new(CreationTime: nginxFileSystem.LastModified,
|
|
DirectoryFullPath: Path.GetDirectoryName(nginxFileSystem.URI?.OriginalString ?? throw new Exception()),
|
|
Exists: true,
|
|
ExtensionLowered: Path.GetExtension(nginxFileSystem.Name).ToLower(),
|
|
FullName: nginxFileSystem.URI?.OriginalString ?? throw new Exception(),
|
|
Id: id,
|
|
LastWriteTime: nginxFileSystem.LastModified,
|
|
Length: nginxFileSystem.Length is null ? null : (long)nginxFileSystem.Length.Value,
|
|
Name: nginxFileSystem.Name,
|
|
NameWithoutExtension: Path.GetFileNameWithoutExtension(nginxFileSystem.Name));
|
|
|
|
public static FileHolder Get(NginxFileSystem nginxFileSystem) =>
|
|
GetExisting(nginxFileSystem, id: null);
|
|
|
|
private static FileHolder GetExisting(FileInfo fileInfo, int? id) =>
|
|
new(CreationTime: fileInfo.CreationTime,
|
|
DirectoryFullPath: fileInfo.DirectoryName,
|
|
Exists: fileInfo.Exists,
|
|
ExtensionLowered: fileInfo.Extension.ToLower(),
|
|
FullName: fileInfo.FullName,
|
|
Id: id,
|
|
LastWriteTime: fileInfo.LastWriteTime,
|
|
Length: fileInfo.Length,
|
|
Name: fileInfo.Name,
|
|
NameWithoutExtension: Path.GetFileNameWithoutExtension(fileInfo.FullName));
|
|
|
|
private static FileHolder GetNonExisting(FileInfo fileInfo, int? id) =>
|
|
new(CreationTime: null,
|
|
DirectoryFullPath: fileInfo.DirectoryName,
|
|
Exists: fileInfo.Exists,
|
|
ExtensionLowered: fileInfo.Extension.ToLower(),
|
|
FullName: fileInfo.FullName,
|
|
Id: id,
|
|
LastWriteTime: null,
|
|
Length: null,
|
|
Name: fileInfo.Name,
|
|
NameWithoutExtension: 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(CreationTime: dateTime,
|
|
DirectoryFullPath: filePath.DirectoryFullPath,
|
|
Exists: true,
|
|
ExtensionLowered: filePath.ExtensionLowered,
|
|
FullName: filePath.FullName,
|
|
Id: id,
|
|
LastWriteTime: new(filePath.LastWriteTicks),
|
|
Length: filePath.Length,
|
|
Name: filePath.Name,
|
|
NameWithoutExtension: Path.GetFileNameWithoutExtension(filePath.FullName));
|
|
return result;
|
|
}
|
|
|
|
private static FileHolder GetExisting(FileHolder fileHolder) =>
|
|
new(CreationTime: fileHolder.CreationTime,
|
|
DirectoryFullPath: fileHolder.DirectoryFullPath,
|
|
Exists: fileHolder.Exists,
|
|
ExtensionLowered: fileHolder.ExtensionLowered,
|
|
FullName: fileHolder.FullName,
|
|
Id: null,
|
|
LastWriteTime: fileHolder.LastWriteTime,
|
|
Length: fileHolder.Length,
|
|
Name: fileHolder.Name,
|
|
NameWithoutExtension: Path.GetFileNameWithoutExtension(fileHolder.FullName));
|
|
|
|
private static FileHolder GetNonExisting(FileHolder fileHolder) =>
|
|
new(CreationTime: null,
|
|
DirectoryFullPath: fileHolder.DirectoryFullPath,
|
|
Exists: fileHolder.Exists,
|
|
ExtensionLowered: fileHolder.ExtensionLowered,
|
|
FullName: fileHolder.FullName,
|
|
Id: null,
|
|
LastWriteTime: null,
|
|
Length: null,
|
|
Name: fileHolder.Name,
|
|
NameWithoutExtension: 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
|
|
{
|
|
} |