BookResolver.cs 2.1 KB

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