BookResolver.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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", ".opf", ".pdf" };
  13. protected 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(fileExtension,
  44. StringComparer
  45. .OrdinalIgnoreCase);
  46. }).ToList();
  47. // Don't return a Book if there is more (or less) than one document in the directory
  48. if (bookFiles.Count != 1)
  49. {
  50. return null;
  51. }
  52. return new Book
  53. {
  54. Path = bookFiles[0].FullName
  55. };
  56. }
  57. }
  58. }