828 lines
33 KiB
C#
828 lines
33 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using System.Web.Script.Serialization;
|
|
|
|
namespace Mk0.Software.Bildduplikate
|
|
{
|
|
public partial class Main : Form
|
|
{
|
|
static readonly string[] suffixes = { "Bytes", "KB", "MB", "GB", "TB", "PB" };
|
|
private int line = 0, total = 0, offset = 0;
|
|
private string file;
|
|
private bool api = false;
|
|
private int worst = 0;
|
|
private int lastStep = 0;
|
|
|
|
private List<Duplicate> duplicates = new List<Duplicate>();
|
|
|
|
internal List<Duplicate> Duplicates { get => duplicates; set => duplicates = value; }
|
|
|
|
public Main()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Select CSV with possible duplicates
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void ButtonCSV_Click(object sender, EventArgs e)
|
|
{
|
|
openFileDialog1.Title = "Select CSV";
|
|
openFileDialog1.DefaultExt = "csv";
|
|
openFileDialog1.Filter = "CSV (*.csv)|*.csv";
|
|
openFileDialog1.FilterIndex = 2;
|
|
openFileDialog1.CheckFileExists = true;
|
|
openFileDialog1.CheckPathExists = true;
|
|
|
|
if (openFileDialog1.ShowDialog() == DialogResult.OK)
|
|
{
|
|
LoadCSV(openFileDialog1.FileName);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load possible duplicates from CSV
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
private void LoadCSV(string fileName)
|
|
{
|
|
line = 0;
|
|
total = 0;
|
|
file = fileName;
|
|
offset = 0;
|
|
Duplicates.Clear();
|
|
|
|
using (StreamReader reader = new StreamReader(fileName))
|
|
{
|
|
while (!reader.EndOfStream)
|
|
{
|
|
var line = reader.ReadLine();
|
|
var values = line.Split(',');
|
|
|
|
Duplicate d = new Duplicate(values[0], values[1]);
|
|
Duplicates.Add(d);
|
|
}
|
|
}
|
|
|
|
total = Duplicates.Count;
|
|
api = false;
|
|
|
|
LoadDuplicates(line);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Process for manual duplicate processing
|
|
/// </summary>
|
|
/// <param name="lineDup"></param>
|
|
/// <param name="api"></param>
|
|
private void LoadDuplicates(int lineDup, bool api = false)
|
|
{
|
|
if (Duplicates.Count > lineDup)
|
|
{
|
|
if (File.Exists(Duplicates.ElementAt(lineDup).Path1) && File.Exists(Duplicates.ElementAt(lineDup).Path2))
|
|
{
|
|
Image img1 = GetCopyImage(Duplicates.ElementAt(lineDup).Path1);
|
|
Image img2 = GetCopyImage(Duplicates.ElementAt(lineDup).Path2);
|
|
pictureBox1.Image = img1;
|
|
pictureBox2.Image = img2;
|
|
FileInfo f1 = new FileInfo(Duplicates.ElementAt(lineDup).Path1);
|
|
FileInfo f2 = new FileInfo(Duplicates.ElementAt(lineDup).Path2);
|
|
int dup1 = 0, dup2 = 0;
|
|
|
|
string size1, size2;
|
|
if (f1.Length == f2.Length)
|
|
{
|
|
size1 = @" \b " + FormatSize(f1.Length) + @" \b0 ";
|
|
size2 = @" \b " + FormatSize(f2.Length) + @" \b0 ";
|
|
dup1++;
|
|
dup1++;
|
|
dup2++;
|
|
dup2++;
|
|
}
|
|
else if (f1.Length > f2.Length)
|
|
{
|
|
size1 = @" \b " + FormatSize(f1.Length) + @" \b0 ";
|
|
size2 = FormatSize(f2.Length);
|
|
dup1++;
|
|
dup1++;
|
|
}
|
|
else
|
|
{
|
|
size1 = FormatSize(f1.Length);
|
|
size2 = @" \b " + FormatSize(f2.Length) + @" \b0 ";
|
|
dup2++;
|
|
dup2++;
|
|
}
|
|
|
|
string filePath1 = GetRtfUnicodeEscapedString(Path.GetDirectoryName(Duplicates.ElementAt(lineDup).Path1));
|
|
string filePath2 = GetRtfUnicodeEscapedString(Path.GetDirectoryName(Duplicates.ElementAt(lineDup).Path2));
|
|
if (filePath1 == filePath2)
|
|
{
|
|
filePath1 = @" \b " + filePath1 + @" \b0 ";
|
|
filePath2 = @" \b " + filePath2 + @" \b0 ";
|
|
dup1++;
|
|
dup2++;
|
|
}
|
|
|
|
string fileName1 = Path.GetFileName(Duplicates.ElementAt(lineDup).Path1);
|
|
string fileName2 = Path.GetFileName(Duplicates.ElementAt(lineDup).Path2);
|
|
if (fileName1 == fileName2)
|
|
{
|
|
fileName1 = @" \b " + GetRtfUnicodeEscapedString(fileName1) + @" \b0 ";
|
|
fileName2 = @" \b " + GetRtfUnicodeEscapedString(fileName2) + @" \b0 ";
|
|
dup1++;
|
|
dup2++;
|
|
}
|
|
else
|
|
{
|
|
if (fileName1.Count(Char.IsDigit) < fileName2.Count(Char.IsDigit))
|
|
{
|
|
fileName1 = @" \b " + GetRtfUnicodeEscapedString(fileName1) + @" \b0 ";
|
|
fileName2 = GetRtfUnicodeEscapedString(fileName2);
|
|
dup1++;
|
|
}
|
|
else if (fileName2.Count(Char.IsDigit) < fileName1.Count(Char.IsDigit))
|
|
{
|
|
fileName1 = GetRtfUnicodeEscapedString(fileName1);
|
|
fileName2 = @" \b " + GetRtfUnicodeEscapedString(fileName2) + @" \b0 ";
|
|
dup2++;
|
|
}
|
|
else
|
|
{
|
|
if (fileName1.Length > fileName2.Length)
|
|
{
|
|
fileName1 = GetRtfUnicodeEscapedString(fileName1);
|
|
fileName2 = @" \b " + GetRtfUnicodeEscapedString(fileName2) + @" \b0 ";
|
|
dup2++;
|
|
}
|
|
else if (fileName2.Length > fileName1.Length)
|
|
{
|
|
fileName1 = @" \b " + GetRtfUnicodeEscapedString(fileName1) + @" \b0 ";
|
|
fileName2 = GetRtfUnicodeEscapedString(fileName2);
|
|
dup1++;
|
|
}
|
|
else
|
|
{
|
|
}
|
|
}
|
|
}
|
|
|
|
DateTime lastTime1 = f1.LastWriteTime;
|
|
DateTime lastTime2 = f2.LastWriteTime;
|
|
string lastTime1str, lastTime2str;
|
|
if (lastTime1 == lastTime2)
|
|
{
|
|
lastTime1str = @" \b " + lastTime1.ToString() + @" \b0 ";
|
|
lastTime2str = @" \b " + lastTime2.ToString() + @" \b0 ";
|
|
dup1++;
|
|
dup2++;
|
|
}
|
|
else if (lastTime1 > lastTime2)
|
|
{
|
|
lastTime1str = @" \b " + lastTime1.ToString() + @" \b0 ";
|
|
lastTime2str = lastTime2.ToString();
|
|
dup1++;
|
|
}
|
|
else
|
|
{
|
|
lastTime1str = lastTime1.ToString();
|
|
lastTime2str = @" \b " + lastTime2.ToString() + @" \b0 ";
|
|
dup2++;
|
|
}
|
|
|
|
float res1 = img1.HorizontalResolution;
|
|
float res2 = img2.HorizontalResolution;
|
|
string res1str, res2str;
|
|
if (res1 == res2)
|
|
{
|
|
if (img1.Height == img2.Height && img1.Width == img2.Width)
|
|
{
|
|
res1str = @" \b " + img1.Width + "x" + img1.Height + " @ " + res1 + " DPI" + @" \b0 ";
|
|
res2str = @" \b " + img2.Width + "x" + img2.Height + " @ " + res2 + " DPI" + @" \b0 ";
|
|
dup1++;
|
|
dup1++;
|
|
dup2++;
|
|
dup2++;
|
|
}
|
|
else if (img1.Height > img2.Height && img1.Width > img2.Width)
|
|
{
|
|
res1str = @" \b " + img1.Width + "x" + img1.Height + " @ " + res1 + " DPI" + @" \b0 ";
|
|
res2str = img2.Width + "x" + img2.Height + " @ " + res2 + " DPI";
|
|
dup1++;
|
|
dup1++;
|
|
}
|
|
else if (img2.Height > img1.Height && img2.Width > img1.Width)
|
|
{
|
|
res1str = img1.Width + "x" + img1.Height + " @ " + res1 + " DPI";
|
|
res2str = @" \b " + img2.Width + "x" + img2.Height + " @ " + res2 + " DPI" + @" \b0 ";
|
|
dup2++;
|
|
dup2++;
|
|
}
|
|
else
|
|
{
|
|
res1str = img1.Width + "x" + img1.Height + " @ " + res1 + " DPI";
|
|
res2str = img2.Width + "x" + img2.Height + " @ " + res2 + " DPI";
|
|
}
|
|
}
|
|
else if (res1 > res2)
|
|
{
|
|
res1str = @" \b " + img1.Width + "x" + img1.Height + " @ " + res1 + " DPI" + @" \b0 ";
|
|
res2str = img2.Width + "x" + img2.Height + " @ " + res2 + " DPI";
|
|
dup1++;
|
|
dup1++;
|
|
}
|
|
else
|
|
{
|
|
res1str = img1.Width + "x" + img1.Height + " @ " + res1 + " DPI";
|
|
res2str = @" \b " + img2.Width + "x" + img2.Height + " @ " + res2 + " DPI" + @" \b0 ";
|
|
dup2++;
|
|
dup2++;
|
|
}
|
|
|
|
richTextBoxDesc1.Rtf = @"{\rtf1\ansi " + filePath1 + @" \line " + fileName1 + @" \line " + size1 + @" \line " + lastTime1str + @" \line " + res1str + " }";
|
|
richTextBoxDesc2.Rtf = @"{\rtf1\ansi " + filePath2 + @" \line " + fileName2 + @" \line " + size2 + @" \line " + lastTime2str + @" \line " + res2str + " }";
|
|
richTextBoxDesc1.SelectAll();
|
|
richTextBoxDesc1.SelectionAlignment = HorizontalAlignment.Right;
|
|
label3.Text = total + " Duplikate";
|
|
ButtonHighlight(dup1, dup2);
|
|
UIElementsVisible(true);
|
|
}
|
|
else
|
|
{
|
|
if (api)
|
|
{
|
|
try
|
|
{
|
|
WebClient webClient = new WebClient();
|
|
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
|
|
string json = webClient.UploadString(Properties.Settings.Default.ApiUrl + "?isDuplicate", "{\"duplicateId\":" + Duplicates.ElementAt(lineDup).ID + "}");
|
|
var resObj = new JavaScriptSerializer().Deserialize<APIResponse>(json);
|
|
if (resObj.response_code != "200") { MessageBox.Show(resObj.response_desc, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); }
|
|
else
|
|
{
|
|
total--;
|
|
line++;
|
|
LoadDuplicates(line, api);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Fehler bei der Kommunikation mit der API:" + Environment.NewLine + ex.ToString(), "API Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
File_DeleteLine(lineDup);
|
|
total--;
|
|
line++;
|
|
LoadDuplicates(line, api);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (api)
|
|
{
|
|
ButtonAPILoad_Click(null, null);
|
|
}
|
|
else
|
|
{
|
|
UIElementsVisible(false);
|
|
MessageBox.Show("Alle Duplikate in CSV abgearbeitet", "Fertig!", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Highlight Button which is possibly the worst duplicate
|
|
/// </summary>
|
|
/// <param name="dup1"></param>
|
|
/// <param name="dup2"></param>
|
|
private void ButtonHighlight(int dup1, int dup2)
|
|
{
|
|
if (dup1 < dup2)
|
|
{
|
|
buttonDel1.Font = new Font(buttonDel1.Font.Name, buttonDel1.Font.Size, FontStyle.Bold);
|
|
buttonDel2.Font = new Font(buttonDel2.Font.Name, buttonDel2.Font.Size, FontStyle.Regular);
|
|
worst = 1;
|
|
}
|
|
else if (dup2 < dup1)
|
|
{
|
|
buttonDel1.Font = new Font(buttonDel1.Font.Name, buttonDel1.Font.Size, FontStyle.Regular);
|
|
buttonDel2.Font = new Font(buttonDel2.Font.Name, buttonDel2.Font.Size, FontStyle.Bold);
|
|
worst = 2;
|
|
}
|
|
else
|
|
{
|
|
buttonDel1.Font = new Font(buttonDel1.Font.Name, buttonDel1.Font.Size, FontStyle.Regular);
|
|
buttonDel2.Font = new Font(buttonDel2.Font.Name, buttonDel2.Font.Size, FontStyle.Regular);
|
|
worst = 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper for RTF Unicode
|
|
/// </summary>
|
|
/// <param name="s"></param>
|
|
/// <returns></returns>
|
|
private static string GetRtfUnicodeEscapedString(string s)
|
|
{
|
|
var sb = new StringBuilder();
|
|
foreach (var c in s)
|
|
{
|
|
if (c == '\\' || c == '{' || c == '}')
|
|
sb.Append(@"\" + c);
|
|
else if (c <= 0x7f)
|
|
sb.Append(c);
|
|
else
|
|
sb.Append("\\u" + Convert.ToUInt32(c) + "?");
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// UI-Elements shown or not shown
|
|
/// </summary>
|
|
/// <param name="vis"></param>
|
|
private void UIElementsVisible(bool vis)
|
|
{
|
|
richTextBoxDesc1.Visible = vis;
|
|
richTextBoxDesc2.Visible = vis;
|
|
label3.Visible = vis;
|
|
buttonDel1.Visible = vis;
|
|
buttonDel2.Visible = vis;
|
|
buttonKeinDuplikat.Visible = vis;
|
|
pictureBox1.Visible = vis;
|
|
pictureBox2.Visible = vis;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format string of file size
|
|
/// </summary>
|
|
/// <param name="length"></param>
|
|
/// <returns></returns>
|
|
private string FormatSize(long length)
|
|
{
|
|
int counter = 0;
|
|
decimal number = (decimal)length;
|
|
while (Math.Round(number / 1024) >= 1)
|
|
{
|
|
number /= 1024;
|
|
counter++;
|
|
}
|
|
return string.Format("{0:n1}{1}", number, suffixes[counter]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Mark images as non-duplicates
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void ButtonKeinDuplikat_Click(object sender, EventArgs e)
|
|
{
|
|
if (api)
|
|
{
|
|
try
|
|
{
|
|
WebClient webClient = new WebClient();
|
|
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
|
|
string json = webClient.UploadString(Properties.Settings.Default.ApiUrl + "?noDuplicate", "{\"duplicateId\":" + Duplicates.ElementAt(line).ID + "}");
|
|
var resObj = new JavaScriptSerializer().Deserialize<APIResponse>(json);
|
|
if (resObj.response_code != "200") { MessageBox.Show(resObj.response_desc, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); }
|
|
else
|
|
{
|
|
total--;
|
|
line++;
|
|
LoadDuplicates(line, api);
|
|
lastStep = 1;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Fehler bei der Kommunikation mit der API:" + Environment.NewLine + ex.ToString(), "API Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
buttonUndo.Visible = true;
|
|
}
|
|
else
|
|
{
|
|
File_DeleteLine(line);
|
|
total--;
|
|
line++;
|
|
LoadDuplicates(line, api);
|
|
lastStep = 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete duplicate file1
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void ButtonDel1_Click(object sender, EventArgs e)
|
|
{
|
|
if (api)
|
|
{
|
|
try
|
|
{
|
|
WebClient webClient = new WebClient();
|
|
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
|
|
string json = webClient.UploadString(Properties.Settings.Default.ApiUrl + "?isDuplicate", "{\"duplicateId\":" + Duplicates.ElementAt(line).ID + "}");
|
|
var resObj = new JavaScriptSerializer().Deserialize<APIResponse>(json);
|
|
if (resObj.response_code != "200") { MessageBox.Show(resObj.response_desc, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); }
|
|
else
|
|
{
|
|
File.Delete(Duplicates.ElementAt(line).Path1);
|
|
total--;
|
|
line++;
|
|
LoadDuplicates(line, api);
|
|
lastStep = 2;
|
|
}
|
|
//todo erst einkommentieren, wenn es die möglichkeit gibt, gelöschte wiederherzustellen
|
|
//buttonUndo.Visible = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Fehler bei der Kommunikation mit der API:" + Environment.NewLine + ex.ToString(), "API Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
File.Delete(Duplicates.ElementAt(line).Path1);
|
|
File_DeleteLine(line);
|
|
line++;
|
|
total--;
|
|
LoadDuplicates(line);
|
|
lastStep = 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete duplicate file2
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void ButtonDel2_Click(object sender, EventArgs e)
|
|
{
|
|
if (api)
|
|
{
|
|
try
|
|
{
|
|
WebClient webClient = new WebClient();
|
|
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
|
|
string json = webClient.UploadString(Properties.Settings.Default.ApiUrl + "?isDuplicate", "{\"duplicateId\":" + Duplicates.ElementAt(line).ID + "}");
|
|
var resObj = new JavaScriptSerializer().Deserialize<APIResponse>(json);
|
|
if (resObj.response_code != "200") { MessageBox.Show(resObj.response_desc, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); }
|
|
else
|
|
{
|
|
File.Delete(Duplicates.ElementAt(line).Path2);
|
|
total--;
|
|
line++;
|
|
LoadDuplicates(line, api);
|
|
lastStep = 2;
|
|
}
|
|
//todo erst einkommentieren, wenn es die möglichkeit gibt, gelöschte wiederherzustellen
|
|
//buttonUndo.Visible = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Fehler bei der Kommunikation mit der API:" + Environment.NewLine + ex.ToString(), "API Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
File.Delete(Duplicates.ElementAt(line).Path2);
|
|
File_DeleteLine(line);
|
|
line++;
|
|
total--;
|
|
LoadDuplicates(line);
|
|
lastStep = 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper for showing images and being able to delete them
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
private Image GetCopyImage(string path)
|
|
{
|
|
using (Image image = Image.FromFile(path))
|
|
{
|
|
Bitmap bitmap = new Bitmap(image);
|
|
return bitmap;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Keyboard control handling for duplicate processing
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void Form1_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.KeyCode == Keys.C)
|
|
{
|
|
ButtonCSV_Click(sender, e);
|
|
}
|
|
else if (e.KeyCode == Keys.D1 || e.KeyCode == Keys.NumPad1)
|
|
{
|
|
ButtonDel1_Click(sender, e);
|
|
}
|
|
else if (e.KeyCode == Keys.D2 || e.KeyCode == Keys.NumPad2)
|
|
{
|
|
ButtonDel2_Click(sender, e);
|
|
}
|
|
else if (e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.K)
|
|
{
|
|
ButtonKeinDuplikat_Click(sender, e);
|
|
}
|
|
else if (e.KeyCode == Keys.R)
|
|
{
|
|
ButtonReload_Click(sender, e);
|
|
}
|
|
else if (e.KeyCode == Keys.D)
|
|
{
|
|
DeleteWorstDuplicate();
|
|
}
|
|
else if (e.KeyCode == Keys.A)
|
|
{
|
|
ButtonAPI_Click(sender, e);
|
|
}
|
|
else if (e.KeyCode == Keys.C)
|
|
{
|
|
ButtonCSV_Click(sender, e);
|
|
}
|
|
else if (e.KeyCode == Keys.L)
|
|
{
|
|
ButtonAPILoad_Click(sender, e);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes the most probably worst duplicate
|
|
/// </summary>
|
|
private void DeleteWorstDuplicate()
|
|
{
|
|
if (worst == 1)
|
|
{
|
|
ButtonDel1_Click(null, null);
|
|
}
|
|
else if (worst == 2)
|
|
{
|
|
ButtonDel2_Click(null, null);
|
|
}
|
|
else
|
|
{
|
|
ButtonDel2_Click(null, null);
|
|
//MessageBox.Show("Keines der beiden Bilder konnte als besser oder schlechter erkannt werden, bitte wählen sie selbst!", "Ooops...", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Show API Settings Dialog
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void ButtonAPI_Click(object sender, EventArgs e)
|
|
{
|
|
API api = new API();
|
|
api.ShowDialog();
|
|
if (Properties.Settings.Default.ApiUrl != String.Empty)
|
|
{
|
|
buttonAPILoad.Visible = true;
|
|
if (Properties.Settings.Default.autoload)
|
|
{
|
|
ButtonAPILoad_Click(null, null);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
buttonAPILoad.Visible = false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads possible duplicates from API
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void ButtonAPILoad_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
WebClient webClient = new WebClient();
|
|
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
|
|
string json = webClient.UploadString(Properties.Settings.Default.ApiUrl + "?getDuplicates", "{\"amount\":" + Properties.Settings.Default.imagesFromAPI + "}");
|
|
var resObj = new JavaScriptSerializer().Deserialize<APIResponse>(json);
|
|
if (resObj.response_code != "200") { MessageBox.Show(resObj.response_desc, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); }
|
|
else
|
|
{
|
|
Duplicates.Clear();
|
|
line = 0;
|
|
total = 0;
|
|
file = "";
|
|
api = true;
|
|
offset = 0;
|
|
foreach (APIResponsePayload pl in resObj.payload)
|
|
{
|
|
Duplicate d = new Duplicate(ReplacePath(pl.file1), ReplacePath(pl.file2), pl.id);
|
|
Duplicates.Add(d);
|
|
}
|
|
|
|
WebClient webClient2 = new WebClient();
|
|
webClient2.Headers[HttpRequestHeader.ContentType] = "application/json";
|
|
string json2 = webClient.UploadString(Properties.Settings.Default.ApiUrl + "?totalDuplicates", "");
|
|
var resObj2 = new JavaScriptSerializer().Deserialize<APIResponse>(json2);
|
|
if (resObj2.response_code != "200") { MessageBox.Show(resObj2.response_desc, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); }
|
|
else
|
|
{
|
|
total = int.Parse(resObj2.totalDuplicates);
|
|
}
|
|
|
|
if (total == 0)
|
|
{
|
|
UIElementsVisible(false);
|
|
MessageBox.Show("Alle Duplikate von API abgearbeitet", "Fertig!", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
else
|
|
{
|
|
LoadDuplicates(line, true);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Fehler bei der Kommunikation mit der API:" + Environment.NewLine + ex.ToString(), "API Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Replaces API saved path with local (or network) path
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
private string ReplacePath(string file)
|
|
{
|
|
if (Properties.Settings.Default.ReplacePath == String.Empty)
|
|
{
|
|
return file;
|
|
}
|
|
return file.Replace(Properties.Settings.Default.ReplacePath, Properties.Settings.Default.ReplaceWith);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handling when Window is closing
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void Main_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
Properties.Settings.Default.lastWidth = Width;
|
|
Properties.Settings.Default.lastHeight = Height;
|
|
Properties.Settings.Default.lastTop = Top;
|
|
Properties.Settings.Default.lastLeft = Left;
|
|
Properties.Settings.Default.fullScreen = WindowState == FormWindowState.Maximized;
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Window handling when Window is Shown Event
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void Main_Shown(object sender, EventArgs e)
|
|
{
|
|
if (Properties.Settings.Default.fullScreen)
|
|
{
|
|
WindowState = FormWindowState.Maximized;
|
|
}
|
|
else
|
|
{
|
|
if (Properties.Settings.Default.lastHeight >= MinimumSize.Height)
|
|
{
|
|
Height = Properties.Settings.Default.lastHeight;
|
|
}
|
|
if (Properties.Settings.Default.lastWidth >= MinimumSize.Width)
|
|
{
|
|
Width = Properties.Settings.Default.lastWidth;
|
|
}
|
|
if (Properties.Settings.Default.lastTop <= Screen.PrimaryScreen.Bounds.Height && Properties.Settings.Default.lastTop >= 0)
|
|
{
|
|
Top = Properties.Settings.Default.lastTop;
|
|
}
|
|
if (Properties.Settings.Default.lastLeft <= Screen.PrimaryScreen.Bounds.Width && Properties.Settings.Default.lastLeft >= 0)
|
|
{
|
|
Left = Properties.Settings.Default.lastLeft;
|
|
}
|
|
}
|
|
|
|
if (Properties.Settings.Default.ApiUrl != String.Empty)
|
|
{
|
|
buttonAPILoad.Visible = true;
|
|
if (Properties.Settings.Default.autoload)
|
|
{
|
|
ButtonAPILoad_Click(null, null);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Undo last processing step
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void ButtonUndo_Click(object sender, EventArgs e)
|
|
{
|
|
if (lastStep == 1)
|
|
{
|
|
//undo mark as non-duplicate
|
|
try
|
|
{
|
|
WebClient webClient = new WebClient();
|
|
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
|
|
string json = webClient.UploadString(Properties.Settings.Default.ApiUrl + "?undoDuplicate", "{\"duplicateId\":" + Duplicates.ElementAt(line - 1).ID + "}");
|
|
var resObj = new JavaScriptSerializer().Deserialize<APIResponse>(json);
|
|
if (resObj.response_code != "200") { MessageBox.Show(resObj.response_desc, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); }
|
|
else
|
|
{
|
|
File.Delete(Duplicates.ElementAt(line).Path2);
|
|
total++;
|
|
line--;
|
|
LoadDuplicates(line, api);
|
|
lastStep = 1;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Fehler bei der Kommunikation mit der API:" + Environment.NewLine + ex.ToString(), "API Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
buttonUndo.Visible = false;
|
|
}
|
|
else if (lastStep == 2)
|
|
{
|
|
//undo delete duplicate
|
|
lastStep = 0;
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Keine Aktion die Rückgängig gemacht werden kann. Undo funktioniert grundsätzlich nur bei API Bildvergleichen, nicht bei CSV.", "Nix da", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
//lastStep = 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reloads CSV / API
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void ButtonReload_Click(object sender, EventArgs e)
|
|
{
|
|
LoadDuplicates(line);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete specified line in CSV file
|
|
/// </summary>
|
|
/// <param name="lineFile"></param>
|
|
private void File_DeleteLine(int lineFile)
|
|
{
|
|
int linetodelete = lineFile + 1 - offset;
|
|
offset++;
|
|
StringBuilder sb = new StringBuilder();
|
|
using (StreamReader sr = new StreamReader(file))
|
|
{
|
|
int Countup = 0;
|
|
while (!sr.EndOfStream)
|
|
{
|
|
Countup++;
|
|
if (Countup != linetodelete)
|
|
{
|
|
using (StringWriter sw = new StringWriter(sb))
|
|
{
|
|
sw.WriteLine(sr.ReadLine());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
sr.ReadLine();
|
|
}
|
|
}
|
|
}
|
|
using (StreamWriter sw = new StreamWriter(file))
|
|
{
|
|
sw.Write(sb.ToString());
|
|
}
|
|
Debug.Print("lösche Zeile " + linetodelete);
|
|
}
|
|
}
|
|
}
|