diff --git a/src/BingImageDownload/BingInteractionAndParsing.cs b/src/BingImageDownload/BingInteractionAndParsing.cs index ebb3c34..106a017 100644 --- a/src/BingImageDownload/BingInteractionAndParsing.cs +++ b/src/BingImageDownload/BingInteractionAndParsing.cs @@ -119,7 +119,6 @@ internal bool DownloadAndSaveImage(XElement imageNode, string imageUrl) return false; } - var filePath = Path.Combine(paths.SavePath, GetFileName(imageUrl)); var tempFilename = Path.Combine(paths.DownloadPath, Guid.NewGuid() + ".jpg"); try @@ -135,8 +134,18 @@ internal bool DownloadAndSaveImage(XElement imageNode, string imageUrl) consoleWriter.WriteLine(2, "Downloaded Image, Checking If Duplicate"); var newImage = false; - if (!imageHashing.ImageInHash(tempFilename, filePath)) + var haveIdenticalImage = imageHashing.HaveIdenticalImageInHashTable(tempFilename); + + if (!haveIdenticalImage) { + var filePath = Path.Combine(paths.SavePath, GetFileName(imageUrl)); + var counter = 0; + while (imageHashing.HaveFileNameInHashTable(filePath)) + { + counter++; + filePath = Path.Combine(paths.SavePath, GetFileName(imageUrl, counter)); + } + newImage = true; consoleWriter.WriteLine(3, "Found New Image"); using (var srcImg = Image.Load(tempFilename)) @@ -157,7 +166,7 @@ internal bool DownloadAndSaveImage(XElement imageNode, string imageUrl) return newImage; } - internal string GetFileName(string imageUrl) + internal string GetFileName(string imageUrl, int counter = 0) { var name = imageUrl.Substring(7 + Url.Length); if (name.Contains("_")) @@ -170,7 +179,7 @@ internal string GetFileName(string imageUrl) name = name.Substring(name.IndexOf(".", StringComparison.Ordinal) + 1); } - name = $"{name}.jpg"; + name = counter > 0 ? $"{name} ({counter}).jpg" : $"{name}.jpg"; return Path.GetInvalidFileNameChars().Aggregate(name, (current, invalidChar) => current.Replace(invalidChar, '-')); } diff --git a/src/BingImageDownload/ImageHashing.cs b/src/BingImageDownload/ImageHashing.cs index a95bf5c..167080c 100644 --- a/src/BingImageDownload/ImageHashing.cs +++ b/src/BingImageDownload/ImageHashing.cs @@ -41,15 +41,13 @@ internal ImageHashing(ConsoleWriter consoleWriter, Paths paths, Serializer seria SaveHashTableBin(); } - internal bool ImageInHash(string tempFilename, string realFileName) + internal bool HaveIdenticalImageInHashTable(string tempFilename) { - if (HaveFilePathInHashTable(realFileName)) return true; - var testHash = GetRgbHistogramHash(tempFilename); return histogramHashTable.Any(hash => hash.Equal(testHash)); } - internal bool HaveFilePathInHashTable(string filePath) + internal bool HaveFileNameInHashTable(string filePath) { var fileName = Path.GetFileName(filePath); return histogramHashTable.Any(x => x.FileName.Equals(fileName, StringComparison.InvariantCultureIgnoreCase)); @@ -57,7 +55,7 @@ internal bool HaveFilePathInHashTable(string filePath) internal void AddHash(string filePath, bool saveHashTable = true) { - if (HaveFilePathInHashTable(filePath)) return; + if (HaveFileNameInHashTable(filePath)) return; histogramHashTable.Add(GetRgbHistogramHash(filePath)); @@ -83,13 +81,13 @@ private void HashExistingImages(int retryCount = 0) try { - foreach (var file in Directory.GetFiles(paths.SavePath, "*.jpg").Where(x => !HaveFilePathInHashTable(x))) + foreach (var file in Directory.GetFiles(paths.SavePath, "*.jpg").Where(x => !HaveFileNameInHashTable(x))) { consoleWriter.WriteLine($"Hashing file: {file}"); AddHash(file, false); } - foreach (var file in Directory.GetFiles(paths.ArchivePath, "*.jpg").Where(x => !HaveFilePathInHashTable(x))) + foreach (var file in Directory.GetFiles(paths.ArchivePath, "*.jpg").Where(x => !HaveFileNameInHashTable(x))) { consoleWriter.WriteLine($"Hashing file: {file}"); AddHash(file, false);