2
0

BookResolver.cs 2.3 KB

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