StubResolver.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Emby.Naming.Common;
  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);
  27. if (!options.StubFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
  28. {
  29. return false;
  30. }
  31. path = Path.GetFileNameWithoutExtension(path);
  32. var token = Path.GetExtension(path).TrimStart('.');
  33. foreach (var rule in options.StubTypes)
  34. {
  35. if (string.Equals(rule.Token, token, StringComparison.OrdinalIgnoreCase))
  36. {
  37. stubType = rule.StubType;
  38. return true;
  39. }
  40. }
  41. return true;
  42. }
  43. }
  44. }