BookResolver.cs 2.2 KB

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