AudioBookResolver.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Emby.Naming.Common;
  5. namespace Emby.Naming.AudioBook
  6. {
  7. public class AudioBookResolver
  8. {
  9. private readonly NamingOptions _options;
  10. public AudioBookResolver(NamingOptions options)
  11. {
  12. _options = options;
  13. }
  14. public AudioBookFileInfo ParseFile(string path)
  15. {
  16. return Resolve(path, false);
  17. }
  18. public AudioBookFileInfo ParseDirectory(string path)
  19. {
  20. return Resolve(path, true);
  21. }
  22. public AudioBookFileInfo Resolve(string path, bool IsDirectory = false)
  23. {
  24. if (string.IsNullOrEmpty(path))
  25. {
  26. throw new ArgumentNullException(nameof(path));
  27. }
  28. if (IsDirectory)
  29. return null;
  30. var extension = Path.GetExtension(path) ?? string.Empty;
  31. // Check supported extensions
  32. if (!_options.AudioFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
  33. {
  34. return null;
  35. }
  36. var container = extension.TrimStart('.');
  37. var parsingResult = new AudioBookFilePathParser(_options)
  38. .Parse(path, IsDirectory);
  39. return new AudioBookFileInfo
  40. {
  41. Path = path,
  42. Container = container,
  43. PartNumber = parsingResult.PartNumber,
  44. ChapterNumber = parsingResult.ChapterNumber,
  45. IsDirectory = IsDirectory
  46. };
  47. }
  48. }
  49. }