namespace Oms.Application.Storage;
///
/// 文件存储 key / URL 规范化。
///
public static class FileStoragePath
{
///
/// 规范化为库内 object key:uploads/a.jpg(无前导 /,完整 URL 取 AbsolutePath)。
///
public static string NormalizeKey(string? pathOrUrl)
{
if (string.IsNullOrWhiteSpace(pathOrUrl))
{
throw new ArgumentException("路径不能为空", nameof(pathOrUrl));
}
var key = pathOrUrl.Trim().Replace('\\', '/');
if (key.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
|| key.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
key = new Uri(key).AbsolutePath;
}
key = key.TrimStart('/');
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("object key 不能为空", nameof(pathOrUrl));
}
return key;
}
///
/// 是否已是绝对 URL(含微信头像等外链)。
///
public static bool IsAbsoluteUrl(string? value) =>
!string.IsNullOrWhiteSpace(value)
&& (value.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
|| value.StartsWith("https://", StringComparison.OrdinalIgnoreCase));
}