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; } // Normalize separators so Windows source paths (backslashes) match Linux-normalized filePaths var normSource = source.Replace('\\', Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar) + 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; } /// /// Normalizes path separators to the host's and then /// applies . /// public static string NormalizeAndRemap(string path, string? source, string? target) { string normalized = string.Join(Path.DirectorySeparatorChar, path.Split(['\\', '/'])); return RemapPath(normalized, source, target); } }