Move-By-Id
This commit is contained in:
186
Move-By-Id/MoveById.cs
Normal file
186
Move-By-Id/MoveById.cs
Normal file
@ -0,0 +1,186 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Phares.Shared;
|
||||
using Serilog;
|
||||
using ShellProgressBar;
|
||||
using View_by_Distance.Move.By.Id.Models;
|
||||
using View_by_Distance.Shared.Models;
|
||||
using View_by_Distance.Shared.Models.Methods;
|
||||
|
||||
namespace View_by_Distance.Move.By.Id;
|
||||
|
||||
public class MoveById
|
||||
{
|
||||
|
||||
private readonly AppSettings _AppSettings;
|
||||
private readonly string _WorkingDirectory;
|
||||
private readonly IsEnvironment _IsEnvironment;
|
||||
private readonly Configuration _Configuration;
|
||||
private readonly IConfigurationRoot _ConfigurationRoot;
|
||||
private readonly Property.Models.Configuration _PropertyConfiguration;
|
||||
|
||||
public MoveById(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<MoveById>();
|
||||
Property.Models.Configuration propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
|
||||
Configuration configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration);
|
||||
_PropertyConfiguration = propertyConfiguration;
|
||||
_Configuration = configuration;
|
||||
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
||||
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 = System.Text.Json.JsonSerializer.Deserialize<MatchNginx[]>(json);
|
||||
if (matchNginxCollection is null)
|
||||
throw new NullReferenceException(nameof(matchNginxCollection));
|
||||
if (matchNginxCollection.Any())
|
||||
{
|
||||
List<string> lines = MoveFilesByIdInDirectories(options, 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)
|
||||
{ }
|
||||
}
|
||||
|
||||
private static List<string> GetAllFiles(MatchNginx[] matchNginxCollection)
|
||||
{
|
||||
List<string> allFiles = new();
|
||||
string[] files;
|
||||
string directoryName;
|
||||
ReadOnlySpan<char> 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;
|
||||
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 List<string> GetToDoCollection(ProgressBar progressBar, List<string> allFiles)
|
||||
{
|
||||
List<string> results = new();
|
||||
int? id;
|
||||
string? message;
|
||||
string[] matches;
|
||||
DateTime?[] dateTimes;
|
||||
FileHolder fileHolder;
|
||||
bool isIgnoreExtension;
|
||||
const string jpeg = ".jpeg";
|
||||
bool isValidImageFormatExtension;
|
||||
bool nameWithoutExtensionIsIdFormat;
|
||||
foreach (string file in allFiles)
|
||||
{
|
||||
progressBar.Tick();
|
||||
fileHolder = new(file);
|
||||
if (fileHolder.ExtensionLowered == ".id" || fileHolder.DirectoryName is null)
|
||||
continue;
|
||||
if (allFiles.Contains($"{fileHolder.FullName}.id"))
|
||||
continue;
|
||||
isValidImageFormatExtension = _PropertyConfiguration.ValidImageFormatExtensions.Contains(fileHolder.ExtensionLowered);
|
||||
isIgnoreExtension = isValidImageFormatExtension && _Configuration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered);
|
||||
nameWithoutExtensionIsIdFormat = Shared.Models.Stateless.Methods.IProperty.NameWithoutExtensionIsIdFormat(fileHolder);
|
||||
if (!isIgnoreExtension && isValidImageFormatExtension)
|
||||
{
|
||||
if (fileHolder.ExtensionLowered == jpeg)
|
||||
continue;
|
||||
if (nameWithoutExtensionIsIdFormat)
|
||||
continue;
|
||||
}
|
||||
(dateTimes, id, message) = Shared.Models.Stateless.Methods.IProperty.Get(fileHolder, isIgnoreExtension, isValidImageFormatExtension);
|
||||
if (id is null)
|
||||
continue;
|
||||
matches = (from l in allFiles where l.Contains($"{id}{fileHolder.ExtensionLowered}") select l).ToArray();
|
||||
if (!matches.Any())
|
||||
continue;
|
||||
results.Add(fileHolder.FullName);
|
||||
results.AddRange(matches);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void CreateDirectories(List<string> directories)
|
||||
{
|
||||
foreach (string directory in directories)
|
||||
{
|
||||
if (Directory.Exists(directory))
|
||||
continue;
|
||||
_ = Directory.CreateDirectory(directory);
|
||||
}
|
||||
}
|
||||
|
||||
private List<string> MoveFilesByIdInDirectories(ProgressBarOptions options, MatchNginx[] matchNginxCollection)
|
||||
{
|
||||
List<string> results = new();
|
||||
string moveTo;
|
||||
string? directory;
|
||||
string message = "Moving allFiles";
|
||||
List<string> distinctDirectories = new();
|
||||
List<string> allFiles = GetAllFiles(matchNginxCollection);
|
||||
ProgressBar progressBar = new(allFiles.Count, message, options);
|
||||
List<string> toDoCollection = GetToDoCollection(progressBar, allFiles);
|
||||
List<(string, string)> moveCollection = new();
|
||||
foreach (string file in toDoCollection)
|
||||
{
|
||||
moveTo = $"{_AppSettings.MoveTo}{file[1..]}";
|
||||
directory = Path.GetDirectoryName(moveTo);
|
||||
if (directory is null)
|
||||
continue;
|
||||
moveCollection.Add(new(file, moveTo));
|
||||
if (distinctDirectories.Contains(directory))
|
||||
continue;
|
||||
distinctDirectories.Add(directory);
|
||||
}
|
||||
CreateDirectories(distinctDirectories);
|
||||
foreach ((string from, string to) in moveCollection)
|
||||
{
|
||||
if (File.Exists(to))
|
||||
continue;
|
||||
File.Move(from, to);
|
||||
}
|
||||
progressBar.Dispose();
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user