36 lines
1.2 KiB
C#
36 lines
1.2 KiB
C#
namespace dy.net.storage
|
|
{
|
|
public static class StoragePath
|
|
{
|
|
public static string NormalizeRemote(string path)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(path)) return "/";
|
|
|
|
var normalized = path.Replace('\\', '/').Trim();
|
|
var segments = normalized.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
|
if (segments.Any(x => x == "." || x == ".."))
|
|
throw new ArgumentException("远端存储路径不能包含 . 或 ..", nameof(path));
|
|
|
|
return "/" + string.Join('/', segments);
|
|
}
|
|
|
|
public static string CombineRemote(params string[] parts)
|
|
{
|
|
return NormalizeRemote(string.Join('/', parts.Where(x => !string.IsNullOrWhiteSpace(x))));
|
|
}
|
|
|
|
public static string DirectoryName(string path)
|
|
{
|
|
var normalized = NormalizeRemote(path);
|
|
var lastSlash = normalized.LastIndexOf('/');
|
|
return lastSlash <= 0 ? "/" : normalized[..lastSlash];
|
|
}
|
|
|
|
public static string Encode(string path)
|
|
{
|
|
var normalized = NormalizeRemote(path);
|
|
return string.Join('/', normalized.Split('/').Select(Uri.EscapeDataString));
|
|
}
|
|
}
|
|
}
|