LookForAbandoned,
DateTimeOriginalThenMinimumDateTime and IsNotUniqueAndNeedsReview
This commit is contained in:
@ -37,6 +37,7 @@
|
||||
<PackageReference Include="WindowsShortcutFactory" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Property\Property.csproj" />
|
||||
<ProjectReference Include="..\Shared\View-by-Distance.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
@ -2,9 +2,11 @@ using Microsoft.Extensions.Configuration;
|
||||
using Phares.Shared;
|
||||
using Serilog;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using View_by_Distance.Drag_Drop_Explorer.Models;
|
||||
using View_by_Distance.Shared.Models.Stateless;
|
||||
using View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
using WindowsShortcutFactory;
|
||||
|
||||
@ -64,7 +66,7 @@ public partial class DragDropMove : Form
|
||||
Controls.Add(_FirstTextBox);
|
||||
}
|
||||
|
||||
void Form1_Load(object? sender, EventArgs e)
|
||||
private void Form1_Load(object? sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -73,6 +75,8 @@ public partial class DragDropMove : Form
|
||||
DragEnter += new DragEventHandler(Form1_DragEnter);
|
||||
_FirstTextBox.LostFocus += new EventHandler(TextBox_LostFocus);
|
||||
_PathTextBox.LostFocus += new EventHandler(TextBox_LostFocus);
|
||||
if (_WorkingDirectory is null)
|
||||
{ }
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@ -86,14 +90,14 @@ public partial class DragDropMove : Form
|
||||
return result;
|
||||
}
|
||||
|
||||
void TextBox_LostFocus(object? sender, EventArgs e)
|
||||
private void TextBox_LostFocus(object? sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (sender is TextBox textBox)
|
||||
{
|
||||
textBox.Text = GetConverted(textBox.Text);
|
||||
if (textBox.Text == "ps")
|
||||
if (textBox.Text == "315360000000000")
|
||||
throw new NotImplementedException();
|
||||
|
||||
}
|
||||
@ -104,7 +108,7 @@ public partial class DragDropMove : Form
|
||||
}
|
||||
}
|
||||
|
||||
void Form1_DragEnter(object? sender, DragEventArgs e)
|
||||
private void Form1_DragEnter(object? sender, DragEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -172,12 +176,145 @@ public partial class DragDropMove : Form
|
||||
}
|
||||
}
|
||||
|
||||
void Form1_DragDrop(object? sender, DragEventArgs e)
|
||||
public static byte[] GetBytes(string value)
|
||||
{
|
||||
byte[] results = new byte[value.Length + 1];
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
results[i] = (byte)value[i];
|
||||
results[value.Length] = 0x00;
|
||||
return results;
|
||||
}
|
||||
|
||||
private static PropertyItem GetPropertyItem(ConstructorInfo constructorInfo, int id, string value)
|
||||
{
|
||||
PropertyItem result = (PropertyItem)constructorInfo.Invoke(null);
|
||||
byte[] bytes = GetBytes(value);
|
||||
result.Id = id;
|
||||
result.Len = value.Length + 1;
|
||||
result.Type = 2;
|
||||
result.Value = bytes;
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<(string, int, DateTime)> GetCollection(string checkDirectory, DateTime minimumDateTime, DateTime maximumDateTime, long ticks)
|
||||
{
|
||||
List<(string, int, DateTime)> results = new();
|
||||
DateTime dateTime;
|
||||
Shared.Models.Property property;
|
||||
string[] files = Directory.GetFiles(checkDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
property = Property.Models.A_Property.GetImageProperty(file);
|
||||
if (property.Id is null || property.DateTimeOriginal is null)
|
||||
continue;
|
||||
dateTime = property.DateTimeOriginal.Value.AddTicks(ticks);
|
||||
if (dateTime < minimumDateTime)
|
||||
continue;
|
||||
if (dateTime > maximumDateTime)
|
||||
continue;
|
||||
results.Add((file, property.Id.Value, property.DateTimeOriginal.Value.AddTicks(ticks)));
|
||||
}
|
||||
if (files.Length != results.Count)
|
||||
throw new Exception();
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void DateFix(string sourceDirectory, string checkDirectory, DateTime minimumDateTime, DateTime maximumDateTime, long ticks)
|
||||
{
|
||||
Bitmap bitmap;
|
||||
string checkFile;
|
||||
PropertyItem? propertyItem;
|
||||
string? ticksDirectory = null;
|
||||
Shared.Models.Property property;
|
||||
int dateTimeOriginal = (int)IExif.Tags.DateTimeOriginal;
|
||||
for (int i = 0; i < int.MaxValue; i++)
|
||||
{
|
||||
ticksDirectory = Path.Combine(sourceDirectory, ticks.ToString());
|
||||
if (!Directory.Exists(ticksDirectory))
|
||||
{
|
||||
_ = Directory.CreateDirectory(ticksDirectory);
|
||||
break;
|
||||
}
|
||||
ticks++;
|
||||
|
||||
}
|
||||
List<(string, int, DateTime)> collection = GetCollection(checkDirectory, minimumDateTime, maximumDateTime, ticks);
|
||||
ConstructorInfo? constructorInfo = typeof(PropertyItem).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, Array.Empty<Type>(), null) ?? throw new Exception();
|
||||
foreach ((string file, int id, DateTime dateTime) in collection)
|
||||
{
|
||||
if (ticksDirectory is null)
|
||||
throw new Exception();
|
||||
checkFile = Path.Combine(ticksDirectory, Path.GetFileName(file));
|
||||
if (File.Exists(checkFile))
|
||||
continue;
|
||||
propertyItem = GetPropertyItem(constructorInfo, dateTimeOriginal, dateTime.ToString("yyyy:MM:dd HH:mm:ss"));
|
||||
bitmap = new(file);
|
||||
bitmap.SetPropertyItem(propertyItem);
|
||||
bitmap.Save(checkFile);
|
||||
bitmap.Dispose();
|
||||
property = Property.Models.A_Property.GetImageProperty(checkFile);
|
||||
if (property.Id is null || property.Id.Value != id)
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
|
||||
private void DateFix(string sourceDirectory)
|
||||
{
|
||||
string checkDirectory;
|
||||
long oneYearTicks = DateTime.MinValue.AddYears(1).Ticks;
|
||||
checkDirectory = Path.Combine(sourceDirectory, oneYearTicks.ToString());
|
||||
if (Directory.Exists(checkDirectory))
|
||||
DateFix(sourceDirectory, checkDirectory, DateTime.MinValue, DateTime.MaxValue, new TimeSpan(oneYearTicks - DateTime.MinValue.Ticks).Ticks);
|
||||
else
|
||||
{
|
||||
checkDirectory = Path.Combine(sourceDirectory, "1");
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_Logger.Error($"<{checkDirectory}> doesn't exist!");
|
||||
else
|
||||
{
|
||||
string badDirectory = Path.Combine(sourceDirectory, "Bad");
|
||||
string targetDirectory = Path.Combine(sourceDirectory, "Target");
|
||||
string[] minimumDirectory = Directory.GetDirectories(targetDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
if (minimumDirectory.Length != 1)
|
||||
_Logger.Error($"<{checkDirectory}> doesn't exist!");
|
||||
else
|
||||
{
|
||||
string format = "yyyy-MM-dd";
|
||||
string[] maximumDirectory = Directory.GetDirectories(minimumDirectory.First(), "*", SearchOption.TopDirectoryOnly);
|
||||
string[] badFiles = !Directory.Exists(badDirectory) ? Array.Empty<string>() : Directory.GetFiles(badDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
string[] targetFiles = !Directory.Exists(targetDirectory) ? Array.Empty<string>() : Directory.GetFiles(targetDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
if (badFiles.Length != 1 || targetFiles.Length != 1 || maximumDirectory.Length != 1)
|
||||
_Logger.Error("bad file(s) or target file(s) or maximum directory doesn't equal 1!");
|
||||
else
|
||||
{
|
||||
DateTime minimumDateTime = DateTime.ParseExact(Path.GetFileName(minimumDirectory.First()), format, null, System.Globalization.DateTimeStyles.None);
|
||||
DateTime maximumDateTime = DateTime.ParseExact(Path.GetFileName(maximumDirectory.First()), format, null, System.Globalization.DateTimeStyles.None).AddHours(23);
|
||||
Shared.Models.Property badProperty = Property.Models.A_Property.GetImageProperty(badFiles.First());
|
||||
Shared.Models.Property targetProperty = Property.Models.A_Property.GetImageProperty(targetFiles.First());
|
||||
if (badProperty.DateTimeOriginal is null || targetProperty.DateTimeOriginal is null)
|
||||
_Logger.Error("Date is null!");
|
||||
else
|
||||
{
|
||||
TimeSpan timeSpan = new(targetProperty.DateTimeOriginal.Value.Ticks - badProperty.DateTimeOriginal.Value.Ticks);
|
||||
DateFix(sourceDirectory, checkDirectory, minimumDateTime, maximumDateTime, timeSpan.Ticks);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Form1_DragDrop(object? sender, DragEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (e.Data is not null && e.Data.GetData(DataFormats.FileDrop) is string[] paths && paths.Any())
|
||||
MovePaths(paths);
|
||||
{
|
||||
if (paths.Length == 1 && Directory.Exists(paths.First()))
|
||||
DateFix(paths.First());
|
||||
else
|
||||
MovePaths(paths);
|
||||
}
|
||||
else
|
||||
{
|
||||
_FirstTextBox.Text = string.Empty;
|
||||
@ -191,5 +328,4 @@ public partial class DragDropMove : Form
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user