Ready to test
This commit is contained in:
@ -5,6 +5,7 @@ using System.Text;
|
||||
using View_by_Distance.Date.Group.Models;
|
||||
using View_by_Distance.Property.Models;
|
||||
using View_by_Distance.Shared.Models.Methods;
|
||||
using WindowsShortcutFactory;
|
||||
|
||||
namespace View_by_Distance.Date.Group;
|
||||
|
||||
@ -39,50 +40,60 @@ public class DateGroup
|
||||
Property.Models.Configuration.Verify(propertyConfiguration);
|
||||
Models.Configuration configuration = Models.Stateless.Configuration.Get(isEnvironment, configurationRoot, workingDirectory, propertyConfiguration);
|
||||
Verify(configuration);
|
||||
bool reverse = false;
|
||||
string modelName = nameof(modelName);
|
||||
string predictorModelName = nameof(predictorModelName);
|
||||
_Configuration = configuration;
|
||||
if (configuration.ByHash is null)
|
||||
throw new Exception($"{nameof(configuration.ByHash)} is null!");
|
||||
if (propertyConfiguration.MaxImagesInDirectoryForTopLevelFirstPass is null)
|
||||
throw new Exception($"{nameof(propertyConfiguration.MaxImagesInDirectoryForTopLevelFirstPass)} is null!");
|
||||
if (configuration.ByCreateDateShortcut is null)
|
||||
throw new Exception($"{nameof(configuration.ByCreateDateShortcut)} is null!");
|
||||
if (propertyConfiguration.PopulatePropertyId is null)
|
||||
throw new Exception($"{nameof(propertyConfiguration.PopulatePropertyId)} is null!");
|
||||
if (!_IsEnvironment.Development)
|
||||
throw new Exception("This program only allows development environments!");
|
||||
string searchPattern = "*";
|
||||
long ticks = DateTime.Now.Ticks;
|
||||
List<string> topDirectories = new();
|
||||
PropertyLogic propertyLogic = GetPropertyLogic();
|
||||
string[] dbFiles = Directory.GetFiles(propertyConfiguration.RootDirectory, "*.db", SearchOption.AllDirectories);
|
||||
foreach (string dbFile in dbFiles)
|
||||
File.Delete(dbFile);
|
||||
if (true || appSettings.MaxDegreeOfParallelism.Value < 2)
|
||||
ticks = LogDelta(ticks, nameof(File.Delete));
|
||||
for (int i = 1; i < 10; i++)
|
||||
_ = Property.Models.Stateless.IPath.DeleteEmptyDirectories(propertyConfiguration.RootDirectory);
|
||||
List<(int g, string sourceDirectory, string[] sourceDirectoryFiles, int r)> groupCollection = Property.Models.Stateless.A_Property.GetGroupCollection(propertyConfiguration.RootDirectory, searchPattern, topDirectories, propertyConfiguration.MaxImagesInDirectoryForTopLevelFirstPass.Value, reverse: false);
|
||||
if (appSettings.MaxDegreeOfParallelism.Value < 2)
|
||||
ticks = LogDelta(ticks, nameof(Property.Models.Stateless.A_Property.GetGroupCollection));
|
||||
List<Property.Models.Group> groupResultsCollection = new();
|
||||
if (!propertyConfiguration.PopulatePropertyId.Value || !configuration.ByHash.Value)
|
||||
groupResultsCollection = propertyLogic.GetParallelWork(propertyConfiguration, topDirectories, groupCollection, firstPass: true, filterOnFirstPass: true);
|
||||
if (true || appSettings.MaxDegreeOfParallelism.Value < 2)
|
||||
ticks = LogDelta(ticks, nameof(Property.Models.Stateless.IPath.DeleteEmptyDirectories));
|
||||
List<PropertyHolder[]> propertyHolderCollections = Property.Models.Stateless.A_Property.Get(propertyConfiguration, reverse, modelName, predictorModelName, propertyLogic);
|
||||
if (configuration.ByCreateDateShortcut.HasValue && configuration.ByCreateDateShortcut.Value)
|
||||
CreateDateShortcut(propertyConfiguration, propertyHolderCollections);
|
||||
else
|
||||
{
|
||||
_Exceptions.AddRange(propertyLogic.DoWork(propertyConfiguration, topDirectories, groupCollection, firstPass: true));
|
||||
string message = $"There were {_Exceptions.Count} exception(s) thrown! {Environment.NewLine}{string.Join(Environment.NewLine, _Exceptions)}";
|
||||
_Log.Information(message);
|
||||
if (_Exceptions.Count != 0)
|
||||
throw new Exception(message);
|
||||
List<string> topDirectories = new();
|
||||
List<Property.Models.DirectoryInfo> directoryInfoCollection = new();
|
||||
propertyLogic.ParallelWork(propertyConfiguration, modelName, predictorModelName, ticks, propertyHolderCollections, firstPass: true);
|
||||
if (appSettings.MaxDegreeOfParallelism.Value < 2)
|
||||
ticks = LogDelta(ticks, nameof(PropertyLogic.DoWork));
|
||||
topDirectories.Clear();
|
||||
groupCollection = Property.Models.Stateless.A_Property.GetGroupCollection(propertyConfiguration.RootDirectory, searchPattern, topDirectories, propertyConfiguration.MaxImagesInDirectoryForTopLevelFirstPass.Value, reverse: false);
|
||||
if (appSettings.MaxDegreeOfParallelism.Value < 2)
|
||||
ticks = LogDelta(ticks, nameof(Property.Models.Stateless.A_Property.GetGroupCollection));
|
||||
groupResultsCollection = propertyLogic.GetParallelWork(propertyConfiguration, topDirectories, groupCollection, firstPass: false, filterOnFirstPass: true);
|
||||
ticks = LogDelta(ticks, nameof(PropertyLogic.ParallelWork));
|
||||
if (propertyLogic.ExceptionsDirectories.Any())
|
||||
throw new Exception();
|
||||
if (propertyConfiguration.PopulatePropertyId.Value && (configuration.ByCreateDateShortcut.Value || configuration.ByHash.Value))
|
||||
{
|
||||
if (Property.Models.Stateless.A_Property.Any(propertyHolderCollections))
|
||||
propertyLogic.ParallelWork(propertyConfiguration, modelName, predictorModelName, ticks, propertyHolderCollections, firstPass: false);
|
||||
if (appSettings.MaxDegreeOfParallelism.Value < 2)
|
||||
ticks = LogDelta(ticks, nameof(PropertyLogic.ParallelWork));
|
||||
if (propertyLogic.ExceptionsDirectories.Any())
|
||||
throw new Exception();
|
||||
}
|
||||
if ((from l in directoryInfoCollection where l.Moved.Any(a => a) select true).Any())
|
||||
throw new Exception();
|
||||
MoveFiles(topDirectories, directoryInfoCollection);
|
||||
}
|
||||
MoveFiles(topDirectories, groupResultsCollection);
|
||||
}
|
||||
|
||||
private static void Verify(Models.Configuration configuration)
|
||||
{
|
||||
if (configuration.ByCreateDateShortcut is null)
|
||||
throw new Exception($"{nameof(configuration.ByCreateDateShortcut)} is null!");
|
||||
if (configuration.ByDay is null)
|
||||
throw new Exception($"{nameof(configuration.ByDay)} is null!");
|
||||
if (configuration.ByHash is null)
|
||||
@ -91,17 +102,17 @@ public class DateGroup
|
||||
throw new Exception($"{nameof(configuration.BySeason)} is null!");
|
||||
if (configuration.ByWeek is null)
|
||||
throw new Exception($"{nameof(configuration.ByWeek)} is null!");
|
||||
if (!configuration.ByDay.Value && !configuration.ByWeek.Value && !configuration.BySeason.Value && !configuration.ByHash.Value)
|
||||
if (!configuration.ByCreateDateShortcut.Value && !configuration.ByDay.Value && !configuration.ByWeek.Value && !configuration.BySeason.Value && !configuration.ByHash.Value)
|
||||
throw new Exception("Change configuration!");
|
||||
if (configuration.KeepFullPath is null)
|
||||
throw new Exception($"{nameof(configuration.KeepFullPath)} is null!");
|
||||
if (configuration?.PropertyConfiguration?.PopulatePropertyId is null)
|
||||
throw new Exception($"{nameof(configuration.PropertyConfiguration.PopulatePropertyId)} must be set!");
|
||||
if (configuration.PropertyConfiguration.PopulatePropertyId.Value && !configuration.ByHash.Value)
|
||||
if (configuration.PropertyConfiguration.PopulatePropertyId.Value && !configuration.ByCreateDateShortcut.Value && !configuration.ByHash.Value)
|
||||
throw new Exception("Change configuration!");
|
||||
if (!configuration.PropertyConfiguration.PopulatePropertyId.Value && configuration.ByHash.Value)
|
||||
throw new Exception("Change configuration!");
|
||||
if (configuration.ByDay.Value && configuration.ByWeek.Value && configuration.BySeason.Value && configuration.ByHash.Value)
|
||||
if (configuration.ByCreateDateShortcut.Value && configuration.ByDay.Value && configuration.ByWeek.Value && configuration.BySeason.Value && configuration.ByHash.Value)
|
||||
throw new Exception("Change configuration!");
|
||||
}
|
||||
|
||||
@ -148,9 +159,11 @@ public class DateGroup
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<(string Source, string[] Destination)> GetMoveFileCollection(string destinationDirectory, string topDirectory, Property.Models.Group group)
|
||||
private List<(string Source, string[] Destination)> GetMoveFileCollection(string destinationDirectory, string topDirectory, Property.Models.DirectoryInfo group)
|
||||
{
|
||||
List<(string Source, string[] Destination)> results = new();
|
||||
if (_Configuration.ByCreateDateShortcut is null)
|
||||
throw new Exception($"{nameof(_Configuration.ByCreateDateShortcut)} is null!");
|
||||
if (_Configuration.ByDay is null)
|
||||
throw new Exception($"{nameof(_Configuration.ByDay)} is null!");
|
||||
if (_Configuration.ByHash is null)
|
||||
@ -181,8 +194,8 @@ public class DateGroup
|
||||
string[] directorySegments;
|
||||
DateTime? minimumDateTime = null;
|
||||
List<string> destinationCollection;
|
||||
string filteredSourceDirectoryFile;
|
||||
List<string> directoryNames = new();
|
||||
FileInfo filteredSourceDirectoryFileInfo;
|
||||
List<string> topDirectorySegments = new();
|
||||
StringBuilder destinationDirectoryName = new();
|
||||
Calendar calendar = new CultureInfo("en-US").Calendar;
|
||||
@ -219,7 +232,7 @@ public class DateGroup
|
||||
}
|
||||
if (matches is null)
|
||||
matches = Array.Empty<string>();
|
||||
for (int i = 0; i < group.FilteredSourceDirectoryFiles.Length; i++)
|
||||
for (int i = 0; i < group.SourceDirectoryFileInfoCollection.Length; i++)
|
||||
{
|
||||
destinationCollection = new();
|
||||
directoryNames.Clear();
|
||||
@ -227,14 +240,14 @@ public class DateGroup
|
||||
property = group.PropertyCollection[i];
|
||||
if (property is null)
|
||||
continue;
|
||||
filteredSourceDirectoryFile = group.FilteredSourceDirectoryFiles[i];
|
||||
filteredSourceDirectoryFileInfo = group.SourceDirectoryFileInfoCollection[i];
|
||||
minimumDateTime = Property.Models.Stateless.A_Property.GetMinimumDateTime(property);
|
||||
directory = Path.GetDirectoryName(filteredSourceDirectoryFile);
|
||||
directory = filteredSourceDirectoryFileInfo.DirectoryName;
|
||||
if (string.IsNullOrEmpty(directory))
|
||||
continue;
|
||||
day = minimumDateTime.Value.ToString("MM-dd");
|
||||
month = minimumDateTime.Value.ToString("MMMM");
|
||||
(propertyWrongYear, _) = property.IsWrongYear(filteredSourceDirectoryFile, minimumDateTime);
|
||||
(propertyWrongYear, _) = property.IsWrongYear(filteredSourceDirectoryFileInfo.FullName, minimumDateTime);
|
||||
if (propertyWrongYear is null)
|
||||
flag = '#';
|
||||
else
|
||||
@ -303,13 +316,13 @@ public class DateGroup
|
||||
}
|
||||
}
|
||||
if (!_Configuration.ByHash.Value || property.Id is null)
|
||||
fileName = Path.GetFileName(filteredSourceDirectoryFile);
|
||||
fileName = filteredSourceDirectoryFileInfo.Name;
|
||||
else
|
||||
fileName = $"{property.Id.Value}{Path.GetExtension(filteredSourceDirectoryFile).ToLower()}";
|
||||
fileName = $"{property.Id.Value}{filteredSourceDirectoryFileInfo.Extension.ToLower()}";
|
||||
destinationCollection.Add(destinationDirectory);
|
||||
destinationCollection.AddRange(directoryNames);
|
||||
destinationCollection.Add(fileName);
|
||||
results.Add(new(filteredSourceDirectoryFile, destinationCollection.ToArray()));
|
||||
results.Add(new(filteredSourceDirectoryFileInfo.FullName, destinationCollection.ToArray()));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
@ -317,18 +330,15 @@ public class DateGroup
|
||||
private PropertyLogic GetPropertyLogic()
|
||||
{
|
||||
PropertyLogic result;
|
||||
|
||||
string[] verifyToSeason = Array.Empty<string>();
|
||||
|
||||
if (_AppSettings.MaxDegreeOfParallelism is null)
|
||||
throw new Exception($"{nameof(_AppSettings.MaxDegreeOfParallelism)} is null!");
|
||||
if (_Configuration?.PropertyConfiguration is null)
|
||||
throw new Exception($"{nameof(_Configuration.PropertyConfiguration)} must be set!");
|
||||
result = new(_AppSettings.MaxDegreeOfParallelism.Value, _Configuration.PropertyConfiguration, verifyToSeason);
|
||||
result = new(_AppSettings.MaxDegreeOfParallelism.Value, _Configuration.PropertyConfiguration);
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<(string Source, string[] Destination)> GetFileMoveCollectionAll(List<string> topDirectories, List<Property.Models.Group> groupCollection)
|
||||
private List<(string Source, string[] Destination)> GetFileMoveCollectionAll(List<string> topDirectories, List<Property.Models.DirectoryInfo> groupCollection)
|
||||
{
|
||||
List<(string Source, string[] Destination)> results = new();
|
||||
if (_Configuration.KeepFullPath is null)
|
||||
@ -341,7 +351,7 @@ public class DateGroup
|
||||
string destinationDirectory;
|
||||
string destinationRoot = Property.Models.Stateless.IResult.GetResultsGroupDirectory(_Configuration.PropertyConfiguration, "Z) Moved");
|
||||
List<(string Source, string[] Destination)> fileMoveCollectionDirectory;
|
||||
foreach (Property.Models.Group group in groupCollection)
|
||||
foreach (Property.Models.DirectoryInfo group in groupCollection)
|
||||
{
|
||||
sourceDirectory = group.SourceDirectory;
|
||||
if (!_Configuration.KeepFullPath.Value)
|
||||
@ -366,7 +376,7 @@ public class DateGroup
|
||||
return results;
|
||||
}
|
||||
|
||||
private void MoveFiles(List<string> topDirectories, List<Property.Models.Group> groupCollection)
|
||||
private void MoveFiles(List<string> topDirectories, List<Property.Models.DirectoryInfo> groupCollection)
|
||||
{
|
||||
if (_Log is null)
|
||||
throw new Exception($"{nameof(_Log)} is null!");
|
||||
@ -423,4 +433,57 @@ public class DateGroup
|
||||
_ = Property.Models.Stateless.IPath.DeleteEmptyDirectories(_Configuration.PropertyConfiguration.RootDirectory);
|
||||
}
|
||||
|
||||
private static void CreateDateShortcut(Property.Models.Configuration configuration, List<PropertyHolder[]> propertyHolderCollections)
|
||||
{
|
||||
string path;
|
||||
string fileName;
|
||||
string directory;
|
||||
int selectedTotal;
|
||||
const int minimum = 3;
|
||||
List<DateTime> dateTimes;
|
||||
const int maximumHours = 24;
|
||||
string? relativePathDirectory;
|
||||
WindowsShortcut windowsShortcut;
|
||||
TimeSpan threeStandardDeviationHigh;
|
||||
List<PropertyHolder> selectedPropertyHolderCollection;
|
||||
string aPropertyContentDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(A_Property), "()");
|
||||
foreach (PropertyHolder[] propertyHolderCollection in propertyHolderCollections)
|
||||
{
|
||||
if (!propertyHolderCollection.Any())
|
||||
continue;
|
||||
selectedTotal = 0;
|
||||
threeStandardDeviationHigh = Property.Models.Stateless.A_Property.GetThreeStandardDeviationHigh(minimum, propertyHolderCollection);
|
||||
if (threeStandardDeviationHigh.TotalHours > maximumHours)
|
||||
threeStandardDeviationHigh = new(maximumHours, 0, 0);
|
||||
for (int i = 0; i < propertyHolderCollection.Length; i++)
|
||||
{
|
||||
(i, dateTimes, selectedPropertyHolderCollection) = Property.Models.Stateless.A_Property.Get(propertyHolderCollection, threeStandardDeviationHigh, i);
|
||||
selectedTotal += selectedPropertyHolderCollection.Count;
|
||||
foreach (PropertyHolder propertyHolder in selectedPropertyHolderCollection)
|
||||
{
|
||||
if (propertyHolder.Property is null || propertyHolder.MinimumDateTime is null)
|
||||
continue;
|
||||
relativePathDirectory = Path.GetDirectoryName(propertyHolder.RelativePath);
|
||||
if (string.IsNullOrEmpty(relativePathDirectory))
|
||||
continue;
|
||||
path = Path.GetFullPath($"{configuration.RootDirectory}{propertyHolder.RelativePath[..^5]}");
|
||||
directory = Path.Combine($"{aPropertyContentDirectory}{relativePathDirectory}", $"{dateTimes.Min():yyyy-MM-dd_HH-mm-ss}---{dateTimes.Max():yyyy-MM-dd_HH-mm-ss}");
|
||||
if (!Directory.Exists(directory))
|
||||
_ = Directory.CreateDirectory(directory);
|
||||
fileName = Path.Combine(directory, $"{Path.GetFileName(propertyHolder.RelativePath[..^5])}.lnk");
|
||||
if (File.Exists(fileName))
|
||||
continue;
|
||||
windowsShortcut = new() { Path = path };
|
||||
windowsShortcut.Save(fileName);
|
||||
windowsShortcut.Dispose();
|
||||
if (!File.Exists(fileName))
|
||||
continue;
|
||||
File.SetLastWriteTime(fileName, propertyHolder.MinimumDateTime.Value);
|
||||
}
|
||||
}
|
||||
if (selectedTotal < propertyHolderCollection.Length && selectedTotal < (from l in propertyHolderCollection where l.Property is not null && l.MinimumDateTime.HasValue select true).Count())
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user