92 lines
4.4 KiB
C#
92 lines
4.4 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using View_by_Distance.Shared.Models.Stateless.Methods;
|
|
|
|
namespace View_by_Distance.Shared.Models;
|
|
|
|
public record FilePath(long CreationTicks,
|
|
string DirectoryName,
|
|
string ExtensionLowered,
|
|
string FileNameFirstSegment,
|
|
string FullName,
|
|
int? Id,
|
|
bool? HasIgnoreKeyword,
|
|
bool? HasDateTimeOriginal,
|
|
bool IsIntelligentIdFormat,
|
|
long LastWriteTicks,
|
|
long Length,
|
|
string Name,
|
|
string NameWithoutExtension,
|
|
int? SortOrder)
|
|
{
|
|
|
|
public override string ToString()
|
|
{
|
|
string result = JsonSerializer.Serialize(this, FilePathSourceGenerationContext.Default.FilePath);
|
|
return result;
|
|
}
|
|
|
|
public static FilePath Get(Properties.IPropertyConfiguration propertyConfiguration, FileHolder fileHolder, int? index)
|
|
{
|
|
if (fileHolder.CreationTime is null)
|
|
throw new NullReferenceException(nameof(fileHolder.CreationTime));
|
|
if (fileHolder.LastWriteTime is null)
|
|
throw new NullReferenceException(nameof(fileHolder.LastWriteTime));
|
|
if (fileHolder.Length is null)
|
|
throw new NullReferenceException(nameof(fileHolder.Length));
|
|
FilePath result;
|
|
int? id;
|
|
int? sortOder;
|
|
string fileNameFirstSegment = fileHolder.Name.Split('.')[0];
|
|
int sortOrderOnlyLengthIndex = propertyConfiguration.Offset.ToString().Length;
|
|
string fileDirectoryName = fileHolder.DirectoryName ?? throw new NullReferenceException();
|
|
bool isIntelligentIdFormat = IId.NameWithoutExtensionIsIntelligentIdFormat(propertyConfiguration, fileNameFirstSegment);
|
|
bool isPaddedIntelligentIdFormat = IId.NameWithoutExtensionIsPaddedIntelligentIdFormat(propertyConfiguration, sortOrderOnlyLengthIndex, fileNameFirstSegment);
|
|
bool fileNameFirstSegmentIsIdFormat = !isPaddedIntelligentIdFormat && !isIntelligentIdFormat && IId.NameWithoutExtensionIsIdFormat(propertyConfiguration, fileHolder);
|
|
bool? hasIgnoreKeyword = !isIntelligentIdFormat && !isPaddedIntelligentIdFormat ? null : fileNameFirstSegment[^1] is '2' or '8';
|
|
bool? hasDateTimeOriginal = !isIntelligentIdFormat && !isPaddedIntelligentIdFormat ? null : fileNameFirstSegment[^1] is '1' or '9';
|
|
if (!fileNameFirstSegmentIsIdFormat && !isIntelligentIdFormat && !isPaddedIntelligentIdFormat)
|
|
(id, sortOder) = (null, null);
|
|
else if (isIntelligentIdFormat)
|
|
(id, sortOder) = (IId.GetId(propertyConfiguration, fileNameFirstSegment), null);
|
|
else if (isPaddedIntelligentIdFormat)
|
|
{
|
|
if (!int.TryParse(fileNameFirstSegment[..sortOrderOnlyLengthIndex], out int absoluteValueOfSortOrder))
|
|
(id, sortOder) = (null, null);
|
|
else
|
|
(id, sortOder) = (IId.GetId(propertyConfiguration, fileNameFirstSegment[sortOrderOnlyLengthIndex..]), absoluteValueOfSortOrder);
|
|
}
|
|
else if (fileNameFirstSegmentIsIdFormat)
|
|
{
|
|
if (index is null)
|
|
throw new NullReferenceException(nameof(index));
|
|
if (!int.TryParse(fileNameFirstSegment, out int valueOfFileNameFirstSegment))
|
|
throw new NotSupportedException();
|
|
(id, sortOder) = (valueOfFileNameFirstSegment, propertyConfiguration.Offset + index);
|
|
}
|
|
else
|
|
throw new NotSupportedException();
|
|
result = new(fileHolder.CreationTime.Value.Ticks,
|
|
fileDirectoryName,
|
|
fileHolder.ExtensionLowered,
|
|
fileNameFirstSegment,
|
|
fileHolder.FullName,
|
|
id,
|
|
hasIgnoreKeyword,
|
|
hasDateTimeOriginal,
|
|
isIntelligentIdFormat,
|
|
fileHolder.LastWriteTime.Value.Ticks,
|
|
fileHolder.Length.Value,
|
|
fileHolder.Name,
|
|
fileHolder.NameWithoutExtension,
|
|
sortOder);
|
|
return result;
|
|
}
|
|
|
|
}
|
|
|
|
[JsonSourceGenerationOptions(WriteIndented = true)]
|
|
[JsonSerializable(typeof(FilePath))]
|
|
public partial class FilePathSourceGenerationContext : JsonSerializerContext
|
|
{
|
|
} |