17 int index = name.LastIndexOf(
'/');
18 if (index == -1 || index == name.Length - 1)
24 return name.Substring(0, index).Replace(
'/',
'.');
36 dir = Path.GetFullPath(dir);
37 if (dir.EndsWith(Path.DirectorySeparatorChar) ==
false)
38 dir += Path.DirectorySeparatorChar;
41 file = Path.Combine(dir, file);
42 if (file.StartsWith(dir) ==
false)
43 throw new InvalidOperationException(
"Resource file does not exist in directory.");
46 file = file[dir.Length..];
47 if (Path.DirectorySeparatorChar !=
'/')
48 file = file.Replace(Path.DirectorySeparatorChar,
'/');
51 if (file !=
"" && Directory.Exists(file))
62 public static string?
ToFilePath(
string dir,
string name)
64 var expectDirectory = name.EndsWith(
'/');
66 name = name.TrimEnd(
'/');
68 var path = ToSafeFilePath(name);
71 var file = Path.Combine(dir, path);
72 if (Directory.Exists(file) || (Directory.Exists(file) ==
false && expectDirectory ==
false))
88 static string? ToSafeFilePath(
string name)
93 while ((next = name.IndexOf(
'/', off)) != -1)
96 if (MayTranslate(name, off, len) ==
false)
102 int rem = name.Length - off;
103 if (MayTranslate(name, off, rem) ==
false)
108 if (Path.DirectorySeparatorChar ==
'/')
115 if (name.Contains(Path.DirectorySeparatorChar))
119 path = name.Replace(
'/', Path.DirectorySeparatorChar);
123 return Path.IsPathRooted(path) ==
false ? path :
null;
134 static bool MayTranslate(
string name,
int off,
int len)
141 var starsWithDot = (name[off] ==
'.');
142 if (len == 1 && starsWithDot)
144 if (len == 2 && starsWithDot && (name[off + 1] ==
'.'))