AudioBookResolver.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.IO;
  3. using Emby.Naming.Common;
  4. using Jellyfin.Extensions;
  5. namespace Emby.Naming.AudioBook
  6. {
  7. /// <summary>
  8. /// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
  9. /// </summary>
  10. public class AudioBookResolver
  11. {
  12. private readonly NamingOptions _options;
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="AudioBookResolver"/> class.
  15. /// </summary>
  16. /// <param name="options"><see cref="NamingOptions"/> containing AudioFileExtensions and also used to pass to AudioBookFilePathParser.</param>
  17. public AudioBookResolver(NamingOptions options)
  18. {
  19. _options = options;
  20. }
  21. /// <summary>
  22. /// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
  23. /// </summary>
  24. /// <param name="path">Path to audiobook file.</param>
  25. /// <returns>Returns <see cref="AudioBookResolver"/> object.</returns>
  26. public AudioBookFileInfo? Resolve(string path)
  27. {
  28. if (path.Length == 0 || Path.GetFileNameWithoutExtension(path).Length == 0)
  29. {
  30. // Return null to indicate this path will not be used, instead of stopping whole process with exception
  31. return null;
  32. }
  33. var extension = Path.GetExtension(path);
  34. // Check supported extensions
  35. if (!_options.AudioFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
  36. {
  37. return null;
  38. }
  39. var container = extension.TrimStart('.');
  40. var parsingResult = new AudioBookFilePathParser(_options).Parse(path);
  41. return new AudioBookFileInfo(
  42. path,
  43. container,
  44. chapterNumber: parsingResult.ChapterNumber,
  45. partNumber: parsingResult.PartNumber);
  46. }
  47. }
  48. }