This commit is contained in:
2022-11-20 23:20:28 -07:00
parent c6aa7e8e3c
commit 3b988ba152
85 changed files with 1355 additions and 980 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.Extensions.Configuration;
using Microsoft.WindowsAPICodePack.Shell;
using Phares.Shared;
using Serilog;
using System.Diagnostics;
@ -15,7 +16,9 @@ public partial class Form : System.Windows.Forms.Form
private readonly ILogger _Logger;
private readonly TextBox _TextBox;
private readonly List<string> _Lines;
private readonly AppSettings _AppSettings;
private readonly ProgressBar _ProgressBar;
private readonly string _WorkingDirectory;
private readonly Configuration _Configuration;
private readonly IsEnvironment _IsEnvironment;
@ -27,6 +30,7 @@ public partial class Form : System.Windows.Forms.Form
public Form()
{
InitializeComponent();
_Lines = new();
ILogger logger;
AppSettings appSettings;
string workingDirectory;
@ -62,7 +66,9 @@ public partial class Form : System.Windows.Forms.Form
_PropertyConfiguration = propertyConfiguration;
_ResizeFileNameExtension = resizeFileNameExtension;
_TextBox = new() { Location = new(5, 5), Dock = DockStyle.Top };
_ProgressBar = new() { Location = new(5, 5), Dock = DockStyle.Top, Visible = false };
Load += new EventHandler(Form1_Load);
Controls.Add(_ProgressBar);
Controls.Add(_TextBox);
}
@ -70,30 +76,23 @@ public partial class Form : System.Windows.Forms.Form
{
try
{
int j;
int f;
int t;
AllowDrop = true;
Container[] containers;
DragDrop += new DragEventHandler(Form1_DragDrop);
DragEnter += new DragEventHandler(Form1_DragEnter);
Property.Models.A_Property propertyLogic = new(_AppSettings.MaxDegreeOfParallelism, _Configuration.PropertyConfiguration, _ResizeFileNameExtension, _Configuration.Reverse);
(j, f, t, containers) = Property.Models.Stateless.Container.GetContainers(_Configuration.PropertyConfiguration, propertyLogic);
List<MappingFromItem> collection = Program.GetMappingFromItemCollection(_Configuration, containers);
foreach (MappingFromItem mappingFromItem in collection)
{
if (_IdToMappingFromItem.ContainsKey(mappingFromItem.Id))
continue;
_IdToMappingFromItem.Add(mappingFromItem.Id, mappingFromItem);
}
if (_Logger is null)
throw new NullReferenceException(nameof(_Logger));
_Logger.Debug((_AppSettings is null).ToString());
_Logger.Debug((_Configuration is null).ToString());
_Logger.Debug((_IsEnvironment is null).ToString());
_Logger.Debug((_WorkingDirectory is null).ToString());
_Logger.Debug((_ConfigurationRoot is null).ToString());
_Logger.Debug((_PropertyConfiguration is null).ToString());
_TextBox.LostFocus += new EventHandler(TextBox_LostFocus);
}
catch (Exception)
{
throw;
}
}
void TextBox_LostFocus(object? sender, EventArgs e)
{
try
{
if (_TextBox.Text == "ps")
throw new NotImplementedException();
}
catch (Exception)
{
@ -114,29 +113,166 @@ public partial class Form : System.Windows.Forms.Form
}
}
void LoadData()
{
Container[] containers;
Property.Models.A_Property propertyLogic = new(_AppSettings.MaxDegreeOfParallelism, _Configuration.PropertyConfiguration, _ResizeFileNameExtension, _Configuration.Reverse);
(_, _, _, containers) = Property.Models.Stateless.Container.GetContainers(_Configuration.PropertyConfiguration, propertyLogic);
List<MappingFromItem> collection = Program.GetMappingFromItemCollection(_Configuration, containers);
foreach (MappingFromItem mappingFromItem in collection)
{
if (_IdToMappingFromItem.ContainsKey(mappingFromItem.Id))
continue;
_IdToMappingFromItem.Add(mappingFromItem.Id, mappingFromItem);
}
if (_Logger is null)
throw new NullReferenceException(nameof(_Logger));
_Logger.Debug((_AppSettings is null).ToString());
_Logger.Debug((_Configuration is null).ToString());
_Logger.Debug((_IsEnvironment is null).ToString());
_Logger.Debug((_WorkingDirectory is null).ToString());
_Logger.Debug((_ConfigurationRoot is null).ToString());
_Logger.Debug((_PropertyConfiguration is null).ToString());
}
private void RenameDirectory(string path, string searchPattern)
{
string[] directories = Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly);
foreach (string directory in directories)
RenameDirectory(directory, searchPattern);
int? id;
string? message;
string checkFile;
DateTime? dateTime;
FileHolder fileHolder;
_ProgressBar.Step = 1;
bool isIgnoreExtension;
_ProgressBar.Value = 0;
DateTime? minimumDateTime;
_ProgressBar.Visible = true;
bool isValidImageFormatExtension;
string? extraLargeBitmapThumbnail;
string[] files = Directory.GetFiles(path, searchPattern, SearchOption.TopDirectoryOnly);
_ProgressBar.Maximum = files.Length;
foreach (string file in files)
{
fileHolder = new(file);
_Lines.Add(fileHolder.NameWithoutExtension);
isValidImageFormatExtension = _Configuration.PropertyConfiguration.ValidImageFormatExtensions.Contains(fileHolder.ExtensionLowered);
isIgnoreExtension = isValidImageFormatExtension && _Configuration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered);
if (fileHolder.CreationTime is null || fileHolder.Name.Contains(fileHolder.CreationTime.Value.ToString("yy")))
continue;
if (fileHolder.LastWriteTime is null || fileHolder.Name.Contains(fileHolder.LastWriteTime.Value.ToString("yy")))
continue;
if (fileHolder.NameWithoutExtension.Length == 1 || fileHolder.NameWithoutExtension[1..].All(l => char.IsNumber(l)))
continue;
if (fileHolder.NameWithoutExtension.Length == 1 || fileHolder.NameWithoutExtension[1..].All(l => char.IsNumber(l)))
continue;
if (!isIgnoreExtension && isValidImageFormatExtension)
extraLargeBitmapThumbnail = null;
else
{
extraLargeBitmapThumbnail = SaveExtraLargeBitmapThumbnail(fileHolder);
if (extraLargeBitmapThumbnail is null)
continue;
fileHolder = new(extraLargeBitmapThumbnail);
isValidImageFormatExtension = _Configuration.PropertyConfiguration.ValidImageFormatExtensions.Contains(fileHolder.ExtensionLowered);
isIgnoreExtension = isValidImageFormatExtension && _Configuration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered);
if (isIgnoreExtension || !isValidImageFormatExtension)
continue;
}
if (fileHolder.DirectoryName is null)
continue;
dateTime = IProperty.GetDateTimeFromName(fileHolder);
if (dateTime is not null && fileHolder.Name.Contains(dateTime.Value.ToString("yy")))
continue;
(minimumDateTime, id, message) = IProperty.Get(fileHolder);
if (id is null)
continue;
if (minimumDateTime is not null && fileHolder.Name.Contains(minimumDateTime.Value.ToString("yy")))
continue;
if (dateTime is not null && minimumDateTime is not null && minimumDateTime.Value != dateTime.Value)
continue;
if (extraLargeBitmapThumbnail is not null)
{
File.Delete(fileHolder.FullName);
fileHolder = new(file);
if (fileHolder.DirectoryName is null)
continue;
}
checkFile = Path.Combine(fileHolder.DirectoryName, $"{id}{fileHolder.ExtensionLowered}");
if (File.Exists(checkFile))
continue;
File.Move(fileHolder.FullName, checkFile);
_ProgressBar.PerformStep();
}
_ProgressBar.Visible = false;
}
private string? SaveExtraLargeBitmapThumbnail(FileHolder fileHolder)
{
string? result;
ShellFile shellFile = ShellFile.FromFilePath(fileHolder.FullName);
if (shellFile is null || shellFile.Thumbnail is null || shellFile.Thumbnail.ExtraLargeBitmap.Clone() is not Bitmap bitmap || bitmap.Width == 0)
result = null;
else
{
result = $"{fileHolder.FullName}{_ResizeFileNameExtension}";
bitmap.Save(result);
bitmap.Dispose();
shellFile.Dispose();
}
return result;
}
void Form1_DragDrop(object? sender, DragEventArgs e)
{
try
{
if (e.Data is not null)
if (e.Data is null)
_TextBox.Text = string.Empty;
else
{
string fileName;
string name;
string[] segments;
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
for (int i = 1; i < 3; i++)
{
fileName = Path.GetFileNameWithoutExtension(file);
Text = fileName;
segments = fileName.Split('.');
if (int.TryParse(segments[0], out int id) && _IdToMappingFromItem.ContainsKey(id))
if (e.Data.GetData(DataFormats.FileDrop) is not string[] paths)
continue;
foreach (string path in paths)
{
MappingFromItem mappingFromItem = _IdToMappingFromItem[id];
Text = mappingFromItem.ImageFileHolder.Name;
_TextBox.Text = mappingFromItem.ImageFileHolder.FullName;
_Logger.Information(mappingFromItem.ImageFileHolder.DirectoryName);
if (!string.IsNullOrEmpty(mappingFromItem.ImageFileHolder.DirectoryName))
_ = Process.Start("explorer.exe", string.Concat("\"", mappingFromItem.ImageFileHolder.DirectoryName, "\""));
name = Path.GetFileNameWithoutExtension(path);
Text = name;
segments = name.Split('.');
if (Directory.Exists(path))
{
if (i == 1)
RenameDirectory(path, "*Rename*");
else if (i == 2)
RenameDirectory(path, "*");
else
continue;
}
else
{
if (i != 1)
continue;
if (!_IdToMappingFromItem.Any())
LoadData();
if (int.TryParse(segments[0], out int id) && _IdToMappingFromItem.TryGetValue(id, out MappingFromItem? value))
{
MappingFromItem mappingFromItem = value;
Text = mappingFromItem.ImageFileHolder.Name;
_TextBox.Text = mappingFromItem.ImageFileHolder.FullName;
if (mappingFromItem.ImageFileHolder.DirectoryName is not null)
_Logger.Information(mappingFromItem.ImageFileHolder.DirectoryName);
if (!string.IsNullOrEmpty(mappingFromItem.ImageFileHolder.DirectoryName))
_ = Process.Start("explorer.exe", string.Concat("\"", mappingFromItem.ImageFileHolder.DirectoryName, "\""));
}
}
}
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", _Lines);
_Lines.Clear();
}
}
}