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.Model.Entities;
  10. namespace Emby.Server.Implementations.Library.Resolvers.Books
  11. {
  12. public class BookResolver : MediaBrowser.Controller.Resolvers.ItemResolver<Book>
  13. {
  14. private readonly string[] _validExtensions = { ".azw", ".azw3", ".cb7", ".cbr", ".cbt", ".cbz", ".epub", ".mobi", ".pdf" };
  15. public override Book Resolve(ItemResolveArgs args)
  16. {
  17. var collectionType = args.GetCollectionType();
  18. // Only process items that are in a collection folder containing books
  19. if (!string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase))
  20. {
  21. return null;
  22. }
  23. if (args.IsDirectory)
  24. {
  25. return GetBook(args);
  26. }
  27. var extension = Path.GetExtension(args.Path);
  28. if (extension != null && _validExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
  29. {
  30. // It's a book
  31. return new Book
  32. {
  33. Path = args.Path,
  34. IsInMixedFolder = true
  35. };
  36. }
  37. return null;
  38. }
  39. private Book GetBook(ItemResolveArgs args)
  40. {
  41. var bookFiles = args.FileSystemChildren.Where(f =>
  42. {
  43. var fileExtension = Path.GetExtension(f.FullName)
  44. ?? string.Empty;
  45. return _validExtensions.Contains(
  46. fileExtension,
  47. StringComparer.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. }