| | | 1 | | namespace Anichron.Core; |
| | | 2 | | |
| | | 3 | | public static class StoragePath |
| | | 4 | | { |
| | | 5 | | public const string OriginalsRoot = "/data/originals"; |
| | | 6 | | public const string ProxiesRoot = "/data/proxies"; |
| | | 7 | | |
| | | 8 | | public static string? Canonicalize(string path) |
| | 37 | 9 | | { |
| | 37 | 10 | | var trimmed = path.Trim(); |
| | 37 | 11 | | return Path.IsPathFullyQualified(trimmed) |
| | 37 | 12 | | ? Path.TrimEndingDirectorySeparator(Path.GetFullPath(trimmed)) |
| | 37 | 13 | | : null; |
| | 37 | 14 | | } |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Joins a stored proxy path, relative to <see cref="ProxiesRoot"/>, with the root. Returns |
| | | 18 | | /// <see langword="null"/> when the path resolves outside the root. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="proxyPath">The path relative to the proxy root.</param> |
| | | 21 | | public static string? ResolveProxy(string proxyPath) |
| | 295 | 22 | | { |
| | 295 | 23 | | var fullPath = Path.GetFullPath(Path.Combine(ProxiesRoot, proxyPath)); |
| | 295 | 24 | | return IsInside(ProxiesRoot, fullPath) ? fullPath : null; |
| | 295 | 25 | | } |
| | | 26 | | |
| | | 27 | | public static bool IsInsideOriginals(string canonicalPath) |
| | 32 | 28 | | => IsInside(OriginalsRoot, canonicalPath); |
| | | 29 | | |
| | | 30 | | public static bool IsInside(string root, string canonicalPath) |
| | 363 | 31 | | => canonicalPath.StartsWith(root + "/", StringComparison.Ordinal); |
| | | 32 | | |
| | | 33 | | public static string? FindOverlap(IEnumerable<string> canonicalRoots, string canonicalPath) |
| | 14 | 34 | | => canonicalRoots.FirstOrDefault(root => Overlaps(root, canonicalPath)); |
| | | 35 | | |
| | | 36 | | public static bool Overlaps(string firstCanonicalPath, string secondCanonicalPath) |
| | 18 | 37 | | => IsSameOrDescendant(firstCanonicalPath, secondCanonicalPath) |
| | 18 | 38 | | || IsSameOrDescendant(secondCanonicalPath, firstCanonicalPath); |
| | | 39 | | |
| | | 40 | | private static bool IsSameOrDescendant(string path, string ancestor) |
| | 30 | 41 | | => path.Equals(ancestor, StringComparison.Ordinal) || IsInside(ancestor, path); |
| | | 42 | | } |