Mostly Sorting
Video Merge as 4, 5, and 6
This commit is contained in:
@ -5,31 +5,119 @@ namespace View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
internal abstract partial class XDirectory
|
||||
{
|
||||
|
||||
private static int GetCeilingAverage(List<string[]> fileCollection)
|
||||
internal static void MoveFiles(List<string> files, string find, string replace)
|
||||
{
|
||||
List<int> counts = [];
|
||||
foreach (string[] files in fileCollection)
|
||||
counts.Add(files.Length);
|
||||
int average = (int)Math.Ceiling(counts.Average());
|
||||
return average;
|
||||
string checkFile;
|
||||
string? checkDirectory;
|
||||
List<string> directories = [];
|
||||
foreach (string file in files)
|
||||
{
|
||||
checkDirectory = Path.GetDirectoryName(file.Replace(find, replace));
|
||||
if (string.IsNullOrEmpty(checkDirectory) || directories.Contains(checkDirectory))
|
||||
continue;
|
||||
directories.Add(checkDirectory);
|
||||
}
|
||||
foreach (string directory in directories)
|
||||
{
|
||||
if (Directory.Exists(directory))
|
||||
continue;
|
||||
_ = Directory.CreateDirectory(directory);
|
||||
}
|
||||
foreach (string file in files)
|
||||
{
|
||||
if (!File.Exists(file))
|
||||
continue;
|
||||
checkFile = file.Replace(find, replace);
|
||||
if (File.Exists(checkFile))
|
||||
{
|
||||
File.Delete(checkFile);
|
||||
continue;
|
||||
}
|
||||
File.Move(file, checkFile);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<string[]> GetFilesCollection(List<string[]> fileCollection, int ceilingAverage)
|
||||
internal static List<string> CopyOrMove(List<(FilePath, string)> toDoCollection, bool move, bool moveBack, Action? tick)
|
||||
{
|
||||
List<string[]> results = [];
|
||||
foreach (string[] files in fileCollection)
|
||||
List<string> results = [];
|
||||
FileInfo fileInfo;
|
||||
List<string> distinctExtensions = [];
|
||||
foreach ((FilePath filePath, string to) in toDoCollection)
|
||||
{
|
||||
if (files.Length < ceilingAverage)
|
||||
results.Add(files);
|
||||
}
|
||||
foreach (string[] files in fileCollection)
|
||||
{
|
||||
if (files.Length >= ceilingAverage)
|
||||
results.Add(files);
|
||||
tick?.Invoke();
|
||||
fileInfo = new(to);
|
||||
if (fileInfo.Exists)
|
||||
{
|
||||
if (filePath.Length == fileInfo.Length && filePath.LastWriteTicks == fileInfo.LastWriteTime.Ticks)
|
||||
continue;
|
||||
fileInfo.Delete();
|
||||
}
|
||||
results.Add(filePath.NameWithoutExtension);
|
||||
try
|
||||
{
|
||||
if (!distinctExtensions.Contains(filePath.ExtensionLowered))
|
||||
distinctExtensions.Add(filePath.ExtensionLowered);
|
||||
if (move || moveBack)
|
||||
File.Move(filePath.FullName, to);
|
||||
else
|
||||
File.Copy(filePath.FullName, to);
|
||||
}
|
||||
catch (Exception) { }
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
internal static int MaybeMove(Properties.IPropertyConfiguration propertyConfiguration, List<FilePair> filePairs, string jsonGroupDirectory, string extension)
|
||||
{
|
||||
FileInfo? toFileInfo;
|
||||
FileInfo fromFileInfo;
|
||||
string checkDirectory;
|
||||
List<(string, string)> rename = [];
|
||||
foreach (FilePair filePair in filePairs)
|
||||
{
|
||||
if (filePair.IsUnique)
|
||||
continue;
|
||||
IsNotUniqueLoop(propertyConfiguration, jsonGroupDirectory, extension, filePair, rename);
|
||||
}
|
||||
foreach ((string from, string to) in rename)
|
||||
{
|
||||
toFileInfo = null;
|
||||
checkDirectory = to;
|
||||
fromFileInfo = new(from);
|
||||
if (!fromFileInfo.Exists)
|
||||
continue;
|
||||
for (int i = 0; i < int.MaxValue; i++)
|
||||
{
|
||||
toFileInfo = new(checkDirectory);
|
||||
if (toFileInfo.Directory is null)
|
||||
continue;
|
||||
if (!toFileInfo.Directory.Exists)
|
||||
_ = Directory.CreateDirectory(toFileInfo.Directory.FullName);
|
||||
if (checkDirectory.Length > 199)
|
||||
throw new Exception();
|
||||
if (!toFileInfo.Exists)
|
||||
break;
|
||||
else if (fromFileInfo.Length == toFileInfo.Length && fromFileInfo.LastWriteTime == toFileInfo.LastWriteTime)
|
||||
checkDirectory = string.Concat(checkDirectory, ".del");
|
||||
else
|
||||
checkDirectory = string.Concat(checkDirectory, ".j");
|
||||
}
|
||||
File.Move(from, checkDirectory);
|
||||
}
|
||||
return rename.Count;
|
||||
}
|
||||
|
||||
private static void IsNotUniqueLoop(Properties.IPropertyConfiguration propertyConfiguration, string jsonGroupDirectory, string extension, FilePair filePair, List<(string, string)> rename)
|
||||
{
|
||||
int length = propertyConfiguration.RootDirectory.Length;
|
||||
foreach (string path in filePair.Collection)
|
||||
{
|
||||
if (filePair.Match is null || path != filePair.Match)
|
||||
continue;
|
||||
rename.Add(new(path, string.Concat(jsonGroupDirectory, filePair.Path[length..], extension)));
|
||||
}
|
||||
}
|
||||
|
||||
internal static ReadOnlyCollection<string[]> GetFilesCollection(string directory, string directorySearchFilter, string fileSearchFilter, bool useCeilingAverage)
|
||||
{
|
||||
List<string[]> results = [];
|
||||
@ -60,6 +148,53 @@ internal abstract partial class XDirectory
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
private static int GetCeilingAverage(List<string[]> fileCollection)
|
||||
{
|
||||
List<int> counts = [];
|
||||
foreach (string[] files in fileCollection)
|
||||
counts.Add(files.Length);
|
||||
int average = (int)Math.Ceiling(counts.Average());
|
||||
return average;
|
||||
}
|
||||
|
||||
private static List<string[]> GetFilesCollection(List<string[]> fileCollection, int ceilingAverage)
|
||||
{
|
||||
List<string[]> results = [];
|
||||
foreach (string[] files in fileCollection)
|
||||
{
|
||||
if (files.Length < ceilingAverage)
|
||||
results.Add(files);
|
||||
}
|
||||
foreach (string[] files in fileCollection)
|
||||
{
|
||||
if (files.Length >= ceilingAverage)
|
||||
results.Add(files);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
internal static ReadOnlyCollection<ReadOnlyCollection<FilePath>> GetFilePathCollections(Properties.IPropertyConfiguration propertyConfiguration, ReadOnlyCollection<string[]> filesCollection)
|
||||
{
|
||||
List<ReadOnlyCollection<FilePath>> results = [];
|
||||
FilePath filePath;
|
||||
List<FilePath> filePaths;
|
||||
Models.FileHolder fileHolder;
|
||||
foreach (string[] files in filesCollection)
|
||||
{
|
||||
filePaths = [];
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileHolder = IFileHolder.Get(file);
|
||||
if (propertyConfiguration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered))
|
||||
continue;
|
||||
filePath = FilePath.Get(propertyConfiguration, fileHolder, index: null);
|
||||
filePaths.Add(filePath);
|
||||
}
|
||||
results.Add(new(filePaths));
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
internal static ReadOnlyCollection<ReadOnlyCollection<FilePath>> GetFilePathCollections(Properties.IPropertyConfiguration propertyConfiguration, string directorySearchFilter, string fileSearchFilter, string directory, bool useCeilingAverage)
|
||||
{
|
||||
ReadOnlyCollection<ReadOnlyCollection<FilePath>> results;
|
||||
@ -68,48 +203,6 @@ internal abstract partial class XDirectory
|
||||
return results;
|
||||
}
|
||||
|
||||
private static bool GetIsNotUniqueAndNeedsReview(string file, List<string> collection)
|
||||
{
|
||||
bool result = false;
|
||||
FileInfo possibleFileInfo;
|
||||
FileInfo fileInfo = new(file);
|
||||
foreach (string possible in collection)
|
||||
{
|
||||
if (possible == file)
|
||||
continue;
|
||||
possibleFileInfo = new(possible);
|
||||
if (possibleFileInfo.LastWriteTime != fileInfo.LastWriteTime)
|
||||
File.SetLastWriteTime(file, new DateTime[] { possibleFileInfo.LastWriteTime, fileInfo.LastWriteTime }.Max());
|
||||
if (possibleFileInfo.LastWriteTime == fileInfo.LastWriteTime && possibleFileInfo.Length == fileInfo.Length)
|
||||
continue;
|
||||
if (!result)
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static string? GetMatch(string file, List<string> collection)
|
||||
{
|
||||
string? result = null;
|
||||
FileInfo possibleFileInfo;
|
||||
List<long> lengths = [];
|
||||
List<string> matches = [];
|
||||
FileInfo fileInfo = new(file);
|
||||
List<DateTime> creationTimes = [];
|
||||
foreach (string possible in collection)
|
||||
{
|
||||
possibleFileInfo = new(possible);
|
||||
lengths.Add(possibleFileInfo.Length);
|
||||
creationTimes.Add(possibleFileInfo.CreationTime);
|
||||
if (possibleFileInfo.CreationTime != fileInfo.LastWriteTime)
|
||||
continue;
|
||||
matches.Add(possible);
|
||||
}
|
||||
if (matches.Count == 1 || (matches.Count > 0 && lengths.Distinct().Count() == 1 && creationTimes.Distinct().Count() == 1))
|
||||
result = matches.First();
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static List<FilePair> GetFiles(Properties.IPropertyConfiguration propertyConfiguration, ReadOnlyCollection<ReadOnlyCollection<FilePath>> filePathsCollection, IReadOnlyDictionary<string, List<string>> fileNamesToFiles, IReadOnlyDictionary<string, List<string>> compareFileNamesToFiles)
|
||||
{
|
||||
List<FilePair> results = [];
|
||||
@ -168,126 +261,51 @@ internal abstract partial class XDirectory
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void IsNotUniqueLoop(Properties.IPropertyConfiguration propertyConfiguration, string jsonGroupDirectory, string extension, FilePair filePair, List<(string, string)> rename)
|
||||
private static bool GetIsNotUniqueAndNeedsReview(string file, List<string> collection)
|
||||
{
|
||||
int length = propertyConfiguration.RootDirectory.Length;
|
||||
foreach (string path in filePair.Collection)
|
||||
bool result = false;
|
||||
FileInfo possibleFileInfo;
|
||||
FileInfo fileInfo = new(file);
|
||||
foreach (string possible in collection)
|
||||
{
|
||||
if (filePair.Match is null || path != filePair.Match)
|
||||
if (possible == file)
|
||||
continue;
|
||||
rename.Add(new(path, string.Concat(jsonGroupDirectory, filePair.Path[length..], extension)));
|
||||
possibleFileInfo = new(possible);
|
||||
if (possibleFileInfo.LastWriteTime != fileInfo.LastWriteTime)
|
||||
File.SetLastWriteTime(file, new DateTime[] { possibleFileInfo.LastWriteTime, fileInfo.LastWriteTime }.Max());
|
||||
if (possibleFileInfo.LastWriteTime == fileInfo.LastWriteTime && possibleFileInfo.Length == fileInfo.Length)
|
||||
continue;
|
||||
if (!result)
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static int MaybeMove(Properties.IPropertyConfiguration propertyConfiguration, List<FilePair> filePairs, string jsonGroupDirectory, string extension)
|
||||
private static string? GetMatch(string file, List<string> collection)
|
||||
{
|
||||
FileInfo? toFileInfo;
|
||||
FileInfo fromFileInfo;
|
||||
string checkDirectory;
|
||||
List<(string, string)> rename = [];
|
||||
foreach (FilePair filePair in filePairs)
|
||||
string? result = null;
|
||||
FileInfo possibleFileInfo;
|
||||
List<long> lengths = [];
|
||||
List<string> matches = [];
|
||||
FileInfo fileInfo = new(file);
|
||||
List<DateTime> creationTimes = [];
|
||||
foreach (string possible in collection)
|
||||
{
|
||||
if (filePair.IsUnique)
|
||||
possibleFileInfo = new(possible);
|
||||
lengths.Add(possibleFileInfo.Length);
|
||||
creationTimes.Add(possibleFileInfo.CreationTime);
|
||||
if (possibleFileInfo.CreationTime != fileInfo.LastWriteTime)
|
||||
continue;
|
||||
IsNotUniqueLoop(propertyConfiguration, jsonGroupDirectory, extension, filePair, rename);
|
||||
matches.Add(possible);
|
||||
}
|
||||
foreach ((string from, string to) in rename)
|
||||
{
|
||||
toFileInfo = null;
|
||||
checkDirectory = to;
|
||||
fromFileInfo = new(from);
|
||||
if (!fromFileInfo.Exists)
|
||||
continue;
|
||||
for (int i = 0; i < int.MaxValue; i++)
|
||||
{
|
||||
toFileInfo = new(checkDirectory);
|
||||
if (toFileInfo.Directory is null)
|
||||
continue;
|
||||
if (!toFileInfo.Directory.Exists)
|
||||
_ = Directory.CreateDirectory(toFileInfo.Directory.FullName);
|
||||
if (checkDirectory.Length > 199)
|
||||
throw new Exception();
|
||||
if (!toFileInfo.Exists)
|
||||
break;
|
||||
else if (fromFileInfo.Length == toFileInfo.Length && fromFileInfo.LastWriteTime == toFileInfo.LastWriteTime)
|
||||
checkDirectory = string.Concat(checkDirectory, ".del");
|
||||
else
|
||||
checkDirectory = string.Concat(checkDirectory, ".j");
|
||||
}
|
||||
File.Move(from, checkDirectory);
|
||||
}
|
||||
return rename.Count;
|
||||
}
|
||||
|
||||
internal static void MoveFiles(List<string> files, string find, string replace)
|
||||
{
|
||||
string checkFile;
|
||||
string? checkDirectory;
|
||||
List<string> directories = [];
|
||||
foreach (string file in files)
|
||||
{
|
||||
checkDirectory = Path.GetDirectoryName(file.Replace(find, replace));
|
||||
if (string.IsNullOrEmpty(checkDirectory) || directories.Contains(checkDirectory))
|
||||
continue;
|
||||
directories.Add(checkDirectory);
|
||||
}
|
||||
foreach (string directory in directories)
|
||||
{
|
||||
if (Directory.Exists(directory))
|
||||
continue;
|
||||
_ = Directory.CreateDirectory(directory);
|
||||
}
|
||||
foreach (string file in files)
|
||||
{
|
||||
if (!File.Exists(file))
|
||||
continue;
|
||||
checkFile = file.Replace(find, replace);
|
||||
if (File.Exists(checkFile))
|
||||
{
|
||||
File.Delete(checkFile);
|
||||
continue;
|
||||
}
|
||||
File.Move(file, checkFile);
|
||||
}
|
||||
}
|
||||
|
||||
private static FilePath[] GetSortedRecords(ReadOnlyCollection<ReadOnlyCollection<FilePath>> filePathsCollection)
|
||||
{
|
||||
List<FilePath> results = [];
|
||||
foreach (ReadOnlyCollection<FilePath> filePaths in filePathsCollection)
|
||||
{
|
||||
foreach (FilePath filePath in filePaths)
|
||||
results.Add(filePath);
|
||||
}
|
||||
return (from l in results orderby l.CreationTicks, l.FullName.Length descending select l).ToArray();
|
||||
}
|
||||
|
||||
internal static ReadOnlyCollection<ReadOnlyCollection<FilePath>> GetFilePathCollections(Properties.IPropertyConfiguration propertyConfiguration, ReadOnlyCollection<string[]> filesCollection)
|
||||
{
|
||||
List<ReadOnlyCollection<FilePath>> results = [];
|
||||
FilePath filePath;
|
||||
List<FilePath> filePaths;
|
||||
Models.FileHolder fileHolder;
|
||||
foreach (string[] files in filesCollection)
|
||||
{
|
||||
filePaths = [];
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileHolder = IFileHolder.Get(file);
|
||||
if (propertyConfiguration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered))
|
||||
continue;
|
||||
filePath = FilePath.Get(propertyConfiguration, fileHolder, index: null);
|
||||
filePaths.Add(filePath);
|
||||
}
|
||||
results.Add(new(filePaths));
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
if (matches.Count == 1 || (matches.Count > 0 && lengths.Distinct().Count() == 1 && creationTimes.Distinct().Count() == 1))
|
||||
result = matches.First();
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static (string[], List<(FilePath, string)>) GetToDoCollection(Properties.IPropertyConfiguration propertyConfiguration, bool copyDuplicates, bool ifCanUseId, ReadOnlyCollection<ReadOnlyCollection<FilePath>> filePathsCollection, IReadOnlyDictionary<string, ReadOnlyDictionary<byte, ReadOnlyCollection<string>>> fileGroups, Action? tick)
|
||||
{
|
||||
List<(FilePath, string)> results = [];
|
||||
string key;
|
||||
string paddedId;
|
||||
string checkFile;
|
||||
string directory;
|
||||
@ -305,6 +323,8 @@ internal abstract partial class XDirectory
|
||||
List<string> distinctDirectories = [];
|
||||
FilePath[] sortedRecords = GetSortedRecords(filePathsCollection);
|
||||
ReadOnlyDictionary<byte, ReadOnlyCollection<string>>? keyValuePairs;
|
||||
if (!fileGroups.TryGetValue(propertyConfiguration.ResultContent, out keyValuePairs))
|
||||
throw new NotImplementedException();
|
||||
bool isOffsetDeterministicHashCode = IId.IsOffsetDeterministicHashCode(propertyConfiguration);
|
||||
for (int i = 0; i < sortedRecords.Length; i++)
|
||||
{
|
||||
@ -312,11 +332,8 @@ internal abstract partial class XDirectory
|
||||
filePath = sortedRecords[i];
|
||||
if (filePath.Name.EndsWith("len") || filePath.ExtensionLowered == ".id" || filePath.ExtensionLowered == ".lsv" || filePath.DirectoryFullPath is null)
|
||||
continue;
|
||||
key = propertyConfiguration.ValidVideoFormatExtensions.Contains(filePath.ExtensionLowered) ? propertyConfiguration.ResultContentCollection : propertyConfiguration.ResultContent;
|
||||
if (!fileGroups.TryGetValue(key, out keyValuePairs))
|
||||
continue;
|
||||
cei = IPath.GetCombinedEnumAndIndex(propertyConfiguration, filePath);
|
||||
fileDirectoryName = Path.GetFileName(filePath.DirectoryFullPath);
|
||||
cei = IPath.GetCombinedEnumAndIndex(propertyConfiguration, filePath);
|
||||
if (fileDirectoryName.Length < propertyConfiguration.ResultAllInOneSubdirectoryLength + 3 || !filePath.Name.StartsWith(fileDirectoryName))
|
||||
{
|
||||
if (wrapped)
|
||||
@ -409,34 +426,15 @@ internal abstract partial class XDirectory
|
||||
return (distinctDirectories.ToArray(), results);
|
||||
}
|
||||
|
||||
internal static List<string> CopyOrMove(List<(FilePath, string)> toDoCollection, bool move, bool moveBack, Action? tick)
|
||||
private static FilePath[] GetSortedRecords(ReadOnlyCollection<ReadOnlyCollection<FilePath>> filePathsCollection)
|
||||
{
|
||||
List<string> results = [];
|
||||
FileInfo fileInfo;
|
||||
List<string> distinctExtensions = [];
|
||||
foreach ((FilePath filePath, string to) in toDoCollection)
|
||||
List<FilePath> results = [];
|
||||
foreach (ReadOnlyCollection<FilePath> filePaths in filePathsCollection)
|
||||
{
|
||||
tick?.Invoke();
|
||||
fileInfo = new(to);
|
||||
if (fileInfo.Exists)
|
||||
{
|
||||
if (filePath.Length == fileInfo.Length && filePath.LastWriteTicks == fileInfo.LastWriteTime.Ticks)
|
||||
continue;
|
||||
fileInfo.Delete();
|
||||
}
|
||||
results.Add(filePath.NameWithoutExtension);
|
||||
try
|
||||
{
|
||||
if (!distinctExtensions.Contains(filePath.ExtensionLowered))
|
||||
distinctExtensions.Add(filePath.ExtensionLowered);
|
||||
if (move || moveBack)
|
||||
File.Move(filePath.FullName, to);
|
||||
else
|
||||
File.Copy(filePath.FullName, to);
|
||||
}
|
||||
catch (Exception) { }
|
||||
foreach (FilePath filePath in filePaths)
|
||||
results.Add(filePath);
|
||||
}
|
||||
return results;
|
||||
return (from l in results orderby l.CreationTicks, l.FullName.Length descending select l).ToArray();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user