285 lines
12 KiB
C#
285 lines
12 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.WindowsAPICodePack.Shell;
|
|
using Phares.Shared;
|
|
using Serilog;
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using View_by_Distance.Drag_Drop.Models;
|
|
using View_by_Distance.Resize.Models;
|
|
using View_by_Distance.Shared.Models;
|
|
using View_by_Distance.Shared.Models.Stateless.Methods;
|
|
|
|
namespace View_by_Distance.Drag_Drop;
|
|
|
|
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;
|
|
private readonly string _ResizeFileNameExtension;
|
|
private readonly IConfigurationRoot _ConfigurationRoot;
|
|
private readonly Property.Models.Configuration _PropertyConfiguration;
|
|
private readonly Dictionary<int, MappingFromItem> _IdToMappingFromItem;
|
|
|
|
public Form()
|
|
{
|
|
InitializeComponent();
|
|
_Lines = new();
|
|
ILogger logger;
|
|
AppSettings appSettings;
|
|
string workingDirectory;
|
|
Configuration configuration;
|
|
IsEnvironment isEnvironment;
|
|
_IdToMappingFromItem = new();
|
|
IConfigurationRoot configurationRoot;
|
|
LoggerConfiguration loggerConfiguration = new();
|
|
Property.Models.Configuration propertyConfiguration;
|
|
Assembly assembly = Assembly.GetExecutingAssembly();
|
|
bool debuggerWasAttachedAtLineZero = Debugger.IsAttached || assembly.Location.Contains(@"\bin\Debug");
|
|
isEnvironment = new(processesCount: null, nullASPNetCoreEnvironmentIsDevelopment: debuggerWasAttachedAtLineZero, nullASPNetCoreEnvironmentIsProduction: !debuggerWasAttachedAtLineZero);
|
|
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
|
|
.AddEnvironmentVariables()
|
|
.AddJsonFile(isEnvironment.AppSettingsFileName);
|
|
configurationRoot = configurationBuilder.Build();
|
|
appSettings = Models.Binder.AppSettings.Get(configurationRoot);
|
|
workingDirectory = IWorkingDirectory.GetWorkingDirectory(assembly.GetName().Name, appSettings.WorkingDirectoryName);
|
|
Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory);
|
|
_ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, configurationRoot);
|
|
Log.Logger = loggerConfiguration.CreateLogger();
|
|
logger = Log.ForContext<Form>();
|
|
propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
|
|
configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration);
|
|
(_, _, string resizeFileNameExtension) = C_Resize.GetTuple(configuration.OutputExtension, configuration.OutputQuality);
|
|
logger.Information("Complete");
|
|
_Logger = logger;
|
|
_AppSettings = appSettings;
|
|
_Configuration = configuration;
|
|
_IsEnvironment = isEnvironment;
|
|
_WorkingDirectory = workingDirectory;
|
|
_ConfigurationRoot = configurationRoot;
|
|
_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);
|
|
}
|
|
|
|
void Form1_Load(object? sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
AllowDrop = true;
|
|
DragDrop += new DragEventHandler(Form1_DragDrop);
|
|
DragEnter += new DragEventHandler(Form1_DragEnter);
|
|
_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)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
void Form1_DragEnter(object? sender, DragEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (e.Data is not null && e.Data.GetDataPresent(DataFormats.FileDrop))
|
|
e.Effect = DragDropEffects.Copy;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
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 null)
|
|
_TextBox.Text = string.Empty;
|
|
else
|
|
{
|
|
string name;
|
|
string[] segments;
|
|
for (int i = 1; i < 3; i++)
|
|
{
|
|
if (e.Data.GetData(DataFormats.FileDrop) is not string[] paths)
|
|
continue;
|
|
foreach (string path in paths)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
} |