Changed GetDimensions to handle a stream at the end and one exit Switched to using Action? over IDlibDotNet for Tick method Switched to using AsReadOnly over new() Moved Meta Base to Shared
134 lines
6.1 KiB
C#
134 lines
6.1 KiB
C#
using System.Collections.ObjectModel;
|
|
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 DirectoryFullPath,
|
|
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 static FilePath? GetNullSafe(Properties.IPropertyConfiguration propertyConfiguration, FileHolder fileHolder, int? index)
|
|
{
|
|
FilePath? result;
|
|
if (fileHolder.CreationTime is null)
|
|
result = null;
|
|
else if (fileHolder.LastWriteTime is null)
|
|
result = null;
|
|
else if (fileHolder.Length is null)
|
|
result = null;
|
|
else
|
|
result = Get(propertyConfiguration, fileHolder, index);
|
|
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.DirectoryFullPath ?? 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;
|
|
}
|
|
|
|
public static ReadOnlyDictionary<int, ReadOnlyCollection<FilePath>> GetKeyValuePairs(ReadOnlyCollection<ReadOnlyCollection<FilePath>> filePathsCollection)
|
|
{
|
|
Dictionary<int, ReadOnlyCollection<FilePath>> results = [];
|
|
List<FilePath>? collection;
|
|
Dictionary<int, List<FilePath>> keyValuePairs = [];
|
|
foreach (ReadOnlyCollection<FilePath> filePaths in filePathsCollection)
|
|
{
|
|
if (filePaths.Count == 0)
|
|
continue;
|
|
foreach (FilePath filePath in filePaths)
|
|
{
|
|
if (filePath.Id is null)
|
|
continue;
|
|
if (!keyValuePairs.TryGetValue(filePath.Id.Value, out collection))
|
|
{
|
|
keyValuePairs.Add(filePath.Id.Value, []);
|
|
if (!keyValuePairs.TryGetValue(filePath.Id.Value, out collection))
|
|
throw new Exception();
|
|
}
|
|
collection.Add(filePath);
|
|
}
|
|
}
|
|
foreach (KeyValuePair<int, List<FilePath>> keyValuePair in keyValuePairs)
|
|
results.Add(keyValuePair.Key, keyValuePair.Value.AsReadOnly());
|
|
return results.AsReadOnly();
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
string result = JsonSerializer.Serialize(this, FilePathSourceGenerationContext.Default.FilePath);
|
|
return result;
|
|
}
|
|
|
|
}
|
|
|
|
[JsonSourceGenerationOptions(WriteIndented = true)]
|
|
[JsonSerializable(typeof(FilePath))]
|
|
public partial class FilePathSourceGenerationContext : JsonSerializerContext
|
|
{
|
|
} |