Set-Created-Date
This commit is contained in:
165
Set-Created-Date/SetCreatedDate.cs
Normal file
165
Set-Created-Date/SetCreatedDate.cs
Normal file
@ -0,0 +1,165 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Phares.Shared;
|
||||
using Serilog;
|
||||
using ShellProgressBar;
|
||||
using View_by_Distance.Set.Created.Date.Models;
|
||||
using View_by_Distance.Shared.Models;
|
||||
using View_by_Distance.Shared.Models.Methods;
|
||||
|
||||
namespace View_by_Distance.Set.Created.Date;
|
||||
|
||||
public class SetCreatedDate
|
||||
{
|
||||
|
||||
private readonly AppSettings _AppSettings;
|
||||
private readonly string _WorkingDirectory;
|
||||
private readonly Configuration _Configuration;
|
||||
private readonly IsEnvironment _IsEnvironment;
|
||||
private readonly IConfigurationRoot _ConfigurationRoot;
|
||||
private readonly IReadOnlyDictionary<string, string[]> _FileGroups;
|
||||
private readonly Property.Models.Configuration _PropertyConfiguration;
|
||||
|
||||
public SetCreatedDate(List<string> args, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console)
|
||||
{
|
||||
if (isSilent)
|
||||
{ }
|
||||
if (console is null)
|
||||
{ }
|
||||
_AppSettings = appSettings;
|
||||
_IsEnvironment = isEnvironment;
|
||||
_WorkingDirectory = workingDirectory;
|
||||
_ConfigurationRoot = configurationRoot;
|
||||
ILogger? log = Log.ForContext<SetCreatedDate>();
|
||||
Property.Models.Configuration propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
|
||||
string[] directories = new string[] { propertyConfiguration.ResultContent };
|
||||
_FileGroups = Shared.Models.Stateless.Methods.IPath.GetKeyValuePairs(propertyConfiguration, appSettings.CopyTo, directories);
|
||||
Configuration configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration);
|
||||
_PropertyConfiguration = propertyConfiguration;
|
||||
_Configuration = configuration;
|
||||
propertyConfiguration.Update();
|
||||
log.Information(propertyConfiguration.RootDirectory);
|
||||
Verify();
|
||||
List<string> lines = SetCreatedDateFilesInDirectories(log);
|
||||
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines);
|
||||
if (!lines.Any())
|
||||
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(propertyConfiguration.RootDirectory);
|
||||
}
|
||||
|
||||
private void Verify()
|
||||
{
|
||||
if (_AppSettings is null)
|
||||
{ }
|
||||
if (_IsEnvironment is null)
|
||||
{ }
|
||||
if (_Configuration is null)
|
||||
{ }
|
||||
if (_ConfigurationRoot is null)
|
||||
{ }
|
||||
if (_WorkingDirectory is null)
|
||||
{ }
|
||||
if (_PropertyConfiguration is null)
|
||||
{ }
|
||||
}
|
||||
|
||||
private static (List<string>, List<string>) Get(List<string[]> filesCollection)
|
||||
{
|
||||
List<string> results = new();
|
||||
string? directory;
|
||||
List<string> directories = new();
|
||||
foreach (string[] files in filesCollection)
|
||||
{
|
||||
if (!files.Any())
|
||||
continue;
|
||||
directory = Path.GetDirectoryName(files.First());
|
||||
if (directory is null)
|
||||
continue;
|
||||
if (!directories.Contains(directory))
|
||||
directories.Add(directory);
|
||||
results.AddRange(files);
|
||||
}
|
||||
return (directories, results);
|
||||
}
|
||||
|
||||
private List<(FileHolder, DateTime)> GetToDoCollection(ProgressBar progressBar, FileHolder[] fileHolders)
|
||||
{
|
||||
List<(FileHolder, DateTime)> results = new();
|
||||
int? id;
|
||||
string? message;
|
||||
DateTime? dateTime;
|
||||
DateTime?[] dateTimes;
|
||||
bool isIgnoreExtension;
|
||||
DateTime? dateTimeOriginal;
|
||||
bool isValidImageFormatExtension;
|
||||
foreach (FileHolder fileHolder in fileHolders)
|
||||
{
|
||||
progressBar.Tick();
|
||||
if (fileHolder.ExtensionLowered == ".id" || fileHolder.ExtensionLowered == ".lsv" || fileHolder.DirectoryName is null)
|
||||
continue;
|
||||
if (_PropertyConfiguration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered))
|
||||
continue;
|
||||
isValidImageFormatExtension = _PropertyConfiguration.ValidImageFormatExtensions.Contains(fileHolder.ExtensionLowered);
|
||||
isIgnoreExtension = isValidImageFormatExtension && _PropertyConfiguration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered);
|
||||
if (isIgnoreExtension || !isValidImageFormatExtension)
|
||||
continue;
|
||||
(dateTimeOriginal, dateTimes, id, message) = Shared.Models.Stateless.Methods.IProperty.Get(fileHolder, isIgnoreExtension, isValidImageFormatExtension, _PropertyConfiguration.PopulatePropertyId);
|
||||
dateTime = dateTimeOriginal is not null ? dateTimeOriginal : dateTime = dateTimes.Min();
|
||||
if (dateTime is null)
|
||||
continue;
|
||||
if (dateTime.Value == fileHolder.CreationTime)
|
||||
continue;
|
||||
results.Add((fileHolder, dateTime.Value));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static List<string> SetCreatedDateForeach(ProgressBar progressBar, List<(FileHolder, DateTime)> toDoCollection)
|
||||
{
|
||||
List<string> results = new();
|
||||
foreach ((FileHolder fileHolder, DateTime dateTime) in toDoCollection)
|
||||
{
|
||||
progressBar.Tick();
|
||||
results.Add(fileHolder.NameWithoutExtension);
|
||||
try
|
||||
{ File.SetCreationTime(fileHolder.FullName, dateTime); }
|
||||
catch (Exception) { }
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private List<string> SetCreatedDateFilesInDirectories(ILogger log)
|
||||
{
|
||||
List<string> results = new();
|
||||
ProgressBar progressBar;
|
||||
ConsoleKey? consoleKey = null;
|
||||
const string fileSearchFilter = "*";
|
||||
string message = nameof(SetCreatedDate);
|
||||
const string directorySearchFilter = "*";
|
||||
List<string[]> filesCollection = Shared.Models.Stateless.Methods.IDirectory.GetFilesCollection(_PropertyConfiguration.RootDirectory, directorySearchFilter, fileSearchFilter);
|
||||
(_, List<string> allFiles) = Get(filesCollection);
|
||||
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
||||
progressBar = new(allFiles.Count, message, options);
|
||||
FileHolder[] fileHolders = (from l in allFiles select new FileHolder(l)).ToArray();
|
||||
List<(FileHolder, DateTime)> toDoCollection = GetToDoCollection(progressBar, fileHolders);
|
||||
progressBar.Dispose();
|
||||
log.Information($"Ready to set created date {toDoCollection.Count} file(s)?");
|
||||
for (int y = 0; y < int.MaxValue; y++)
|
||||
{
|
||||
log.Information("Press \"Y\" key to set created date file(s), \"N\" key to log file(s) or close console to not set created date files");
|
||||
consoleKey = System.Console.ReadKey().Key;
|
||||
if (consoleKey is ConsoleKey.Y or ConsoleKey.N)
|
||||
break;
|
||||
}
|
||||
log.Information(". . .");
|
||||
if (consoleKey is null || consoleKey.Value != ConsoleKey.Y)
|
||||
log.Information("Nothing set!");
|
||||
else
|
||||
{
|
||||
progressBar = new(allFiles.Count, message, options);
|
||||
results.AddRange(SetCreatedDateForeach(progressBar, toDoCollection));
|
||||
progressBar.Dispose();
|
||||
log.Information("Done setting created date");
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user