Blame view

netcore/src/Modularity/Extend/NCC.Extend/ImageModerationService.cs 26.5 KB
e486fd72   “wangming”   feat: implement i...
1
2
3
4
5
6
7
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Net.Http;
  using System.Security.Cryptography;
  using System.Text;
  using System.Threading.Tasks;
891ea9b6   “wangming”   refactor: update ...
8
9
10
11
12
  using AlibabaCloud.OpenApiClient.Models;
  using AlibabaCloud.SDK.Green20220302;
  using AlibabaCloud.SDK.Green20220302.Models;
  using AlibabaCloud.TeaUtil.Models;
  using Tea;
e486fd72   “wangming”   feat: implement i...
13
14
15
16
  using NCC.Common.Configuration;
  using NCC.Dependency;
  using Newtonsoft.Json;
  using Newtonsoft.Json.Linq;
891ea9b6   “wangming”   refactor: update ...
17
  using OSS = AlibabaCloud.OSS.V2;
e486fd72   “wangming”   feat: implement i...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  
  namespace NCC.Extend
  {
      /// <summary>
      /// 图片审核服务(阿里云内容安全),仅内部调用,不暴露 API
      /// </summary>
      public class ImageModerationService : ITransient
      {
          private readonly HttpClient _httpClient;
          private readonly string _endpoint;
          private readonly string _accessKeyId;
          private readonly string _accessKeySecret;
          private readonly string _region;
          private readonly bool _enabled;
          private readonly bool _isEnhancedVersion; // 是否为增强版(green-cip
  
          /// <summary>
          /// 初始化图片审核服务
          /// </summary>
          public ImageModerationService()
          {
              _httpClient = new HttpClient();
              _enabled = App.Configuration["NCC_App:ImageModeration:Enabled"] == "true";
5652f5e0   “wangming”   refactor: update ...
41
              _endpoint = App.Configuration["NCC_App:ImageModeration:Endpoint"]
e486fd72   “wangming”   feat: implement i...
42
                  ?? "https://green.cn-shanghai.aliyuncs.com";
5652f5e0   “wangming”   refactor: update ...
43
              _region = App.Configuration["NCC_App:ImageModeration:Region"]
e486fd72   “wangming”   feat: implement i...
44
                  ?? "cn-shanghai";
5652f5e0   “wangming”   refactor: update ...
45
  
e486fd72   “wangming”   feat: implement i...
46
47
48
49
50
51
              // 优先使用ImageModeration配置,如果没有则使用AliyunOSS的配置
              _accessKeyId = App.Configuration["NCC_App:ImageModeration:AccessKeyId"];
              if (string.IsNullOrEmpty(_accessKeyId))
              {
                  _accessKeyId = App.Configuration["NCC_App:AliyunOSS:AccessKeyId"];
              }
5652f5e0   “wangming”   refactor: update ...
52
  
e486fd72   “wangming”   feat: implement i...
53
54
55
56
57
              _accessKeySecret = App.Configuration["NCC_App:ImageModeration:AccessKeySecret"];
              if (string.IsNullOrEmpty(_accessKeySecret))
              {
                  _accessKeySecret = App.Configuration["NCC_App:AliyunOSS:AccessKeySecret"];
              }
5652f5e0   “wangming”   refactor: update ...
58
  
e486fd72   “wangming”   feat: implement i...
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
              // 判断是否为增强版(endpoint 包含 green-cip
              _isEnhancedVersion = _endpoint.Contains("green-cip");
          }
  
          /// <summary>
          /// 图片审核结果
          /// </summary>
          public class ModerationResult
          {
              public bool Passed { get; set; }
              public string Message { get; set; }
              public string RawResponse { get; set; }
              public object Details { get; set; }
          }
  
          /// <summary>
          /// 图片审核(从本地文件路径)
          /// </summary>
          /// <param name="filePath">本地文件路径</param>
          /// <returns>审核结果详情</returns>
          public async Task<ModerationResult> ScanImageFromFileAsync(string filePath)
          {
              if (!_enabled)
              {
5652f5e0   “wangming”   refactor: update ...
83
84
85
86
                  return new ModerationResult
                  {
                      Passed = true,
                      Message = "审核未启用,直接通过"
e486fd72   “wangming”   feat: implement i...
87
88
89
90
91
                  };
              }
  
              if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
              {
5652f5e0   “wangming”   refactor: update ...
92
93
94
95
                  return new ModerationResult
                  {
                      Passed = false,
                      Message = "文件不存在或路径为空"
e486fd72   “wangming”   feat: implement i...
96
97
98
99
100
101
102
103
104
105
106
107
                  };
              }
  
              try
              {
                  var imageBytes = await File.ReadAllBytesAsync(filePath);
                  return await ScanImageAsync(imageBytes);
              }
              catch (Exception ex)
              {
                  // 审核异常时,根据配置决定是否通过
                  var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
5652f5e0   “wangming”   refactor: update ...
108
109
110
                  return new ModerationResult
                  {
                      Passed = !failOnError,
e486fd72   “wangming”   feat: implement i...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
                      Message = $"审核异常:{ex.Message}",
                      RawResponse = ex.ToString()
                  };
              }
          }
  
          /// <summary>
          /// 图片审核(从字节数组)
          /// </summary>
          /// <param name="imageBytes">图片字节数组</param>
          /// <returns>审核结果详情</returns>
          public async Task<ModerationResult> ScanImageAsync(byte[] imageBytes)
          {
              if (!_enabled)
              {
5652f5e0   “wangming”   refactor: update ...
126
127
128
129
                  return new ModerationResult
                  {
                      Passed = true,
                      Message = "审核未启用,直接通过"
e486fd72   “wangming”   feat: implement i...
130
131
132
133
134
                  };
              }
  
              if (imageBytes == null || imageBytes.Length == 0)
              {
5652f5e0   “wangming”   refactor: update ...
135
136
137
138
                  return new ModerationResult
                  {
                      Passed = false,
                      Message = "图片数据为空"
e486fd72   “wangming”   feat: implement i...
139
140
141
142
143
144
145
                  };
              }
  
              // 检查 AccessKey 是否配置
              if (string.IsNullOrEmpty(_accessKeyId) || string.IsNullOrEmpty(_accessKeySecret))
              {
                  var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
5652f5e0   “wangming”   refactor: update ...
146
147
148
                  return new ModerationResult
                  {
                      Passed = !failOnError,
e486fd72   “wangming”   feat: implement i...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
                      Message = "图片审核服务未配置 AccessKey,无法调用审核接口",
                      RawResponse = "AccessKeyId 或 AccessKeySecret 未配置"
                  };
              }
  
              try
              {
                  // 判断是否为增强版,使用不同的调用方式
                  if (_isEnhancedVersion)
                  {
                      return await ScanImageEnhancedAsync(imageBytes);
                  }
                  else
                  {
                      return await ScanImageLegacyAsync(imageBytes);
                  }
              }
              catch (Exception ex)
              {
                  // 审核异常时,根据配置决定是否通过
                  var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
5652f5e0   “wangming”   refactor: update ...
170
171
172
                  return new ModerationResult
                  {
                      Passed = !failOnError,
e486fd72   “wangming”   feat: implement i...
173
174
175
176
177
178
179
                      Message = $"审核异常:{ex.Message}",
                      RawResponse = ex.ToString()
                  };
              }
          }
  
          /// <summary>
891ea9b6   “wangming”   refactor: update ...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
          /// 创建增强版内容安全 ClientGreen 20220302
          /// </summary>
          /// <returns>Green Client 实例</returns>
          private static Client CreateGreenClient(string accessKeyId, string accessKeySecret, string endpoint)
          {
              var endpointHost = endpoint?.Replace("https://", "").Replace("http://", "") ?? "green-cip.cn-shanghai.aliyuncs.com";
              var config = new Config
              {
                  AccessKeyId = accessKeyId,
                  AccessKeySecret = accessKeySecret,
                  Endpoint = endpointHost
              };
              return new Client(config);
          }
  
          /// <summary>
e486fd72   “wangming”   feat: implement i...
196
          /// 图片审核(增强版 - green-cip
891ea9b6   “wangming”   refactor: update ...
197
          /// 使用官方 SDKDescribeUploadToken  上传临时 OSS  ImageModeration
e486fd72   “wangming”   feat: implement i...
198
199
200
201
202
203
204
          /// </summary>
          /// <param name="imageBytes">图片字节数组</param>
          /// <returns>审核结果详情</returns>
          private async Task<ModerationResult> ScanImageEnhancedAsync(byte[] imageBytes)
          {
              try
              {
891ea9b6   “wangming”   refactor: update ...
205
206
207
208
209
210
211
212
213
214
215
                  var client = CreateGreenClient(_accessKeyId, _accessKeySecret, _endpoint);
                  var runtime = new RuntimeOptions();
  
                  // 1. 获取临时 OSS 上传凭证
                  DescribeUploadTokenResponse tokenResponse = null;
                  await Task.Run(() =>
                  {
                      tokenResponse = client.DescribeUploadToken();
                  }).ConfigureAwait(false);
  
                  if (tokenResponse?.Body?.Code != 200)
e486fd72   “wangming”   feat: implement i...
216
217
                  {
                      var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
891ea9b6   “wangming”   refactor: update ...
218
219
220
221
222
                      return new ModerationResult
                      {
                          Passed = !failOnError,
                          Message = $"获取临时 OSS 凭证失败:{tokenResponse?.Body?.Msg ?? "未知错误"}",
                          RawResponse = tokenResponse != null ? JsonConvert.SerializeObject(tokenResponse.Body) : "null"
e486fd72   “wangming”   feat: implement i...
223
224
225
                      };
                  }
  
891ea9b6   “wangming”   refactor: update ...
226
                  var tokenData = tokenResponse.Body.Data;
e486fd72   “wangming”   feat: implement i...
227
228
229
                  if (tokenData == null)
                  {
                      var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
891ea9b6   “wangming”   refactor: update ...
230
231
232
                      return new ModerationResult
                      {
                          Passed = !failOnError,
e486fd72   “wangming”   feat: implement i...
233
                          Message = "临时 OSS 凭证数据为空",
891ea9b6   “wangming”   refactor: update ...
234
                          RawResponse = JsonConvert.SerializeObject(tokenResponse.Body)
e486fd72   “wangming”   feat: implement i...
235
236
237
                      };
                  }
  
891ea9b6   “wangming”   refactor: update ...
238
239
240
241
242
243
244
245
246
247
248
249
250
                  var bucketName = tokenData.BucketName;
                  var fileNamePrefix = tokenData.FileNamePrefix ?? "";
                  var ossEndpoint = tokenData.OssInternetEndPoint ?? "";
                  if (string.IsNullOrEmpty(ossEndpoint))
                      ossEndpoint = tokenData.OssInternalEndPoint ?? "";
                  if (!ossEndpoint.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                      ossEndpoint = "https://" + ossEndpoint;
  
                  //  token 返回的 OSS endpoint 推导 region,与签名一致(避免 Invalid signing region
                  var ossRegion = _region;
                  const string ossCnPrefix = "oss-cn-";
                  var ossCnIdx = ossEndpoint.IndexOf(ossCnPrefix, StringComparison.OrdinalIgnoreCase);
                  if (ossCnIdx >= 0)
e486fd72   “wangming”   feat: implement i...
251
                  {
891ea9b6   “wangming”   refactor: update ...
252
253
254
255
                      var start = ossCnIdx + ossCnPrefix.Length; // "oss-cn-" 后为 "shanghai" 等,region  "cn-xxx"
                      var end = ossEndpoint.IndexOf(".", start, StringComparison.Ordinal);
                      var regionSuffix = end > start ? ossEndpoint.Substring(start, end - start) : ossEndpoint.Substring(start);
                      ossRegion = "cn-" + regionSuffix;
e486fd72   “wangming”   feat: implement i...
256
257
                  }
  
891ea9b6   “wangming”   refactor: update ...
258
259
260
261
262
263
264
265
266
267
268
269
                  // 2. 使用 OSS.V2 + STS 凭证上传图片到临时 OSS
                  var objectName = fileNamePrefix + Guid.NewGuid().ToString("N") + ".jpg";
                  var ossCfg = OSS.Configuration.LoadDefault();
                  ossCfg.CredentialsProvider = new OSS.Credentials.StaticCredentialsProvider(
                      tokenData.AccessKeyId,
                      tokenData.AccessKeySecret,
                      tokenData.SecurityToken);
                  ossCfg.Region = ossRegion;
                  ossCfg.Endpoint = ossEndpoint;
  
                  using (var ossClient = new OSS.Client(ossCfg))
                  using (var bodyStream = new MemoryStream(imageBytes))
e486fd72   “wangming”   feat: implement i...
270
                  {
891ea9b6   “wangming”   refactor: update ...
271
272
273
274
275
276
                      await ossClient.PutObjectAsync(new OSS.Models.PutObjectRequest
                      {
                          Bucket = bucketName,
                          Key = objectName,
                          Body = bodyStream
                      }).ConfigureAwait(false);
e486fd72   “wangming”   feat: implement i...
277
278
279
                  }
  
                  // 3. 调用增强版审核接口 ImageModeration
e486fd72   “wangming”   feat: implement i...
280
281
282
283
284
285
                  var serviceParameters = new Dictionary<string, object>
                  {
                      { "ossBucketName", bucketName },
                      { "ossObjectName", objectName },
                      { "dataId", Guid.NewGuid().ToString() }
                  };
891ea9b6   “wangming”   refactor: update ...
286
287
288
289
290
291
  
                  // 中国区(cn-shanghai 等)使用 baselineCheck,国际区使用 baselineCheck_global
                  var serviceName = _region != null && _region.StartsWith("cn-", StringComparison.OrdinalIgnoreCase)
                      ? "baselineCheck"
                      : "baselineCheck_global";
                  var request = new ImageModerationRequest
e486fd72   “wangming”   feat: implement i...
292
                  {
891ea9b6   “wangming”   refactor: update ...
293
294
                      Service = serviceName,
                      ServiceParameters = JsonConvert.SerializeObject(serviceParameters)
e486fd72   “wangming”   feat: implement i...
295
                  };
891ea9b6   “wangming”   refactor: update ...
296
297
298
299
300
301
302
303
304
305
  
                  ImageModerationResponse moderationResponse = null;
                  await Task.Run(() =>
                  {
                      moderationResponse = client.ImageModerationWithOptions(request, runtime);
                  }).ConfigureAwait(false);
  
                  var rawResponse = moderationResponse != null ? JsonConvert.SerializeObject(moderationResponse.Body) : "null";
  
                  if (moderationResponse?.Body == null)
e486fd72   “wangming”   feat: implement i...
306
307
                  {
                      var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
891ea9b6   “wangming”   refactor: update ...
308
309
310
311
                      return new ModerationResult
                      {
                          Passed = !failOnError,
                          Message = "审核接口无响应",
e486fd72   “wangming”   feat: implement i...
312
313
314
315
                          RawResponse = rawResponse
                      };
                  }
  
891ea9b6   “wangming”   refactor: update ...
316
                  var code = moderationResponse.Body.Code;
e486fd72   “wangming”   feat: implement i...
317
318
319
                  if (code != 200)
                  {
                      var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
891ea9b6   “wangming”   refactor: update ...
320
321
322
323
                      return new ModerationResult
                      {
                          Passed = !failOnError,
                          Message = moderationResponse.Body.Msg ?? $"审核接口返回错误:{code}",
e486fd72   “wangming”   feat: implement i...
324
                          RawResponse = rawResponse,
891ea9b6   “wangming”   refactor: update ...
325
                          Details = moderationResponse.Body
e486fd72   “wangming”   feat: implement i...
326
327
328
                      };
                  }
  
891ea9b6   “wangming”   refactor: update ...
329
                  var data = moderationResponse.Body.Data;
e486fd72   “wangming”   feat: implement i...
330
331
332
                  if (data == null)
                  {
                      var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
891ea9b6   “wangming”   refactor: update ...
333
334
335
                      return new ModerationResult
                      {
                          Passed = !failOnError,
e486fd72   “wangming”   feat: implement i...
336
337
                          Message = "审核结果数据为空",
                          RawResponse = rawResponse,
891ea9b6   “wangming”   refactor: update ...
338
                          Details = moderationResponse.Body
e486fd72   “wangming”   feat: implement i...
339
340
341
                      };
                  }
  
891ea9b6   “wangming”   refactor: update ...
342
343
344
                  var results = data.Result;
                  var riskLevel = data.RiskLevel ?? "";
  
e486fd72   “wangming”   feat: implement i...
345
346
                  if (results == null || results.Count == 0)
                  {
891ea9b6   “wangming”   refactor: update ...
347
348
349
                      return new ModerationResult
                      {
                          Passed = true,
e486fd72   “wangming”   feat: implement i...
350
351
352
353
354
355
                          Message = "没有审核结果,默认通过",
                          RawResponse = rawResponse,
                          Details = data
                      };
                  }
  
e486fd72   “wangming”   feat: implement i...
356
357
                  var highRiskLabels = new List<string>();
                  var allResults = new List<object>();
891ea9b6   “wangming”   refactor: update ...
358
  
e486fd72   “wangming”   feat: implement i...
359
360
                  foreach (var item in results)
                  {
891ea9b6   “wangming”   refactor: update ...
361
362
363
364
365
                      var label = item.Label ?? "";
                      var confidence = item.Confidence;
                      var itemRiskLevel = item.RiskLevel ?? "";
                      var description = item.Description ?? "";
  
e486fd72   “wangming”   feat: implement i...
366
367
368
369
370
371
372
                      allResults.Add(new
                      {
                          label = label,
                          confidence = confidence,
                          riskLevel = itemRiskLevel,
                          description = description
                      });
891ea9b6   “wangming”   refactor: update ...
373
374
  
                      if (itemRiskLevel == "high" || (!string.IsNullOrEmpty(label) && !label.StartsWith("nonLabel")))
e486fd72   “wangming”   feat: implement i...
375
                          highRiskLabels.Add($"{label}({description})");
e486fd72   “wangming”   feat: implement i...
376
377
378
379
                  }
  
                  if (highRiskLabels.Count > 0 || riskLevel == "high")
                  {
891ea9b6   “wangming”   refactor: update ...
380
381
382
                      return new ModerationResult
                      {
                          Passed = false,
e486fd72   “wangming”   feat: implement i...
383
384
385
386
387
388
389
390
391
392
393
                          Message = $"图片审核未通过,风险等级:{riskLevel},违规标签:{string.Join(", ", highRiskLabels)}",
                          RawResponse = rawResponse,
                          Details = new
                          {
                              riskLevel = riskLevel,
                              highRiskLabels = highRiskLabels,
                              allResults = allResults
                          }
                      };
                  }
  
891ea9b6   “wangming”   refactor: update ...
394
395
396
                  return new ModerationResult
                  {
                      Passed = true,
e486fd72   “wangming”   feat: implement i...
397
398
399
400
401
402
403
404
405
                      Message = "图片审核通过",
                      RawResponse = rawResponse,
                      Details = new
                      {
                          riskLevel = riskLevel,
                          allResults = allResults
                      }
                  };
              }
891ea9b6   “wangming”   refactor: update ...
406
407
408
409
410
411
412
413
414
415
416
417
418
              catch (TeaException teaEx)
              {
                  var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
                  var msg = teaEx.Message ?? "增强版审核 Tea 异常";
                  if (teaEx.Data != null && teaEx.Data.Contains("Recommend"))
                      msg += ";诊断:" + teaEx.Data["Recommend"];
                  return new ModerationResult
                  {
                      Passed = !failOnError,
                      Message = msg,
                      RawResponse = teaEx.ToString()
                  };
              }
e486fd72   “wangming”   feat: implement i...
419
420
421
              catch (Exception ex)
              {
                  var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
891ea9b6   “wangming”   refactor: update ...
422
423
424
                  return new ModerationResult
                  {
                      Passed = !failOnError,
e486fd72   “wangming”   feat: implement i...
425
426
427
428
429
430
431
                      Message = $"增强版审核异常:{ex.Message}",
                      RawResponse = ex.ToString()
                  };
              }
          }
  
          /// <summary>
e486fd72   “wangming”   feat: implement i...
432
433
434
435
436
437
438
439
          /// 图片审核(老版 - green.xxx
          /// </summary>
          /// <param name="imageBytes">图片字节数组</param>
          /// <returns>审核结果详情</returns>
          private async Task<ModerationResult> ScanImageLegacyAsync(byte[] imageBytes)
          {
              // 构建请求URL
              var url = $"{_endpoint}/green/image/scan";
5652f5e0   “wangming”   refactor: update ...
440
441
442
443
444
445
  
              // 构建请求体
              var requestBody = new
              {
                  scenes = new[] { "porn", "terrorism", "ad", "qrcode" },
                  tasks = new[]
e486fd72   “wangming”   feat: implement i...
446
                  {
e486fd72   “wangming”   feat: implement i...
447
448
449
450
451
452
                          new
                          {
                              dataId = Guid.NewGuid().ToString(),
                              url = Convert.ToBase64String(imageBytes)
                          }
                      }
5652f5e0   “wangming”   refactor: update ...
453
              };
e486fd72   “wangming”   feat: implement i...
454
  
5652f5e0   “wangming”   refactor: update ...
455
456
457
458
              var jsonContent = JsonConvert.SerializeObject(requestBody);
              var contentBytes = Encoding.UTF8.GetBytes(jsonContent);
              var content = new ByteArrayContent(contentBytes);
              content.Headers.TryAddWithoutValidation("Content-Type", "application/json");
e486fd72   “wangming”   feat: implement i...
459
  
5652f5e0   “wangming”   refactor: update ...
460
461
462
463
464
465
466
467
468
469
              // 计算 Content-MD5(请求体的 MD5,然后 Base64
              string contentMd5;
              using (var md5 = MD5.Create())
              {
                  var hashBytes = md5.ComputeHash(contentBytes);
                  contentMd5 = Convert.ToBase64String(hashBytes);
              }
  
              // 生成请求时间(RFC 1123 格式)
              var dateStr = DateTime.UtcNow.ToString("r");
e486fd72   “wangming”   feat: implement i...
470
  
5652f5e0   “wangming”   refactor: update ...
471
472
473
474
475
              // 生成签名随机数
              var signatureNonce = Guid.NewGuid().ToString();
  
              // 构建规范化的请求头(x-acs- 开头的头,按字典序排序)
              var acsHeaders = new SortedDictionary<string, string>
e486fd72   “wangming”   feat: implement i...
476
477
478
479
480
481
                  {
                      { "x-acs-signature-method", "HMAC-SHA1" },
                      { "x-acs-signature-nonce", signatureNonce },
                      { "x-acs-signature-version", "1.0" },
                      { "x-acs-version", "2018-05-09" }
                  };
5652f5e0   “wangming”   refactor: update ...
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
  
              // 构建 CanonicalizedHeaders(格式:key:value\n
              var canonicalizedHeaders = new StringBuilder();
              foreach (var header in acsHeaders)
              {
                  canonicalizedHeaders.Append($"{header.Key}:{header.Value}\n");
              }
  
              // 构建 CanonicalizedResource(请求路径)
              var uri = new Uri(url);
              var canonicalizedResource = uri.AbsolutePath;
  
              // 构建待签名字符串
              var stringToSign = new StringBuilder();
              stringToSign.Append("POST\n");  // HTTP-Verb
              stringToSign.Append("application/json\n");  // Accept
              stringToSign.Append($"{contentMd5}\n");  // Content-MD5
              stringToSign.Append("application/json\n");  // Content-Type
              stringToSign.Append($"{dateStr}\n");  // Date
              stringToSign.Append(canonicalizedHeaders.ToString());  // CanonicalizedHeaders
              stringToSign.Append(canonicalizedResource);  // CanonicalizedResource
  
              // 计算签名(HMAC-SHA1
              string signature;
              using (var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(_accessKeySecret)))
              {
                  var signatureBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString()));
                  signature = Convert.ToBase64String(signatureBytes);
              }
  
              // 构建 Authorization 
              var authorization = $"acs {_accessKeyId}:{signature}";
  
              // 添加 Content-MD5 到内容头(Content-MD5 是内容头,不是请求头)
              content.Headers.TryAddWithoutValidation("Content-MD5", contentMd5);
  
              // 创建请求并添加所有必需的请求头
              using (var request = new HttpRequestMessage(HttpMethod.Post, url))
              {
                  request.Content = content;
  
                  // 添加标准 HTTP 
                  request.Headers.Add("Accept", "application/json");
                  request.Headers.Add("Date", dateStr);
  
                  // 添加阿里云协议头
e486fd72   “wangming”   feat: implement i...
528
529
                  foreach (var header in acsHeaders)
                  {
5652f5e0   “wangming”   refactor: update ...
530
                      request.Headers.Add(header.Key, header.Value);
e486fd72   “wangming”   feat: implement i...
531
                  }
5652f5e0   “wangming”   refactor: update ...
532
533
534
535
536
537
  
                  // 添加 Authorization 
                  request.Headers.Add("Authorization", authorization);
  
                  var response = await _httpClient.SendAsync(request);
                  var responseContent = await response.Content.ReadAsStringAsync();
e486fd72   “wangming”   feat: implement i...
538
539
540
541
542
543
544
545
  
                  // 保存原始响应
                  var rawResponse = responseContent;
  
                  if (!response.IsSuccessStatusCode)
                  {
                      // HTTP请求失败,根据配置决定是否通过
                      var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
5652f5e0   “wangming”   refactor: update ...
546
547
548
                      return new ModerationResult
                      {
                          Passed = !failOnError,
e486fd72   “wangming”   feat: implement i...
549
550
551
552
553
554
555
556
557
558
                          Message = $"HTTP请求失败:{response.StatusCode}",
                          RawResponse = rawResponse
                      };
                  }
  
                  // 解析响应
                  var result = JsonConvert.DeserializeObject<JObject>(responseContent);
                  if (result == null)
                  {
                      var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
5652f5e0   “wangming”   refactor: update ...
559
560
561
                      return new ModerationResult
                      {
                          Passed = !failOnError,
e486fd72   “wangming”   feat: implement i...
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
                          Message = "响应解析失败",
                          RawResponse = rawResponse
                      };
                  }
  
                  // 阿里云业务错误:未开通内容安全、无权限等(code 596  success: false
                  var apiCode = result["code"]?.Value<int>();
                  var success = result["success"]?.Value<bool>();
                  var apiMsg = result["msg"]?.ToString() ?? "";
                  if (apiCode == 596 || success == false)
                  {
                      var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
                      var friendlyMsg = apiCode == 596
                          ? "内容安全服务未开通或当前账号无权限,请到阿里云控制台开通「内容安全」并确认当前 AccessKey 有权限"
                          : (apiMsg.Length > 0 ? apiMsg : "审核接口返回失败");
5652f5e0   “wangming”   refactor: update ...
577
578
579
                      return new ModerationResult
                      {
                          Passed = !failOnError,
e486fd72   “wangming”   feat: implement i...
580
581
582
583
584
585
586
587
588
589
590
                          Message = friendlyMsg,
                          RawResponse = rawResponse,
                          Details = result
                      };
                  }
  
                  // 检查审核结果
                  var data = result["data"] as JArray;
                  if (data == null || data.Count == 0)
                  {
                      var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
5652f5e0   “wangming”   refactor: update ...
591
592
593
                      return new ModerationResult
                      {
                          Passed = !failOnError,
e486fd72   “wangming”   feat: implement i...
594
595
596
597
598
599
600
601
602
603
                          Message = "审核结果数据为空",
                          RawResponse = rawResponse,
                          Details = result
                      };
                  }
  
                  var taskResult = data[0];
                  var results = taskResult["results"] as JArray;
                  if (results == null || results.Count == 0)
                  {
5652f5e0   “wangming”   refactor: update ...
604
605
606
                      return new ModerationResult
                      {
                          Passed = true,
e486fd72   “wangming”   feat: implement i...
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
                          Message = "没有审核结果,默认通过",
                          RawResponse = rawResponse,
                          Details = taskResult
                      };
                  }
  
                  // 检查所有场景的审核结果
                  var blockScenes = new List<string>();
                  var allSuggestions = new List<object>();
                  foreach (var item in results)
                  {
                      var suggestion = item["suggestion"]?.ToString();
                      var scene = item["scene"]?.ToString();
                      allSuggestions.Add(new
                      {
                          scene = scene,
                          suggestion = suggestion,
                          label = item["label"]?.ToString(),
                          rate = item["rate"]?.ToString()
                      });
5652f5e0   “wangming”   refactor: update ...
627
  
e486fd72   “wangming”   feat: implement i...
628
629
630
631
632
633
634
635
                      if (suggestion == "block")
                      {
                          blockScenes.Add(scene ?? "unknown");
                      }
                  }
  
                  if (blockScenes.Count > 0)
                  {
5652f5e0   “wangming”   refactor: update ...
636
637
638
                      return new ModerationResult
                      {
                          Passed = false,
e486fd72   “wangming”   feat: implement i...
639
640
641
642
643
644
645
646
647
648
                          Message = $"图片审核未通过,违规场景:{string.Join(", ", blockScenes)}",
                          RawResponse = rawResponse,
                          Details = new
                          {
                              blockScenes = blockScenes,
                              allResults = allSuggestions
                          }
                      };
                  }
  
5652f5e0   “wangming”   refactor: update ...
649
650
651
                  return new ModerationResult
                  {
                      Passed = true,
e486fd72   “wangming”   feat: implement i...
652
653
654
655
656
657
658
659
660
661
662
                      Message = "图片审核通过",
                      RawResponse = rawResponse,
                      Details = new
                      {
                          allResults = allSuggestions
                      }
                  };
              }
          }
      }
  }