StubResolver.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.IO;
  3. using Emby.Naming.Common;
  4. using Jellyfin.Extensions;
  5. namespace Emby.Naming.Video
  6. {
  7. /// <summary>
  8. /// Resolve if file is stub (.disc).
  9. /// </summary>
  10. public static class StubResolver
  11. {
  12. /// <summary>
  13. /// Tries to resolve if file is stub (.disc).
  14. /// </summary>
  15. /// <param name="path">Path to file.</param>
  16. /// <param name="options">NamingOptions containing StubFileExtensions and StubTypes.</param>
  17. /// <param name="stubType">Stub type.</param>
  18. /// <returns>True if file is a stub.</returns>
  19. public static bool TryResolveFile(string path, NamingOptions options, out string? stubType)
  20. {
  21. stubType = default;
  22. if (string.IsNullOrEmpty(path))
  23. {
  24. return false;
  25. }
  26. var extension = Path.GetExtension(path.AsSpan());
  27. if (!options.StubFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
  28. {
  29. return false;
  30. }
  31. var token = Path.GetExtension(Path.GetFileNameWithoutExtension(path.AsSpan())).TrimStart('.');
  32. foreach (var rule in options.StubTypes)
  33. {
  34. if (token.Equals(rule.Token, StringComparison.OrdinalIgnoreCase))
  35. {
  36. stubType = rule.StubType;
  37. return true;
  38. }
  39. }
  40. return true;
  41. }
  42. }
  43. }