IntelligentIdRecord
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
using System.Text;
|
||||
using View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
|
||||
namespace View_by_Distance.Shared.Models.Stateless;
|
||||
@ -5,26 +6,59 @@ namespace View_by_Distance.Shared.Models.Stateless;
|
||||
internal abstract class Id
|
||||
{
|
||||
|
||||
internal static bool NameWithoutExtensionIsIdFormat(string fileNameWithoutExtension)
|
||||
private static int GetId(MetadataConfiguration metadataConfiguration, string intelligentId)
|
||||
{
|
||||
bool result;
|
||||
int intMinValueLength = int.MinValue.ToString().Length;
|
||||
if (fileNameWithoutExtension.Length < 5 || fileNameWithoutExtension.Length > intMinValueLength)
|
||||
result = false;
|
||||
else
|
||||
{
|
||||
bool skipOneAllAreNumbers = fileNameWithoutExtension[1..].All(l => char.IsNumber(l));
|
||||
result = (skipOneAllAreNumbers && fileNameWithoutExtension[0] == '-') || (skipOneAllAreNumbers && char.IsNumber(fileNameWithoutExtension[0]));
|
||||
}
|
||||
int result;
|
||||
StringBuilder results = new();
|
||||
if (metadataConfiguration.IntMinValueLength < 4)
|
||||
throw new NotSupportedException();
|
||||
for (int j = intelligentId.Length - 4; j > -1; j--)
|
||||
_ = results.Append(intelligentId[j]);
|
||||
_ = results.Append(intelligentId[^3]).Append(intelligentId[^2]);
|
||||
result = int.Parse(results.ToString());
|
||||
if (intelligentId[^1] is '1' or '2')
|
||||
result *= -1;
|
||||
else if (intelligentId[^1] is not '9' and not '8')
|
||||
throw new NotSupportedException();
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static FilePath GetFilePath(FilePath filePath, string file)
|
||||
private static IntelligentIdRecord GetIntelligentIdRecord(MetadataConfiguration metadataConfiguration, long id, bool ignore)
|
||||
{
|
||||
FilePath result;
|
||||
string fileName = Path.GetFileName(file);
|
||||
string fileExtensionLowered = Path.GetExtension(file).ToLower();
|
||||
result = new(filePath.DirectoryName, fileExtensionLowered, file, filePath.Id, filePath.IsIdFormat, filePath.IsPaddedIdFormat, fileName, filePath.NameWithoutExtension);
|
||||
IntelligentIdRecord result;
|
||||
StringBuilder stringBuilder = new();
|
||||
if (metadataConfiguration.IntMinValueLength < 4)
|
||||
throw new NotSupportedException();
|
||||
int key;
|
||||
string value;
|
||||
if (id > -1)
|
||||
{
|
||||
key = ignore ? 8 : 9;
|
||||
value = id.ToString().PadLeft(metadataConfiguration.IntMinValueLength, '0');
|
||||
}
|
||||
else
|
||||
{
|
||||
key = ignore ? 2 : 1;
|
||||
value = id.ToString()[1..].PadLeft(metadataConfiguration.IntMinValueLength, '0');
|
||||
}
|
||||
for (int j = value.Length - 3; j > -1; j--)
|
||||
_ = stringBuilder.Append(value[j]);
|
||||
result = new(key, value[^2], value[^1], stringBuilder.ToString());
|
||||
return result;
|
||||
}
|
||||
|
||||
private static string GetIntelligentId(IntelligentIdRecord intelligentId) =>
|
||||
$"{intelligentId.Reverse}{intelligentId.GroupChar2}{intelligentId.GroupChar1}{intelligentId.Key}";
|
||||
|
||||
internal static string GetPaddedId(MetadataConfiguration metadataConfiguration, int index, int id)
|
||||
{
|
||||
string result;
|
||||
IntelligentIdRecord intelligentIdRecord = GetIntelligentIdRecord(metadataConfiguration, id, ignore: false);
|
||||
string intelligentId = GetIntelligentId(intelligentIdRecord);
|
||||
int check = GetId(metadataConfiguration, intelligentId);
|
||||
if (check != id)
|
||||
throw new NotSupportedException();
|
||||
result = $"{metadataConfiguration.Offset + index}{intelligentId}";
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -32,44 +66,30 @@ internal abstract class Id
|
||||
{
|
||||
FilePath result;
|
||||
int? id;
|
||||
short? multiplier;
|
||||
char negativeMarker;
|
||||
int absoluteValueOfId;
|
||||
int? sortOder;
|
||||
string fileName = Path.GetFileName(file);
|
||||
string[] segments = Path.GetFileName(fileName).Split('.');
|
||||
string fileExtensionLowered = Path.GetExtension(file).ToLower();
|
||||
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
|
||||
string fileDirectoryName = Path.GetDirectoryName(file) ?? throw new NullReferenceException();
|
||||
short sortOrderOnlyLengthIndex = IId.GetSortOrderOnlyLengthIndex(metadataConfiguration.Offset);
|
||||
bool nameWithoutExtensionIsIdFormat = IId.NameWithoutExtensionIsIdFormat(fileNameWithoutExtension);
|
||||
bool nameWithoutExtensionIsPaddedIdFormat = IId.NameWithoutExtensionIsPaddedIdFormat(fileNameWithoutExtension, sortOrderOnlyLengthIndex);
|
||||
if (!nameWithoutExtensionIsIdFormat && !nameWithoutExtensionIsPaddedIdFormat)
|
||||
id = null;
|
||||
else if (nameWithoutExtensionIsIdFormat)
|
||||
short sortOrderOnlyLengthIndex = IId.GetSortOrderOnlyLengthIndex(metadataConfiguration);
|
||||
string fileNameFirstSegment = segments[0];
|
||||
bool fileNameFirstSegmentIsIntelligentIdFormat = IId.NameWithoutExtensionIsIntelligentIdFormat(metadataConfiguration, fileNameFirstSegment);
|
||||
bool fileNameFirstSegmentIsPaddedIntelligentIdFormat = IId.NameWithoutExtensionIsPaddedIntelligentIdFormat(metadataConfiguration, sortOrderOnlyLengthIndex, fileNameFirstSegment);
|
||||
if (!fileNameFirstSegmentIsIntelligentIdFormat && !fileNameFirstSegmentIsPaddedIntelligentIdFormat)
|
||||
(id, sortOder) = (null, null);
|
||||
else if (fileNameFirstSegmentIsIntelligentIdFormat)
|
||||
(id, sortOder) = (GetId(metadataConfiguration, fileNameFirstSegment), null);
|
||||
else if (fileNameFirstSegmentIsPaddedIntelligentIdFormat)
|
||||
{
|
||||
if (!int.TryParse(fileNameWithoutExtension, out absoluteValueOfId))
|
||||
id = null;
|
||||
if (!int.TryParse(fileNameFirstSegment[..sortOrderOnlyLengthIndex], out int absoluteValueOfSortOrder))
|
||||
(id, sortOder) = (null, null);
|
||||
else
|
||||
id = absoluteValueOfId;
|
||||
(id, sortOder) = (GetId(metadataConfiguration, fileNameFirstSegment[sortOrderOnlyLengthIndex..]), absoluteValueOfSortOrder);
|
||||
}
|
||||
else
|
||||
{
|
||||
negativeMarker = fileNameWithoutExtension[sortOrderOnlyLengthIndex - 2];
|
||||
if (negativeMarker == '7')
|
||||
multiplier = 1;
|
||||
else if (negativeMarker == '3')
|
||||
multiplier = -1;
|
||||
else
|
||||
multiplier = null;
|
||||
if (!int.TryParse(fileNameWithoutExtension[sortOrderOnlyLengthIndex..], out absoluteValueOfId))
|
||||
id = null;
|
||||
else
|
||||
{
|
||||
id = absoluteValueOfId * multiplier;
|
||||
if (id is null || !fileNameWithoutExtension.EndsWith(id.Value.ToString()[1..]))
|
||||
id = null;
|
||||
}
|
||||
}
|
||||
result = new(fileDirectoryName, fileExtensionLowered, file, id, nameWithoutExtensionIsIdFormat, nameWithoutExtensionIsPaddedIdFormat, fileName, fileNameWithoutExtension);
|
||||
throw new NotSupportedException();
|
||||
result = new(fileDirectoryName, fileExtensionLowered, fileNameFirstSegment, file, id, fileNameFirstSegmentIsIntelligentIdFormat, fileName, fileNameWithoutExtension, sortOder);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user