BookResolver.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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);
  29. if (extension is not null && _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)
  45. ?? string.Empty;
  46. return _validExtensions.Contains(
  47. fileExtension,
  48. StringComparer.OrdinalIgnoreCase);
  49. }).ToList();
  50. // Don't return a Book if there is more (or less) than one document in the directory
  51. if (bookFiles.Count != 1)
  52. {
  53. return null;
  54. }
  55. return new Book
  56. {
  57. Path = bookFiles[0].FullName
  58. };
  59. }
  60. }
  61. }