#region ========================================================================= USING ===================================================================================== using Nemera.Plugins.Calibre.Common.Models.DTO.Opf; using System; using System.Globalization; using System.IO; using System.Linq; using System.Xml; using System.Xml.Linq; #endregion namespace Nemera.Plugins.Calibre.Core.Opf; /// /// Reads the metadata of a book from a Calibre OPF file. /// internal static class OpfReader { private const long MAX_OPF_FILE_SIZE_BYTES = 5 * 1024 * 1024; private static readonly XNamespace s_opfNamespace = "http://www.idpf.org/2007/opf"; private static readonly XNamespace s_dcNamespace = "http://purl.org/dc/elements/1.1/"; /// /// Reads the metadata of the book from the OPF file at . /// /// The file system path of the OPF file to read. /// The metadata read from the OPF file, with the fields that could not be read left empty. public static OpfDocumentDto Read(string opfFilePath) { OpfDocumentDto document = new(); FileInfo fileInfo = new(opfFilePath); if (!fileInfo.Exists || fileInfo.Length > MAX_OPF_FILE_SIZE_BYTES) return document; try { using FileStream fileStream = File.OpenRead(opfFilePath); // The OPF file is untrusted XML, so DTD processing and external entity resolution are disabled, and the document size is bounded. XmlReaderSettings xmlReaderSettings = new() { DtdProcessing = DtdProcessing.Prohibit, XmlResolver = null, MaxCharactersInDocument = MAX_OPF_FILE_SIZE_BYTES, MaxCharactersFromEntities = 0 }; using XmlReader xmlReader = XmlReader.Create(fileStream, xmlReaderSettings); XDocument xDocument = XDocument.Load(xmlReader, LoadOptions.None); Parse(xDocument, document); } catch (XmlException) { // A malformed OPF file must not prevent the enrichment of the book from proceeding. } return document; } /// /// Parses the metadata elements of the into the . /// /// The parsed OPF document. /// The model onto which the parsed metadata is written. private static void Parse(XDocument xDocument, OpfDocumentDto document) { XElement? metadata = xDocument.Root?.Element(s_opfNamespace + "metadata"); if (metadata is null) return; document.Title = GetElementText(metadata, s_dcNamespace + "title"); document.Description = GetElementText(metadata, s_dcNamespace + "description"); document.Publisher = GetElementText(metadata, s_dcNamespace + "publisher"); document.LanguageCode = GetElementText(metadata, s_dcNamespace + "language"); document.PublishDate = ParseDate(GetElementText(metadata, s_dcNamespace + "date")); foreach (XElement identifier in metadata.Elements(s_dcNamespace + "identifier")) { string? scheme = identifier.Attribute(s_opfNamespace + "scheme")?.Value; if (!string.IsNullOrWhiteSpace(scheme) && !string.IsNullOrWhiteSpace(identifier.Value)) document.Identifiers.Add(new OpfIdentifierDto(Scheme: scheme.Trim(), Value: identifier.Value.Trim())); } foreach (XElement subject in metadata.Elements(s_dcNamespace + "subject")) if (!string.IsNullOrWhiteSpace(subject.Value)) document.Subjects.Add(subject.Value.Trim()); foreach (XElement creator in metadata.Elements(s_dcNamespace + "creator")) if (!string.IsNullOrWhiteSpace(creator.Value)) document.Creators.Add(new OpfCreatorDto(Name: creator.Value.Trim(), Role: creator.Attribute(s_opfNamespace + "role")?.Value?.Trim())); foreach (XElement contributor in metadata.Elements(s_dcNamespace + "contributor")) if (!string.IsNullOrWhiteSpace(contributor.Value)) document.Contributors.Add(new OpfContributorDto(Name: contributor.Value.Trim(), Role: contributor.Attribute(s_opfNamespace + "role")?.Value?.Trim())); foreach (XElement meta in metadata.Elements(s_opfNamespace + "meta")) { string? name = meta.Attribute("name")?.Value; string? content = meta.Attribute("content")?.Value; if (name is null || content is null) continue; if (string.Equals(name, "calibre:series", StringComparison.OrdinalIgnoreCase)) document.Series = content.Trim(); else if (string.Equals(name, "calibre:series_index", StringComparison.OrdinalIgnoreCase)) document.SeriesIndex = ParseDouble(content); else if (string.Equals(name, "calibre:rating", StringComparison.OrdinalIgnoreCase)) document.Rating = ParseInt(content); } XElement? coverReference = xDocument.Root ?.Element(s_opfNamespace + "guide") ?.Elements(s_opfNamespace + "reference") .FirstOrDefault(reference => string.Equals(reference.Attribute("type")?.Value, "cover", StringComparison.OrdinalIgnoreCase)); document.CoverHref = coverReference?.Attribute("href")?.Value; } /// /// Gets the trimmed text of the child element with the specified of the element. /// /// The parent element. /// The name of the child element. /// The trimmed text, or when the element is missing or empty. private static string? GetElementText(XElement parent, XName name) { string? value = parent.Element(name)?.Value; return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); } /// /// Parses a date string into a . /// /// The date string to parse. /// The parsed date, or when the string is empty or cannot be parsed. private static DateTimeOffset? ParseDate(string? value) { if (string.IsNullOrWhiteSpace(value)) return null; return DateTimeOffset.TryParse(value.Trim(), CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out DateTimeOffset result) ? result : null; } /// /// Parses a double string. /// /// The string to parse. /// The parsed value, or when the string is empty or cannot be parsed. private static double? ParseDouble(string value) { return double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out double result) ? result : null; } /// /// Parses an integer string. /// /// The string to parse. /// The parsed value, or when the string is empty or cannot be parsed. private static int? ParseInt(string value) { return int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int result) ? result : null; } }