2021-05-20 21:28:18 +02:00
|
|
|
#nullable disable
|
|
|
|
|
|
2019-11-01 18:38:54 +01:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
|
2019-01-13 20:54:44 +01:00
|
|
|
using System;
|
2016-12-12 00:49:19 -05:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2025-12-14 00:29:28 +09:00
|
|
|
using Emby.Naming.Book;
|
2023-11-09 14:00:29 -07:00
|
|
|
using Jellyfin.Data.Enums;
|
2021-12-20 13:31:07 +01:00
|
|
|
using Jellyfin.Extensions;
|
2016-12-12 00:49:19 -05:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2022-10-11 23:37:29 +02:00
|
|
|
using MediaBrowser.Controller.Resolvers;
|
2016-12-12 00:49:19 -05:00
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.Library.Resolvers.Books
|
|
|
|
|
{
|
2022-10-11 23:37:29 +02:00
|
|
|
public class BookResolver : ItemResolver<Book>
|
2016-12-12 00:49:19 -05:00
|
|
|
{
|
2020-12-11 21:13:28 -05:00
|
|
|
private readonly string[] _validExtensions = { ".azw", ".azw3", ".cb7", ".cbr", ".cbt", ".cbz", ".epub", ".mobi", ".pdf" };
|
2016-12-12 00:49:19 -05:00
|
|
|
|
2022-10-11 23:37:29 +02:00
|
|
|
protected override Book Resolve(ItemResolveArgs args)
|
2016-12-12 00:49:19 -05:00
|
|
|
{
|
|
|
|
|
var collectionType = args.GetCollectionType();
|
|
|
|
|
|
|
|
|
|
// Only process items that are in a collection folder containing books
|
2023-12-08 15:45:36 -07:00
|
|
|
if (collectionType != CollectionType.books)
|
2020-06-20 21:12:36 +12:00
|
|
|
{
|
2016-12-12 00:49:19 -05:00
|
|
|
return null;
|
2020-06-20 21:12:36 +12:00
|
|
|
}
|
2017-10-06 15:45:05 -04:00
|
|
|
|
2016-12-12 00:49:19 -05:00
|
|
|
if (args.IsDirectory)
|
|
|
|
|
{
|
|
|
|
|
return GetBook(args);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 00:40:09 +02:00
|
|
|
var extension = Path.GetExtension(args.Path.AsSpan());
|
2016-12-12 00:49:19 -05:00
|
|
|
|
2025-12-14 00:29:28 +09:00
|
|
|
if (!_validExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
|
2016-12-12 00:49:19 -05:00
|
|
|
{
|
2025-12-14 00:29:28 +09:00
|
|
|
return null;
|
2016-12-12 00:49:19 -05:00
|
|
|
}
|
|
|
|
|
|
2025-12-14 00:29:28 +09:00
|
|
|
var result = BookFileNameParser.Parse(Path.GetFileNameWithoutExtension(args.Path));
|
|
|
|
|
|
|
|
|
|
return new Book
|
|
|
|
|
{
|
|
|
|
|
Path = args.Path,
|
|
|
|
|
Name = result.Name ?? string.Empty,
|
|
|
|
|
IndexNumber = result.Index,
|
|
|
|
|
ProductionYear = result.Year,
|
|
|
|
|
SeriesName = result.SeriesName ?? Path.GetFileName(Path.GetDirectoryName(args.Path)),
|
|
|
|
|
IsInMixedFolder = true,
|
|
|
|
|
};
|
2016-12-12 00:49:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Book GetBook(ItemResolveArgs args)
|
|
|
|
|
{
|
|
|
|
|
var bookFiles = args.FileSystemChildren.Where(f =>
|
|
|
|
|
{
|
2023-10-06 00:40:09 +02:00
|
|
|
var fileExtension = Path.GetExtension(f.FullName.AsSpan());
|
2016-12-12 00:49:19 -05:00
|
|
|
|
2020-10-12 20:05:11 +02:00
|
|
|
return _validExtensions.Contains(
|
|
|
|
|
fileExtension,
|
2023-10-06 00:40:09 +02:00
|
|
|
StringComparison.OrdinalIgnoreCase);
|
2016-12-12 00:49:19 -05:00
|
|
|
}).ToList();
|
|
|
|
|
|
2025-12-14 00:29:28 +09:00
|
|
|
// directory is only considered a book when it contains exactly one supported file
|
|
|
|
|
// other library structures with multiple books to a directory will get picked up as individual files
|
2016-12-12 00:49:19 -05:00
|
|
|
if (bookFiles.Count != 1)
|
2020-06-20 21:12:36 +12:00
|
|
|
{
|
2016-12-12 00:49:19 -05:00
|
|
|
return null;
|
2020-06-20 21:12:36 +12:00
|
|
|
}
|
2016-12-12 00:49:19 -05:00
|
|
|
|
2025-12-14 00:29:28 +09:00
|
|
|
var result = BookFileNameParser.Parse(Path.GetFileName(args.Path));
|
|
|
|
|
|
2016-12-12 00:49:19 -05:00
|
|
|
return new Book
|
2017-10-06 15:45:05 -04:00
|
|
|
{
|
2025-12-14 00:29:28 +09:00
|
|
|
Path = bookFiles[0].FullName,
|
|
|
|
|
Name = result.Name ?? string.Empty,
|
|
|
|
|
IndexNumber = result.Index,
|
|
|
|
|
ProductionYear = result.Year,
|
|
|
|
|
SeriesName = result.SeriesName ?? string.Empty,
|
2017-10-06 15:45:05 -04:00
|
|
|
};
|
2016-12-12 00:49:19 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|