BookResolver.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. using Jellyfin.Extensions;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Resolvers;
  10. using MediaBrowser.Model.Entities;
  11. namespace Emby.Server.Implementations.Library.Resolvers.Books
  12. {
  13. public class BookResolver : ItemResolver<Book>
  14. {
  15. private readonly string[] _validExtensions = { ".azw", ".azw3", ".cb7", ".cbr", ".cbt", ".cbz", ".epub", ".mobi", ".pdf" };
  16. protected override Book Resolve(ItemResolveArgs args)
  17. {
  18. var collectionType = args.GetCollectionType();
  19. // Only process items that are in a collection folder containing books
  20. if (!string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase))
  21. {
  22. return null;
  23. }
  24. if (args.IsDirectory)
  25. {
  26. return GetBook(args);
  27. }
  28. var extension = Path.GetExtension(args.Path.AsSpan());
  29. if (_validExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
  30. {
  31. // It's a book
  32. return new Book
  33. {
  34. Path = args.Path,
  35. IsInMixedFolder = true
  36. };
  37. }
  38. return null;
  39. }
  40. private Book GetBook(ItemResolveArgs args)
  41. {
  42. var bookFiles = args.FileSystemChildren.Where(f =>
  43. {
  44. var fileExtension = Path.GetExtension(f.FullName.AsSpan());
  45. return _validExtensions.Contains(
  46. fileExtension,
  47. StringComparison.OrdinalIgnoreCase);
  48. }).ToList();
  49. // Don't return a Book if there is more (or less) than one document in the directory
  50. if (bookFiles.Count != 1)
  51. {
  52. return null;
  53. }
  54. return new Book
  55. {
  56. Path = bookFiles[0].FullName
  57. };
  58. }
  59. }
  60. }