All checks were successful
Build and Deploy / deploy (push) Successful in 1m41s
42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
namespace Oms.Application.Storage;
|
||
|
||
/// <summary>
|
||
/// 文件存储 key / URL 规范化。
|
||
/// </summary>
|
||
public static class FileStoragePath
|
||
{
|
||
/// <summary>
|
||
/// 规范化为库内 object key:uploads/a.jpg(无前导 /,完整 URL 取 AbsolutePath)。
|
||
/// </summary>
|
||
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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否已是绝对 URL(含微信头像等外链)。
|
||
/// </summary>
|
||
public static bool IsAbsoluteUrl(string? value) =>
|
||
!string.IsNullOrWhiteSpace(value)
|
||
&& (value.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
|
||
|| value.StartsWith("https://", StringComparison.OrdinalIgnoreCase));
|
||
}
|