namespace Cleanuparr.Shared.Helpers; /// /// Helpers for working with file system paths. /// public static class PathHelper { /// /// Remaps a file path by replacing a source directory prefix with a target directory prefix. /// Checks path-segment boundaries to avoid false matches /// (e.g. source "/downloads" does not match "/downloads-other/file.mkv"). /// /// The file path to remap. /// The directory prefix to replace (e.g. "/downloads"). /// The replacement directory prefix (e.g. "/mnt/media"). /// The remapped path, or unchanged if no match. public static string RemapPath(string filePath, string? source, string? target) { if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target)) { return filePath; } var normSource = source.TrimEnd('/', '\\') + Path.DirectorySeparatorChar; var normTarget = target.TrimEnd('/', '\\'); // Exact match: filePath is exactly the source directory (no trailing separator) if (filePath.Equals(normSource.TrimEnd(Path.DirectorySeparatorChar), StringComparison.OrdinalIgnoreCase)) { return normTarget; } // Prefix match with path-segment boundary: filePath starts with "source/" if (filePath.StartsWith(normSource, StringComparison.OrdinalIgnoreCase)) { return normTarget + Path.DirectorySeparatorChar + filePath[normSource.Length..]; } return filePath; } }