Files
wx-win-agent/node-agent/WxAgent.Core/WechatAttachmentInput.cs
T
rogee edba7d0d38
Build web service image / build (push) Successful in 1m15s
feat: support long text and safe attachment batches
2026-09-30 09:16:36 +08:00

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;
}
}