Skip to content

Commit

Permalink
Handle 2 different images with same name
Browse files Browse the repository at this point in the history
  • Loading branch information
BlythMeister committed Jul 22, 2020
1 parent 71fcc97 commit b47475c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
17 changes: 13 additions & 4 deletions src/BingImageDownload/BingInteractionAndParsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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("_"))
Expand All @@ -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, '-'));
}
Expand Down
12 changes: 5 additions & 7 deletions src/BingImageDownload/ImageHashing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,21 @@ 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));
}

internal void AddHash(string filePath, bool saveHashTable = true)
{
if (HaveFilePathInHashTable(filePath)) return;
if (HaveFileNameInHashTable(filePath)) return;

histogramHashTable.Add(GetRgbHistogramHash(filePath));

Expand All @@ -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);
Expand Down

0 comments on commit b47475c

Please sign in to comment.