diff --git a/.vscode/launch.json b/.vscode/launch.json index adca1e6..9558da7 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -23,6 +23,23 @@ "stopAtEntry": false, "requireExactSource": false }, + { + "name": "Copy-Distinct", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "${workspaceFolder}/Copy-Distinct/bin/Debug/net7.0/win-x64/Copy-Distinct.dll", + "args": [ + "s" + ], + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "cwd": "${workspaceFolder}", + "console": "externalTerminal", + "stopAtEntry": false, + "requireExactSource": false + }, { "name": "Duplicate-Search", "type": "coreclr", diff --git a/Copy-Distinct/Copy-Distinct.cs b/Copy-Distinct/Copy-Distinct.cs new file mode 100644 index 0000000..725b292 --- /dev/null +++ b/Copy-Distinct/Copy-Distinct.cs @@ -0,0 +1,436 @@ +using CliWrap; +using Microsoft.Extensions.Configuration; +using Phares.Shared; +using Serilog; +using ShellProgressBar; +using System.Text.Json; +using View_by_Distance.Copy.Distinct.Models; +using View_by_Distance.Shared.Models; +using View_by_Distance.Shared.Models.Methods; + +namespace View_by_Distance.Copy.Distinct; + +public class CopyDistinct +{ + + private readonly AppSettings _AppSettings; + private readonly string _WorkingDirectory; + private readonly Configuration _Configuration; + private readonly IsEnvironment _IsEnvironment; + private readonly IConfigurationRoot _ConfigurationRoot; + private readonly Property.Models.Configuration _PropertyConfiguration; + + public CopyDistinct(List args, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console) + { + if (isSilent) + { } + if (console is null) + { } + bool any = false; + _AppSettings = appSettings; + _IsEnvironment = isEnvironment; + _WorkingDirectory = workingDirectory; + _ConfigurationRoot = configurationRoot; + ILogger? log = Log.ForContext(); + Property.Models.Configuration propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot); + Configuration configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration); + _PropertyConfiguration = propertyConfiguration; + _Configuration = configuration; + propertyConfiguration.Update(); + string? comparePathRoot = Path.GetDirectoryName(appSettings.ComparePathsFile); + if (comparePathRoot is null || comparePathRoot == propertyConfiguration.RootDirectory) + throw new Exception("Nested isn't allowed!"); + log.Information(propertyConfiguration.RootDirectory); + Verify(); + string json = File.ReadAllText(appSettings.ComparePathsFile); + MatchNginx[]? matchNginxCollection = JsonSerializer.Deserialize(json); + if (matchNginxCollection is null) + throw new NullReferenceException(nameof(matchNginxCollection)); + if (matchNginxCollection.Any()) + { + bool deleted; + for (int i = 1; i < 5; i++) + { + deleted = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(matchNginxCollection.First().ConvertedPath); + if (deleted && !any) + any = true; + } + } + if (!any) + { + List lines = CopyDistinctFilesInDirectories(log, matchNginxCollection); + File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines); + if (comparePathRoot != Path.GetPathRoot(matchNginxCollection[0].ConvertedPath)) + _ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(comparePathRoot); + } + } + + 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 List<(FileHolder, string)> GetToDoCollection(ProgressBar progressBar, string[] files) + { + List<(FileHolder, string)> results = new(); + int? id; + string fileName; + string? message; + string checkFile; + string? directory; + TimeSpan timeSpan; + DateTime? dateTime; + DateTime?[] dateTimes; + FileHolder fileHolder; + string[]? ffmpegFiles; + bool isIgnoreExtension; + string checkFileExtension; + DateTime? minimumDateTime; + const string jpg = ".jpg"; + const string jpeg = ".jpeg"; + List distinct = new(); + bool isValidImageFormatExtension; + bool nameWithoutExtensionIsIdFormat; + IReadOnlyList directories; + foreach (string file in files) + { + progressBar.Tick(); + fileHolder = new(file); + if (file.EndsWith(".rename")) + { + directory = Path.GetDirectoryName(file); + if (string.IsNullOrEmpty(directory)) + continue; + checkFile = Path.Combine(directory, $"rename_{Path.GetFileName(file[..^7])}"); + if (File.Exists(checkFile)) + continue; + if (distinct.Contains(checkFile)) + continue; + distinct.Add(checkFile); + results.Add(new(fileHolder, checkFile)); + continue; + } + if (file.EndsWith(".jpg.del")) + { + checkFile = file[..^4]; + if (File.Exists(checkFile)) + continue; + if (distinct.Contains(checkFile)) + continue; + distinct.Add(checkFile); + results.Add(new(fileHolder, checkFile)); + continue; + } + if (fileHolder.ExtensionLowered == ".id" || fileHolder.ExtensionLowered == ".lsv" || fileHolder.DirectoryName is null) + continue; + if (files.Contains($"{fileHolder.FullName}.id")) + continue; + nameWithoutExtensionIsIdFormat = Shared.Models.Stateless.Methods.IProperty.NameWithoutExtensionIsIdFormat(fileHolder); + if (nameWithoutExtensionIsIdFormat) + continue; + isValidImageFormatExtension = _PropertyConfiguration.ValidImageFormatExtensions.Contains(fileHolder.ExtensionLowered); + isIgnoreExtension = isValidImageFormatExtension && _PropertyConfiguration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered); + if (!isIgnoreExtension && isValidImageFormatExtension) + ffmpegFiles = null; + else + { + try + { directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(file); } + catch (Exception) { continue; } + CommandTask result = Cli.Wrap("ffmpeg.exe") + // .WithArguments(new[] { "-ss", "00:00:00", "-t", "00:00:00", "-i", file, "-qscale:v", "2", "-r", "0.01", $"{fileHolder.Name}-%4d.jpg" }) + .WithArguments(new[] { "-i", file, "-vframes", "1", $"{fileHolder.Name}-%4d.jpg" }) + .WithWorkingDirectory(fileHolder.DirectoryName) + .ExecuteAsync(); + result.Task.Wait(); + ffmpegFiles = Directory.GetFiles(fileHolder.DirectoryName, $"{fileHolder.Name}-*.jpg", SearchOption.TopDirectoryOnly); + if (!ffmpegFiles.Any()) + continue; + fileHolder = new(ffmpegFiles.First()); + if (!fileHolder.Name.EndsWith("-0001.jpg")) + throw new Exception(); + isValidImageFormatExtension = _PropertyConfiguration.ValidImageFormatExtensions.Contains(fileHolder.ExtensionLowered); + isIgnoreExtension = isValidImageFormatExtension && _PropertyConfiguration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered); + if (isIgnoreExtension || !isValidImageFormatExtension) + continue; + if (fileHolder.DirectoryName is null) + continue; + } + if (!isIgnoreExtension && isValidImageFormatExtension) + { + if (fileHolder.ExtensionLowered == jpeg) + { + if (File.Exists($"{fileHolder.FullName}.id")) + { + checkFile = Path.Combine(fileHolder.DirectoryName, $"{fileHolder.NameWithoutExtension}{jpg}.id"); + if (File.Exists(checkFile)) + continue; + if (distinct.Contains(checkFile)) + continue; + distinct.Add(checkFile); + results.Add(new(new($"{fileHolder.FullName}.id"), checkFile)); + } + checkFile = Path.Combine(fileHolder.DirectoryName, $"{fileHolder.NameWithoutExtension}{jpg}"); + if (File.Exists(checkFile)) + continue; + if (distinct.Contains(checkFile)) + continue; + distinct.Add(checkFile); + results.Add(new(fileHolder, checkFile)); + if (File.Exists(checkFile)) + continue; + File.Move(fileHolder.FullName, checkFile); + fileHolder = new(checkFile); + if (fileHolder.DirectoryName is null) + continue; + } + } + (dateTimes, id, message) = Shared.Models.Stateless.Methods.IProperty.Get(fileHolder, isIgnoreExtension, isValidImageFormatExtension); + minimumDateTime = dateTimes.Min(); + if (minimumDateTime is null || !isIgnoreExtension && isValidImageFormatExtension) + { + dateTime = Shared.Models.Stateless.Methods.IProperty.GetDateTimeFromName(fileHolder); + if (dateTime is null || minimumDateTime is null) + timeSpan = new TimeSpan(0); + else + timeSpan = new(Math.Abs(minimumDateTime.Value.Ticks - dateTime.Value.Ticks)); + } + else + { + fileName = Path.GetFileName(fileHolder.DirectoryName); + if (fileName.Length < 4 || !int.TryParse(fileName[..4], out int year)) + year = minimumDateTime.Value.Year; + if (_PropertyConfiguration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered)) + continue; + try + { directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(file); } + catch (Exception) { continue; } + dateTime = Metadata.Models.Stateless.Methods.IMetadata.GetMinimumDateTime(dateTimes, year, directories); + timeSpan = new TimeSpan(int.MaxValue); + } + if (dateTime is not null && string.IsNullOrEmpty(_AppSettings.CopyTo)) + { + checkFileExtension = fileHolder.ExtensionLowered == jpeg ? jpg : fileHolder.ExtensionLowered; + checkFile = Path.Combine(fileHolder.DirectoryName, $"{dateTime.Value:yyyy-MM-dd}.{dateTime.Value.Ticks}.{fileHolder.Length}{checkFileExtension}"); + if (checkFile == fileHolder.FullName) + continue; + if (distinct.Contains(checkFile)) + continue; + distinct.Add(checkFile); + results.Add(new(fileHolder, checkFile)); + if (ffmpegFiles is not null) + { + foreach (string ffmpegFile in ffmpegFiles) + File.Delete(ffmpegFile); + } + continue; + } + if (id is null) + continue; + if (ffmpegFiles is not null) + { + foreach (string ffmpegFile in ffmpegFiles) + File.Delete(ffmpegFile); + fileHolder = new(file); + if (fileHolder.DirectoryName is null) + continue; + } + checkFileExtension = fileHolder.ExtensionLowered == jpeg ? jpg : fileHolder.ExtensionLowered; + checkFile = Path.Combine(fileHolder.DirectoryName, $"{id.Value}{checkFileExtension}"); + if (checkFile == fileHolder.FullName) + continue; + if (File.Exists(checkFile)) + { + checkFile = string.Concat(checkFile, ".del"); + if (File.Exists(checkFile)) + continue; + } + if (distinct.Contains(checkFile)) + continue; + distinct.Add(checkFile); + results.Add(new(fileHolder, checkFile)); + } + return results; + } + + private static List GetAllFiles(MatchNginx[] matchNginxCollection) + { + List allFiles = new(); + string[] files; + string directoryName; + ReadOnlySpan span; + foreach (MatchNginx matchNginx in matchNginxCollection) + { + if (matchNginx.ConvertedPath.Length < 6) + continue; + directoryName = Path.GetFileName(matchNginx.ConvertedPath); + if (matchNginx.ConvertedPath.Contains("!---")) + span = "0"; + else + span = matchNginx.ConvertedPath.AsSpan(matchNginx.ConvertedPath.Length - 5, 3); + if (directoryName.Length == 1 && int.TryParse(span, out int age)) + continue; + if (File.Exists(matchNginx.ConvertedPath)) + continue; + if (!Directory.Exists(matchNginx.ConvertedPath)) + continue; + files = Directory.GetFiles(matchNginx.ConvertedPath, "*", SearchOption.AllDirectories); + if (files.All(l => l.EndsWith(".id"))) + { + foreach (string file in files) + File.Delete(file); + continue; + } + allFiles.AddRange(files); + } + return allFiles; + } + + private static void CreateDirectories(List directories) + { + foreach (string directory in directories) + { + if (Directory.Exists(directory)) + continue; + _ = Directory.CreateDirectory(directory); + } + } + + private List CopyInstead(ILogger log, List<(FileHolder, string)> verifiedToDoCollection) + { + List results = new(); + string copyTo; + string? directory; + ConsoleKey? consoleKey = null; + List distinctDirectories = new(); + List<(FileHolder, string)> copyCollection = new(); + foreach ((FileHolder fileHolder, string to) in verifiedToDoCollection) + { + copyTo = $"{_AppSettings.CopyTo}{to[1..]}"; + directory = Path.GetDirectoryName(copyTo); + if (directory is null) + continue; + copyCollection.Add(new(fileHolder, copyTo)); + if (distinctDirectories.Contains(directory)) + continue; + distinctDirectories.Add(directory); + } + CreateDirectories(distinctDirectories); + log.Information($"Ready to Copy {verifiedToDoCollection.Count} file(s)?"); + for (int y = 0; y < int.MaxValue; y++) + { + log.Information("Press \"Y\" key to copy file(s), \"N\" key to log file(s) or close console to not copy 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 Copied!"); + else + { + foreach ((FileHolder fileHolder, string to) in verifiedToDoCollection) + { + results.Add(fileHolder.NameWithoutExtension); + File.Copy(fileHolder.FullName, to); + } + log.Information("Done Copying"); + } + return results; + } + + private static List Move(ILogger log, List<(FileHolder, string)> verifiedToDoCollection) + { + List results = new(); + ConsoleKey? consoleKey = null; + log.Information($"Ready to Move {verifiedToDoCollection.Count} file(s)?"); + for (int y = 0; y < int.MaxValue; y++) + { + log.Information("Press \"Y\" key to move file(s), \"N\" key to log file(s) or close console to not move 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 moved!"); + else + { + foreach ((FileHolder fileHolder, string to) in verifiedToDoCollection) + { + results.Add(fileHolder.NameWithoutExtension); + try + { File.Move(fileHolder.FullName, to); } + catch (Exception) { } + } + log.Information("Done Moving"); + } + return results; + } +// { +// "_App": "Copy-Distinct", +// "_UserSecretsId": "d862524f-2b48-4f47-b4c3-5a8615814ec2", +// "ComparePathsFile": "C:/Users/lphar/AppData/Local/PharesApps/Drag-Drop-Explorer/2023_24/638220924947919275.json", +// "CopyTo": "", +// "MaxDegreeOfParallelism": 6, +// "Windows": { +// "Configuration": { +// "RootDirectory": "C:/", +// "VerifyToSeason": [] +// } +// } +// } + private List CopyDistinctFilesInDirectories(ILogger log, MatchNginx[] matchNginxCollection) + { + List results = new(); + string[] files; + string message; + List allFiles; + ProgressBar progressBar; + List<(FileHolder, string)> toDoCollection; + allFiles = GetAllFiles(matchNginxCollection); + List<(FileHolder, string)> verifiedToDoCollection; + ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true }; + for (int i = 1; i < 3; i++) + { + if (!allFiles.Any()) + continue; + message = $"{i}) Renaming files"; + files = i == 2 ? allFiles.ToArray() : (from l in allFiles where l.Contains("Rename", StringComparison.OrdinalIgnoreCase) select l).ToArray(); + progressBar = new(files.Length, message, options); + if (!files.Any()) + continue; + toDoCollection = GetToDoCollection(progressBar, files); + verifiedToDoCollection = new(); + foreach ((FileHolder fileHolder, string to) in toDoCollection) + { + if (File.Exists(to)) + continue; + verifiedToDoCollection.Add(new(fileHolder, to)); + if (string.IsNullOrEmpty(_AppSettings.CopyTo)) + File.WriteAllText($"{to}.id", $"{to}{Environment.NewLine}{fileHolder.FullName}"); + } + if (!verifiedToDoCollection.Any()) + continue; + if (string.IsNullOrEmpty(_AppSettings.CopyTo)) + results.AddRange(Move(log, toDoCollection)); + else + results.AddRange(CopyInstead(log, toDoCollection)); + allFiles = GetAllFiles(matchNginxCollection); + progressBar.Dispose(); + } + return results; + } + +} diff --git a/Copy-Distinct/Copy-Distinct.csproj b/Copy-Distinct/Copy-Distinct.csproj new file mode 100644 index 0000000..8d45585 --- /dev/null +++ b/Copy-Distinct/Copy-Distinct.csproj @@ -0,0 +1,62 @@ + + + enable + 10.0 + enable + Exe + win-x64 + net7.0 + d862524f-2b48-4f47-b4c3-5a8615814ec2 + + + Phares.View.by.Distance.Rename + false + 7.0.101.1 + Mike Phares + Phares + true + snupkg + + + true + true + true + + + Windows + + + OSX + + + Linux + + + + + + + + + + + + + + + + + + + + + + + + Always + + + Always + + + \ No newline at end of file diff --git a/Copy-Distinct/Models/AppSettings.cs b/Copy-Distinct/Models/AppSettings.cs new file mode 100644 index 0000000..73a0115 --- /dev/null +++ b/Copy-Distinct/Models/AppSettings.cs @@ -0,0 +1,35 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace View_by_Distance.Copy.Distinct.Models; + +public class AppSettings +{ + + public string Company { init; get; } + public string ComparePathsFile { init; get; } + public string CopyTo { init; get; } + public int MaxDegreeOfParallelism { init; get; } + public string WorkingDirectoryName { init; get; } + + [JsonConstructor] + public AppSettings(string company, + string comparePathsFile, + string copyTo, + int maxDegreeOfParallelism, + string workingDirectoryName) + { + Company = company; + ComparePathsFile = comparePathsFile; + CopyTo = copyTo; + MaxDegreeOfParallelism = maxDegreeOfParallelism; + WorkingDirectoryName = workingDirectoryName; + } + + public override string ToString() + { + string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + return result; + } + +} \ No newline at end of file diff --git a/Copy-Distinct/Models/Binder/AppSettings.cs b/Copy-Distinct/Models/Binder/AppSettings.cs new file mode 100644 index 0000000..41af481 --- /dev/null +++ b/Copy-Distinct/Models/Binder/AppSettings.cs @@ -0,0 +1,48 @@ +using Microsoft.Extensions.Configuration; +using System.Text.Json; + +namespace View_by_Distance.Copy.Distinct.Models.Binder; + +public class AppSettings +{ + +#nullable disable + + public string Company { get; set; } + public string ComparePathsFile { get; set; } + public string CopyTo { get; set; } + public int? MaxDegreeOfParallelism { get; set; } + public string WorkingDirectoryName { get; set; } + +#nullable restore + + public override string ToString() + { + string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + return result; + } + + private static Models.AppSettings Get(AppSettings? appSettings) + { + Models.AppSettings result; + if (appSettings?.MaxDegreeOfParallelism is null) + throw new NullReferenceException(nameof(appSettings.MaxDegreeOfParallelism)); + result = new( + appSettings.Company, + appSettings.ComparePathsFile, + appSettings.CopyTo, + appSettings.MaxDegreeOfParallelism.Value, + appSettings.WorkingDirectoryName + ); + return result; + } + + public static Models.AppSettings Get(IConfigurationRoot configurationRoot) + { + Models.AppSettings result; + AppSettings? appSettings = configurationRoot.Get(); + result = Get(appSettings); + return result; + } + +} \ No newline at end of file diff --git a/Copy-Distinct/Models/Binder/Configuration.cs b/Copy-Distinct/Models/Binder/Configuration.cs new file mode 100644 index 0000000..92e158e --- /dev/null +++ b/Copy-Distinct/Models/Binder/Configuration.cs @@ -0,0 +1,59 @@ +using Microsoft.Extensions.Configuration; +using Phares.Shared; +using System.ComponentModel.DataAnnotations; +using System.Text.Json; + +namespace View_by_Distance.Copy.Distinct.Models.Binder; + +public class Configuration +{ + +#nullable disable + + [Display(Name = "Ignore Extensions"), Required] public string[] IgnoreExtensions { get; set; } + [Display(Name = "Property Configuration"), Required] public Property.Models.Configuration PropertyConfiguration { get; set; } + [Display(Name = "Person Birthday Format"), Required] public string PersonBirthdayFormat { get; set; } + +#nullable restore + + public override string ToString() + { + string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + return result; + } + + private static Models.Configuration Get(Configuration? configuration) + { + Models.Configuration result; + if (configuration is null) + throw new NullReferenceException(nameof(configuration)); + if (configuration.IgnoreExtensions is null) + throw new NullReferenceException(nameof(configuration.IgnoreExtensions)); + if (configuration.PersonBirthdayFormat is null) + throw new NullReferenceException(nameof(configuration.PersonBirthdayFormat)); + result = new( + configuration.IgnoreExtensions, + configuration.PersonBirthdayFormat, + configuration.PropertyConfiguration); + return result; + } + + public static Models.Configuration Get(IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, Property.Models.Configuration propertyConfiguration) + { + Models.Configuration result; + Configuration? configuration; + if (isEnvironment is null) + configuration = configurationRoot.Get(); + else + { + string environmentName = IsEnvironment.GetEnvironmentName(isEnvironment); + string section = string.Concat(environmentName, ":", nameof(Configuration)); + IConfigurationSection configurationSection = configurationRoot.GetSection(section); + configuration = configurationSection.Get(); + } + result = Get(configuration); + result.SetAndUpdate(propertyConfiguration); + return result; + } + +} \ No newline at end of file diff --git a/Copy-Distinct/Models/Configuration.cs b/Copy-Distinct/Models/Configuration.cs new file mode 100644 index 0000000..bdfe904 --- /dev/null +++ b/Copy-Distinct/Models/Configuration.cs @@ -0,0 +1,38 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace View_by_Distance.Copy.Distinct.Models; + +public class Configuration +{ + + protected Property.Models.Configuration _PropertyConfiguration; + public string[] IgnoreExtensions { init; get; } + public string PersonBirthdayFormat { init; get; } + + public Property.Models.Configuration PropertyConfiguration => _PropertyConfiguration; + + [JsonConstructor] + public Configuration( + string[] ignoreExtensions, + string personBirthdayFormat, + Property.Models.Configuration propertyConfiguration) + { + IgnoreExtensions = ignoreExtensions; + PersonBirthdayFormat = personBirthdayFormat; + _PropertyConfiguration = propertyConfiguration; + } + + public override string ToString() + { + string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + return result; + } + + public void SetAndUpdate(Property.Models.Configuration configuration) + { + _PropertyConfiguration = configuration; + _PropertyConfiguration.Update(); + } + +} \ No newline at end of file diff --git a/Copy-Distinct/Models/Stateless/SerilogExtensionMethods.cs b/Copy-Distinct/Models/Stateless/SerilogExtensionMethods.cs new file mode 100644 index 0000000..be42b73 --- /dev/null +++ b/Copy-Distinct/Models/Stateless/SerilogExtensionMethods.cs @@ -0,0 +1,10 @@ +namespace View_by_Distance.Copy.Distinct.Models.Stateless; + +public static class SerilogExtensionMethods +{ + + internal static void Warn(this Serilog.ILogger log, string messageTemplate) => log.Warning(messageTemplate); + + internal static void Info(this Serilog.ILogger log, string messageTemplate) => log.Information(messageTemplate); + +} \ No newline at end of file diff --git a/Copy-Distinct/Program.cs b/Copy-Distinct/Program.cs new file mode 100644 index 0000000..45fbadc --- /dev/null +++ b/Copy-Distinct/Program.cs @@ -0,0 +1,71 @@ +using Microsoft.Extensions.Configuration; +using Phares.Shared; +using Serilog; +using System.Diagnostics; +using System.Reflection; +using View_by_Distance.Copy.Distinct.Models; +using View_by_Distance.Shared.Models.Stateless.Methods; + +namespace View_by_Distance.Copy.Distinct; + +public class Program +{ + + public static void Secondary(List args) + { + LoggerConfiguration loggerConfiguration = new(); + Assembly assembly = Assembly.GetExecutingAssembly(); + bool debuggerWasAttachedAtLineZero = Debugger.IsAttached || assembly.Location.Contains(@"\bin\Debug"); + IsEnvironment isEnvironment = new(processesCount: null, nullASPNetCoreEnvironmentIsDevelopment: debuggerWasAttachedAtLineZero, nullASPNetCoreEnvironmentIsProduction: !debuggerWasAttachedAtLineZero); + IConfigurationBuilder configurationBuilder = new ConfigurationBuilder() + .AddEnvironmentVariables() + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .AddJsonFile(isEnvironment.AppSettingsFileName, optional: false, reloadOnChange: true) + .AddUserSecrets(); + IConfigurationRoot configurationRoot = configurationBuilder.Build(); + AppSettings appSettings = Models.Binder.AppSettings.Get(configurationRoot); + if (appSettings.MaxDegreeOfParallelism > Environment.ProcessorCount) + throw new Exception("MaxDegreeOfParallelism must be =< Environment.ProcessorCount!"); + if (string.IsNullOrEmpty(appSettings.WorkingDirectoryName)) + throw new Exception("Working directory name must have a value!"); + string workingDirectory = IWorkingDirectory.GetWorkingDirectory(assembly.GetName().Name, appSettings.WorkingDirectoryName); + Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory); + _ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, configurationRoot); + Log.Logger = loggerConfiguration.CreateLogger(); + ILogger log = Log.ForContext(); + int silentIndex = args.IndexOf("s"); + if (silentIndex > -1) + args.RemoveAt(silentIndex); + try + { + if (args is null) + throw new Exception("args is null!"); + Shared.Models.Console console = new(); + _ = new CopyDistinct(args, isEnvironment, configurationRoot, appSettings, workingDirectory, silentIndex > -1, console); + } + catch (Exception ex) + { + log.Fatal(string.Concat(ex.Message, Environment.NewLine, ex.StackTrace)); + } + finally + { + Log.CloseAndFlush(); + } + if (silentIndex > -1) + log.Debug("Done. Bye"); + else + { + log.Debug("Done. Press 'Enter' to end"); + _ = Console.ReadLine(); + } + } + + public static void Main(string[] args) + { + if (args is not null) + Secondary(args.ToList()); + else + Secondary(new List()); + } + +} \ No newline at end of file diff --git a/Copy-Distinct/appsettings.Development.json b/Copy-Distinct/appsettings.Development.json new file mode 100644 index 0000000..ad9619a --- /dev/null +++ b/Copy-Distinct/appsettings.Development.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Log4netProvider": "Debug" + } + }, + "Serilog": { + "MinimumLevel": "Debug" + } +} \ No newline at end of file diff --git a/Copy-Distinct/appsettings.json b/Copy-Distinct/appsettings.json new file mode 100644 index 0000000..119a261 --- /dev/null +++ b/Copy-Distinct/appsettings.json @@ -0,0 +1,121 @@ +{ + "ComparePathsFile": "", + "Company": "Mike Phares", + "CopyTo": "", + "Linux": {}, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Log4netProvider": "Debug", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "MaxDegreeOfParallelism": 6, + "Serilog": { + "Using": [ + "Serilog.Sinks.Console", + "Serilog.Sinks.File" + ], + "MinimumLevel": "Information", + "WriteTo": [ + { + "Name": "Debug", + "Args": { + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}" + } + }, + { + "Name": "Console", + "Args": { + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}" + } + }, + { + "Name": "File", + "Args": { + "path": "%workingDirectory% - Log/log-.txt", + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}", + "rollingInterval": "Hour" + } + } + ], + "Enrich": [ + "FromLogContext", + "WithMachineName", + "WithThreadId" + ], + "Properties": { + "Application": "Sample" + } + }, + "WorkingDirectoryName": "PharesApps", + "Windows": { + "Configuration": { + "DateGroup": "1e85c0ba", + "DiffPropertyDirectory": "", + "FileNameDirectorySeparator": ".Z.", + "ForcePropertyLastWriteTimeToCreationTime": false, + "MaxImagesInDirectoryForTopLevelFirstPass": 10, + "OutputExtension": ".jpg", + "Pattern": "[^ABCDEFGHIJKLMNOPQRSTUVWXYZbcdfghjklmnpqrstvwxyz0-9]", + "PersonBirthdayFormat": "yyyy-MM-dd_HH", + "PopulatePropertyId": true, + "PropertiesChangedForProperty": false, + "ResultAllInOne": "_ _ _", + "ResultCollection": "[]", + "ResultContent": "()", + "ResultSingleton": "{}", + "RootDirectory": "D:/Images", + "WriteBitmapDataBytes": false, + "IgnoreExtensions": [ + ".gif", + ".GIF", + ".pdf", + ".PDF" + ], + "ValidImageFormatExtensions": [ + ".bmp", + ".BMP", + ".gif", + ".GIF", + ".jpeg", + ".JPEG", + ".jpg", + ".JPG", + ".png", + ".PNG", + ".tiff", + ".TIFF" + ], + "ValidMetadataExtensions": [ + ".3gp", + ".3GP", + ".avi", + ".AVI", + ".bmp", + ".BMP", + ".gif", + ".GIF", + ".ico", + ".ICO", + ".jpeg", + ".JPEG", + ".jpg", + ".JPG", + ".m4v", + ".M4V", + ".mov", + ".MOV", + ".mp4", + ".MP4", + ".mta", + ".MTA", + ".png", + ".PNG", + ".tiff", + ".TIFF" + ] + } + } +} \ No newline at end of file diff --git a/View-by-Distance-MKLink-Console.sln b/View-by-Distance-MKLink-Console.sln index 2e4d7eb..8ba6163 100644 --- a/View-by-Distance-MKLink-Console.sln +++ b/View-by-Distance-MKLink-Console.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.30114.105 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlurHash", "BlurHash\BlurHash.csproj", "{9689371E-F67C-4392-A636-C398D28C089B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Copy-Distinct", "Copy-Distinct\Copy-Distinct.csproj", "{E08CB662-FF25-48DF-A378-A770E1EFBA56}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Date-Group", "Date-Group\Date-Group.csproj", "{DFEDB5F9-AFFC-40A2-9FEC-9B84C83B63D9}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Delete-By-Distinct", "Delete-By-Distinct\Delete-By-Distinct.csproj", "{3F00BDD5-75F8-470C-ACED-1A26FDC8D7B3}" @@ -178,5 +180,9 @@ Global {6F146145-F7A4-4AB7-94C4-C822A9FC8269}.Debug|Any CPU.Build.0 = Debug|Any CPU {6F146145-F7A4-4AB7-94C4-C822A9FC8269}.Release|Any CPU.ActiveCfg = Release|Any CPU {6F146145-F7A4-4AB7-94C4-C822A9FC8269}.Release|Any CPU.Build.0 = Release|Any CPU + {E08CB662-FF25-48DF-A378-A770E1EFBA56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E08CB662-FF25-48DF-A378-A770E1EFBA56}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E08CB662-FF25-48DF-A378-A770E1EFBA56}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E08CB662-FF25-48DF-A378-A770E1EFBA56}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal