AudioBookResolver.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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) // TODO
  29. {
  30. return null;
  31. }
  32. var extension = Path.GetExtension(path) ?? string.Empty;
  33. // Check supported extensions
  34. if (!_options.AudioFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
  35. {
  36. return null;
  37. }
  38. var container = extension.TrimStart('.');
  39. var parsingResult = new AudioBookFilePathParser(_options)
  40. .Parse(path, IsDirectory);
  41. return new AudioBookFileInfo
  42. {
  43. Path = path,
  44. Container = container,
  45. PartNumber = parsingResult.PartNumber,
  46. ChapterNumber = parsingResult.ChapterNumber,
  47. IsDirectory = IsDirectory
  48. };
  49. }
  50. }
  51. }