38 lines
1.6 KiB
C#
38 lines
1.6 KiB
C#
namespace WxAgent.Core;
|
|
|
|
public static class WechatAttachmentInput
|
|
{
|
|
public const int MaxBatchSize = 10;
|
|
private static readonly HashSet<string> ImageExtensions = new([".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp"], StringComparer.OrdinalIgnoreCase);
|
|
|
|
public static bool IsImagePath(string? path) =>
|
|
!string.IsNullOrWhiteSpace(path) && ImageExtensions.Contains(Path.GetExtension(path));
|
|
|
|
public static IReadOnlyList<string> PrepareFilePaths(IReadOnlyList<string>? paths)
|
|
{
|
|
if (paths is null || paths.Count is < 2 or > MaxBatchSize)
|
|
throw new WxAgentException(WxAgentErrorCode.InvalidArgument, $"File batches must contain 2-{MaxBatchSize} attachments.");
|
|
|
|
var normalized = new string[paths.Count];
|
|
for (var index = 0; index < paths.Count; index++)
|
|
{
|
|
var path = paths[index];
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
throw new WxAgentException(WxAgentErrorCode.InvalidArgument, "Attachment paths must not be empty.");
|
|
|
|
string fullPath;
|
|
try { fullPath = Path.GetFullPath(path); }
|
|
catch (Exception exception) when (exception is ArgumentException or NotSupportedException or PathTooLongException)
|
|
{
|
|
throw new WxAgentException(WxAgentErrorCode.InvalidArgument, "An attachment path is invalid.");
|
|
}
|
|
|
|
if (!File.Exists(fullPath))
|
|
throw new WxAgentException(WxAgentErrorCode.InvalidArgument, "An attachment file does not exist.");
|
|
normalized[index] = fullPath;
|
|
}
|
|
|
|
return normalized;
|
|
}
|
|
}
|