Kavita/API/Extensions/SeriesExtensions.cs
Joe Milazzo 9149c4cbca
Release Polish (#1586)
* Fixed a scaling issue in the epub reader, where images could scale when they shouldn't.

* Removed some caching on library/ api and added more output for a foreign key constraint

* Hooked in Restricted Profile stat collection

* Added a new boolean on age restrictions to explicitly allow unknowns or not. Since unknown is the default state of metadata, if users are allowed access to Unknown, age restricted content could leak.

* Fixed a bug where sometimes series cover generation could fail under conditions where only specials existed.

* Fixed foreign constraint issue when cleaning up series not seen at end of scan loop

* Removed an additional epub parse when scanning and handled merging differently

* Code smell
2022-10-17 15:33:18 -07:00

72 lines
3.6 KiB
C#

using System.Collections.Generic;
using System.Linq;
using API.Comparators;
using API.Entities;
using API.Parser;
using API.Services.Tasks.Scanner;
namespace API.Extensions;
public static class SeriesExtensions
{
/// <summary>
/// Checks against all the name variables of the Series if it matches anything in the list. This does not check against format.
/// </summary>
/// <param name="series"></param>
/// <param name="list"></param>
/// <returns></returns>
public static bool NameInList(this Series series, IEnumerable<string> list)
{
return list.Any(name => Services.Tasks.Scanner.Parser.Parser.Normalize(name) == series.NormalizedName || Services.Tasks.Scanner.Parser.Parser.Normalize(name) == Services.Tasks.Scanner.Parser.Parser.Normalize(series.Name)
|| name == series.Name || name == series.LocalizedName || name == series.OriginalName || Services.Tasks.Scanner.Parser.Parser.Normalize(name) == Services.Tasks.Scanner.Parser.Parser.Normalize(series.OriginalName));
}
/// <summary>
/// Checks against all the name variables of the Series if it matches anything in the list. Includes a check against the Format of the Series
/// </summary>
/// <param name="series"></param>
/// <param name="list"></param>
/// <returns></returns>
public static bool NameInList(this Series series, IEnumerable<ParsedSeries> list)
{
return list.Any(name => Services.Tasks.Scanner.Parser.Parser.Normalize(name.Name) == series.NormalizedName || Services.Tasks.Scanner.Parser.Parser.Normalize(name.Name) == Services.Tasks.Scanner.Parser.Parser.Normalize(series.Name)
|| name.Name == series.Name || name.Name == series.LocalizedName || name.Name == series.OriginalName || Services.Tasks.Scanner.Parser.Parser.Normalize(name.Name) == Services.Tasks.Scanner.Parser.Parser.Normalize(series.OriginalName) && series.Format == name.Format);
}
/// <summary>
/// Checks against all the name variables of the Series if it matches the <see cref="ParserInfo"/>
/// </summary>
/// <param name="series"></param>
/// <param name="info"></param>
/// <returns></returns>
public static bool NameInParserInfo(this Series series, ParserInfo info)
{
if (info == null) return false;
return Services.Tasks.Scanner.Parser.Parser.Normalize(info.Series) == series.NormalizedName || Services.Tasks.Scanner.Parser.Parser.Normalize(info.Series) == Services.Tasks.Scanner.Parser.Parser.Normalize(series.Name)
|| info.Series == series.Name || info.Series == series.LocalizedName || info.Series == series.OriginalName
|| Services.Tasks.Scanner.Parser.Parser.Normalize(info.Series) == Services.Tasks.Scanner.Parser.Parser.Normalize(series.OriginalName);
}
/// <summary>
/// Calculates the Cover Image for the Series
/// </summary>
/// <param name="series"></param>
/// <returns></returns>
/// <remarks>This is under the assumption that the Volume already has a Cover Image calculated and set</remarks>
public static string GetCoverImage(this Series series)
{
var volumes = series.Volumes ?? new List<Volume>();
var firstVolume = volumes.GetCoverImage(series.Format);
string coverImage = null;
var chapters = firstVolume.Chapters.OrderBy(c => double.Parse(c.Number), ChapterSortComparerZeroFirst.Default).ToList();
if (chapters.Count > 1 && chapters.Any(c => c.IsSpecial))
{
coverImage = chapters.FirstOrDefault(c => !c.IsSpecial)?.CoverImage ?? chapters.First().CoverImage;
firstVolume = null;
}
return firstVolume?.CoverImage ?? coverImage;
}
}