BookResolver.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  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 = { ".pdf", ".epub", ".mobi", ".cbr", ".cbz", ".azw3" };
  14. protected 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. return null;
  20. if (args.IsDirectory)
  21. {
  22. return GetBook(args);
  23. }
  24. var extension = Path.GetExtension(args.Path);
  25. if (extension != null && _validExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
  26. {
  27. // It's a book
  28. return new Book
  29. {
  30. Path = args.Path,
  31. IsInMixedFolder = true
  32. };
  33. }
  34. return null;
  35. }
  36. private Book GetBook(ItemResolveArgs args)
  37. {
  38. var bookFiles = args.FileSystemChildren.Where(f =>
  39. {
  40. var fileExtension = Path.GetExtension(f.FullName) ??
  41. string.Empty;
  42. return _validExtensions.Contains(fileExtension,
  43. StringComparer
  44. .OrdinalIgnoreCase);
  45. }).ToList();
  46. // Don't return a Book if there is more (or less) than one document in the directory
  47. if (bookFiles.Count != 1)
  48. return null;
  49. return new Book
  50. {
  51. Path = bookFiles[0].FullName
  52. };
  53. }
  54. }
  55. }