Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

Implemented ability to generate Series summary from ComicInfo.xml (if present)

Joseph Milazzo265f7dcc8c8a
diff --git a/API/Parser/Parser.cs b/API/Parser/Parser.cs
index cc2ac7b5e..828f7cc86 100644
--- a/API/Parser/Parser.cs
+++ b/API/Parser/Parser.cs
@@ -10,8 +10,10 @@ namespace API.Parser
{
public static readonly string MangaFileExtensions = @"\.cbz|\.zip"; // |\.rar|\.cbr
public static readonly string ImageFileExtensions = @"\.png|\.jpeg|\.jpg|\.gif";
+ private static readonly string XmlRegexExtensions = @"\.xml";
private static readonly Regex ImageRegex = new Regex(ImageFileExtensions, RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex MangaFileRegex = new Regex(MangaFileExtensions, RegexOptions.IgnoreCase | RegexOptions.Compiled);
+ private static readonly Regex XmlRegex = new Regex(XmlRegexExtensions, RegexOptions.IgnoreCase | RegexOptions.Compiled);
//?: is a non-capturing group in C#, else anything in () will be a group
private static readonly Regex[] MangaVolumeRegex = new[]
@@ -406,6 +408,12 @@ namespace API.Parser
return ImageRegex.IsMatch(fileInfo.Extension);
}
+ public static bool IsXml(string filePath)
+ {
+ var fileInfo = new FileInfo(filePath);
+ return XmlRegex.IsMatch(fileInfo.Extension);
+ }
+
public static float MinimumNumberFromRange(string range)
{
var tokens = range.Split("-");
@@ -416,5 +424,7 @@ namespace API.Parser
{
return name.ToLower().Replace("-", "").Replace(" ", "");
}
+
+
}
}
\ No newline at end of file
diff --git a/API/Services/ArchiveService.cs b/API/Services/ArchiveService.cs
index 716c0d91b..e7fd988b8 100644
--- a/API/Services/ArchiveService.cs
+++ b/API/Services/ArchiveService.cs
@@ -3,6 +3,9 @@ using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
+using System.Xml;
+using System.Xml.Linq;
+using System.Xml.Serialization;
using API.Extensions;
using API.Interfaces;
using API.Interfaces.Services;
@@ -146,7 +149,34 @@ namespace API.Services
public string GetSummaryInfo(string archivePath)
{
- throw new NotImplementedException();
+ var summary = "";
+ if (!IsValidArchive(archivePath)) return summary;
+
+ using var archive = ZipFile.OpenRead(archivePath);
+ if (!archive.HasFiles()) return summary;
+
+ var info = archive.Entries.SingleOrDefault(x => Path.GetFileNameWithoutExtension(x.Name).ToLower() == "comicinfo" && Parser.Parser.IsXml(x.FullName));
+ if (info == null) return summary;
+
+ // Parse XML file
+ try
+ {
+ using var stream = info.Open();
+ var serializer = new XmlSerializer(typeof(ComicInfo));
+ ComicInfo comicInfo =
+ (ComicInfo)serializer.Deserialize(stream);
+
+ if (comicInfo != null)
+ {
+ return comicInfo.Summary;
+ }
+ }
+ catch (AggregateException ex)
+ {
+ _logger.LogError(ex, "There was an issue parsing ComicInfo.xml from {ArchivePath}", archivePath);
+ }
+
+ return summary;
}
/// <summary>
diff --git a/API/Services/ComicInfo.cs b/API/Services/ComicInfo.cs
new file mode 100644
index 000000000..1f994d224
--- /dev/null
+++ b/API/Services/ComicInfo.cs
@@ -0,0 +1,15 @@
+namespace API.Services
+{
+ public class ComicInfo
+ {
+ public string Summary;
+ public string Title;
+ public string Series;
+ public string Notes;
+ public string Publisher;
+ public string Genre;
+ public int PageCount;
+ public string LanguageISO;
+ public string Web;
+ }
+}
\ No newline at end of file
diff --git a/API/Services/MetadataService.cs b/API/Services/MetadataService.cs
index 023c1decc..c762bdbf7 100644
--- a/API/Services/MetadataService.cs
+++ b/API/Services/MetadataService.cs
@@ -65,10 +65,14 @@ namespace API.Services
}
series.CoverImage = firstCover?.CoverImage;
}
+
+ if (!string.IsNullOrEmpty(series.Summary) && !forceUpdate) return;
- if (string.IsNullOrEmpty(series.Summary) || forceUpdate)
+ var firstVolume = series.Volumes.FirstOrDefault(v => v.Chapters.Any() && v.Number == 1);
+ var firstChapter = firstVolume?.Chapters.FirstOrDefault(c => c.Files.Any());
+ if (firstChapter != null)
{
- series.Summary = _archiveService.GetSummaryInfo(series.Volumes.First().Chapters.First().Files.First().FilePath);
+ series.Summary = _archiveService.GetSummaryInfo(firstChapter.Files.FirstOrDefault()?.FilePath);
}
}