AudioBookListResolver.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Emby.Naming.Common;
  6. using Emby.Naming.Video;
  7. using MediaBrowser.Model.IO;
  8. namespace Emby.Naming.AudioBook
  9. {
  10. public class AudioBookListResolver
  11. {
  12. private readonly NamingOptions _options;
  13. public AudioBookListResolver(NamingOptions options)
  14. {
  15. _options = options;
  16. }
  17. public IEnumerable<AudioBookInfo> Resolve(List<FileSystemMetadata> files)
  18. {
  19. var audioBookResolver = new AudioBookResolver(_options);
  20. var audiobookFileInfos = files
  21. .Select(i => audioBookResolver.Resolve(i.FullName, i.IsDirectory))
  22. .Where(i => i != null)
  23. .ToList();
  24. // Filter out all extras, otherwise they could cause stacks to not be resolved
  25. // See the unit test TestStackedWithTrailer
  26. var metadata = audiobookFileInfos
  27. .Select(i => new FileSystemMetadata
  28. {
  29. FullName = i.Path,
  30. IsDirectory = i.IsDirectory
  31. });
  32. var stackResult = new StackResolver(_options)
  33. .ResolveAudioBooks(metadata);
  34. var list = new List<AudioBookInfo>();
  35. foreach (var stack in stackResult.Stacks)
  36. {
  37. var stackFiles = stack.Files.Select(i => audioBookResolver.Resolve(i, stack.IsDirectoryStack)).ToList();
  38. stackFiles.Sort();
  39. var info = new AudioBookInfo
  40. {
  41. Files = stackFiles,
  42. Name = stack.Name
  43. };
  44. list.Add(info);
  45. }
  46. // Whatever files are left, just add them
  47. /*list.AddRange(remainingFiles.Select(i => new AudioBookInfo
  48. {
  49. Files = new List<AudioBookFileInfo> { i },
  50. Name = i.,
  51. Year = i.Year
  52. }));*/
  53. var orderedList = list.OrderBy(i => i.Name);
  54. return orderedList;
  55. }
  56. }
  57. }