public void CleanupUnusedImageFiles()
{
//Collect up all the image files in our book's directory
var imageFiles = new List<string>();
var imageExtentions = new HashSet<string>(new []{ ".jpg", ".png", ".svg" });
var ignoredFilenameStarts = new HashSet<string>(new [] { "thumbnail", "placeholder", "license" });
foreach (var path in Directory.EnumerateFiles(this._folderPath).Where(
s => imageExtentions.Contains(Path.GetExtension(s).ToLowerInvariant())))
{
var filename = Path.GetFileName(path);
if (ignoredFilenameStarts.Any(s=>filename.StartsWith(s, StringComparison.InvariantCultureIgnoreCase)))
continue;
imageFiles.Add(Path.GetFileName(GetNormalizedPathForOS(path)));
}
//Remove from that list each image actually in use
var element = Dom.RawDom.DocumentElement;
var toRemove = GetImagePathsRelativeToBook(element);
//also, remove from the doomed list anything referenced in the datadiv that looks like an image
//This saves us from deleting, for example, cover page images if this is called before the front-matter
//has been applied to the document.
toRemove.AddRange(from XmlElement dataDivImage in Dom.RawDom.SelectNodes("//div[@id='bloomDataDiv']//div[contains(text(),'.png') or contains(text(),'.jpg') or contains(text(),'.svg')]")
select UrlPathString.CreateFromUrlEncodedString(dataDivImage.InnerText.Trim()).PathOnly.NotEncoded);
foreach (var fileName in toRemove)
{
imageFiles.Remove(GetNormalizedPathForOS(fileName)); //Remove just returns false if it's not in there, which is fine
}
//Delete any files still in the list
foreach (var fileName in imageFiles)
{
var path = Path.Combine(_folderPath, fileName);
try
{
Debug.WriteLine("Removed unused image: "+path);
Logger.WriteEvent("Removed unused image: " + path);
RobustFile.Delete(path);
}
catch (Exception)
{
Debug.WriteLine("Could not remove unused image: " + path);
Logger.WriteEvent("Could not remove unused image: " + path);
//It's not worth bothering the user about, we'll get it someday.
//We're not even doing a Debug.Fail because that makes it harder to unit test this condition.
}
}
}