feat: support long text and safe attachment batches
Build web service image / build (push) Successful in 1m15s
Build web service image / build (push) Successful in 1m15s
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user