public static void UpdateImgMetadataAttributesToMatchImage(string folderPath, XmlElement imgElement, IProgress progress, Metadata metadata)
{
//see also PageEditingModel.UpdateMetadataAttributesOnImage(), which does the same thing but on the browser dom
var url = HtmlDom.GetImageElementUrl(new ElementProxy(imgElement));
string fileName = url.PathOnly.NotEncoded;
if (fileName.ToLowerInvariant() == "placeholder.png" || fileName.ToLowerInvariant() == "license.png")
{
return;
}
if (string.IsNullOrEmpty(fileName))
{
Logger.WriteEvent("Book.UpdateImgMetdataAttributesToMatchImage() Warning: img has no or empty src attribute");
//Debug.Fail(" (Debug only) img has no or empty src attribute");
return; // they have bigger problems, which aren't appropriate to deal with here.
}
if (metadata == null)
{
// The fileName might be URL encoded. See https://silbloom.myjetbrains.com/youtrack/issue/BL-3901.
var path = UrlPathString.GetFullyDecodedPath(folderPath, ref fileName);
progress.WriteStatus("Reading metadata from " + fileName);
if (!RobustFile.Exists(path)) // they have bigger problems, which aren't appropriate to deal with here.
{
imgElement.RemoveAttribute("data-copyright");
imgElement.RemoveAttribute("data-creator");
imgElement.RemoveAttribute("data-license");
Logger.WriteEvent("Book.UpdateImgMetdataAttributesToMatchImage() Image " + path + " is missing");
//Debug.Fail(" (Debug only) Image " + path + " is missing");
return;
}
try
{
metadata = RobustIO.MetadataFromFile(path);
}
catch (UnauthorizedAccessException e)
{
throw new BloomUnauthorizedAccessException(path, e);
}
}
progress.WriteStatus("Writing metadata to HTML for " + fileName);
imgElement.SetAttribute("data-copyright",
String.IsNullOrEmpty(metadata.CopyrightNotice) ? "" : metadata.CopyrightNotice);
imgElement.SetAttribute("data-creator", String.IsNullOrEmpty(metadata.Creator) ? "" : metadata.Creator);
imgElement.SetAttribute("data-license", metadata.License == null ? "" : metadata.License.ToString());
}