BookResolver.cs 2.1 KB

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