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
|
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>
|
7606a6ad
“wangming”
fix: update produ...
|
35
36
37
38
39
40
41
42
43
|
/// FailOnError=false 自动放行时,同步上传场景用简短说明;队列异步审核(backgroundQueue=true)始终返回接口侧原文便于落库
/// </summary>
private static string PassThroughMessage(bool failOnError, string strictFailureDescription, bool backgroundQueue = false)
{
if (failOnError || backgroundQueue) return strictFailureDescription;
return "内容审核未执行,已自动通过(未开通内容安全或未配置密钥;开通并配置后将显示机器审核结论)";
}
/// <summary>
|
e486fd72
“wangming”
feat: implement i...
|
44
45
46
47
48
49
|
/// 初始化图片审核服务
/// </summary>
public ImageModerationService()
{
_httpClient = new HttpClient();
_enabled = App.Configuration["NCC_App:ImageModeration:Enabled"] == "true";
|
5652f5e0
“wangming”
refactor: update ...
|
50
|
_endpoint = App.Configuration["NCC_App:ImageModeration:Endpoint"]
|
e486fd72
“wangming”
feat: implement i...
|
51
|
?? "https://green.cn-shanghai.aliyuncs.com";
|
5652f5e0
“wangming”
refactor: update ...
|
52
|
_region = App.Configuration["NCC_App:ImageModeration:Region"]
|
e486fd72
“wangming”
feat: implement i...
|
53
|
?? "cn-shanghai";
|
5652f5e0
“wangming”
refactor: update ...
|
54
|
|
e486fd72
“wangming”
feat: implement i...
|
55
56
57
58
59
60
|
// 优先使用ImageModeration配置,如果没有则使用AliyunOSS的配置
_accessKeyId = App.Configuration["NCC_App:ImageModeration:AccessKeyId"];
if (string.IsNullOrEmpty(_accessKeyId))
{
_accessKeyId = App.Configuration["NCC_App:AliyunOSS:AccessKeyId"];
}
|
5652f5e0
“wangming”
refactor: update ...
|
61
|
|
e486fd72
“wangming”
feat: implement i...
|
62
63
64
65
66
|
_accessKeySecret = App.Configuration["NCC_App:ImageModeration:AccessKeySecret"];
if (string.IsNullOrEmpty(_accessKeySecret))
{
_accessKeySecret = App.Configuration["NCC_App:AliyunOSS:AccessKeySecret"];
}
|
5652f5e0
“wangming”
refactor: update ...
|
67
|
|
e486fd72
“wangming”
feat: implement i...
|
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
// 判断是否为增强版(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 ...
|
92
93
94
95
|
return new ModerationResult
{
Passed = true,
Message = "审核未启用,直接通过"
|
e486fd72
“wangming”
feat: implement i...
|
96
97
98
99
100
|
};
}
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
{
|
5652f5e0
“wangming”
refactor: update ...
|
101
102
103
104
|
return new ModerationResult
{
Passed = false,
Message = "文件不存在或路径为空"
|
e486fd72
“wangming”
feat: implement i...
|
105
106
107
108
109
110
111
112
113
114
115
116
|
};
}
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 ...
|
117
118
119
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
120
|
Message = PassThroughMessage(failOnError, $"审核异常:{ex.Message}"),
|
e486fd72
“wangming”
feat: implement i...
|
121
122
123
124
125
126
127
128
129
|
RawResponse = ex.ToString()
};
}
}
/// <summary>
/// 图片审核(从字节数组)
/// </summary>
/// <param name="imageBytes">图片字节数组</param>
|
7606a6ad
“wangming”
fix: update produ...
|
130
|
/// <param name="backgroundQueue">为 true 时表示 Hangfire 队列审核,说明文案与落库优先使用接口原文,不走「自动通过」类弱化文案</param>
|
e486fd72
“wangming”
feat: implement i...
|
131
|
/// <returns>审核结果详情</returns>
|
7606a6ad
“wangming”
fix: update produ...
|
132
|
public async Task<ModerationResult> ScanImageAsync(byte[] imageBytes, bool backgroundQueue = false)
|
e486fd72
“wangming”
feat: implement i...
|
133
134
135
|
{
if (!_enabled)
{
|
5652f5e0
“wangming”
refactor: update ...
|
136
137
138
139
|
return new ModerationResult
{
Passed = true,
Message = "审核未启用,直接通过"
|
e486fd72
“wangming”
feat: implement i...
|
140
141
142
143
144
|
};
}
if (imageBytes == null || imageBytes.Length == 0)
{
|
5652f5e0
“wangming”
refactor: update ...
|
145
146
147
148
|
return new ModerationResult
{
Passed = false,
Message = "图片数据为空"
|
e486fd72
“wangming”
feat: implement i...
|
149
150
151
152
153
154
155
|
};
}
// 检查 AccessKey 是否配置
if (string.IsNullOrEmpty(_accessKeyId) || string.IsNullOrEmpty(_accessKeySecret))
{
var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
|
5652f5e0
“wangming”
refactor: update ...
|
156
157
158
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
159
|
Message = PassThroughMessage(failOnError, "图片审核服务未配置 AccessKey,无法调用审核接口", backgroundQueue),
|
e486fd72
“wangming”
feat: implement i...
|
160
161
162
163
164
165
166
167
168
|
RawResponse = "AccessKeyId 或 AccessKeySecret 未配置"
};
}
try
{
// 判断是否为增强版,使用不同的调用方式
if (_isEnhancedVersion)
{
|
7606a6ad
“wangming”
fix: update produ...
|
169
|
return await ScanImageEnhancedAsync(imageBytes, backgroundQueue);
|
e486fd72
“wangming”
feat: implement i...
|
170
171
172
|
}
else
{
|
7606a6ad
“wangming”
fix: update produ...
|
173
|
return await ScanImageLegacyAsync(imageBytes, backgroundQueue);
|
e486fd72
“wangming”
feat: implement i...
|
174
175
176
177
178
179
|
}
}
catch (Exception ex)
{
// 审核异常时,根据配置决定是否通过
var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
|
5652f5e0
“wangming”
refactor: update ...
|
180
181
182
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
183
|
Message = PassThroughMessage(failOnError, $"审核异常:{ex.Message}", backgroundQueue),
|
e486fd72
“wangming”
feat: implement i...
|
184
185
186
187
188
189
|
RawResponse = ex.ToString()
};
}
}
/// <summary>
|
891ea9b6
“wangming”
refactor: update ...
|
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/// 创建增强版内容安全 Client(Green 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...
|
206
|
/// 图片审核(增强版 - green-cip)
|
891ea9b6
“wangming”
refactor: update ...
|
207
|
/// 使用官方 SDK:DescribeUploadToken → 上传临时 OSS → ImageModeration
|
e486fd72
“wangming”
feat: implement i...
|
208
209
|
/// </summary>
/// <param name="imageBytes">图片字节数组</param>
|
7606a6ad
“wangming”
fix: update produ...
|
210
|
/// <param name="backgroundQueue">队列审核时为 true,弱化文案不生效</param>
|
e486fd72
“wangming”
feat: implement i...
|
211
|
/// <returns>审核结果详情</returns>
|
7606a6ad
“wangming”
fix: update produ...
|
212
|
private async Task<ModerationResult> ScanImageEnhancedAsync(byte[] imageBytes, bool backgroundQueue)
|
e486fd72
“wangming”
feat: implement i...
|
213
|
{
|
7606a6ad
“wangming”
fix: update produ...
|
214
|
string Pt(bool fo, string s) => PassThroughMessage(fo, s, backgroundQueue);
|
e486fd72
“wangming”
feat: implement i...
|
215
216
|
try
{
|
891ea9b6
“wangming”
refactor: update ...
|
217
218
219
220
221
222
223
224
225
226
227
|
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...
|
228
229
|
{
var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
|
891ea9b6
“wangming”
refactor: update ...
|
230
231
232
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
233
|
Message = Pt(failOnError, $"获取临时 OSS 凭证失败:{tokenResponse?.Body?.Msg ?? "未知错误"}"),
|
891ea9b6
“wangming”
refactor: update ...
|
234
|
RawResponse = tokenResponse != null ? JsonConvert.SerializeObject(tokenResponse.Body) : "null"
|
e486fd72
“wangming”
feat: implement i...
|
235
236
237
|
};
}
|
891ea9b6
“wangming”
refactor: update ...
|
238
|
var tokenData = tokenResponse.Body.Data;
|
e486fd72
“wangming”
feat: implement i...
|
239
240
241
|
if (tokenData == null)
{
var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
|
891ea9b6
“wangming”
refactor: update ...
|
242
243
244
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
245
|
Message = Pt(failOnError, "临时 OSS 凭证数据为空"),
|
891ea9b6
“wangming”
refactor: update ...
|
246
|
RawResponse = JsonConvert.SerializeObject(tokenResponse.Body)
|
e486fd72
“wangming”
feat: implement i...
|
247
248
249
|
};
}
|
891ea9b6
“wangming”
refactor: update ...
|
250
251
252
253
254
255
256
257
258
259
260
261
262
|
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...
|
263
|
{
|
891ea9b6
“wangming”
refactor: update ...
|
264
265
266
267
|
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...
|
268
269
|
}
|
891ea9b6
“wangming”
refactor: update ...
|
270
271
272
273
274
275
276
277
278
279
280
281
|
// 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...
|
282
|
{
|
891ea9b6
“wangming”
refactor: update ...
|
283
284
285
286
287
288
|
await ossClient.PutObjectAsync(new OSS.Models.PutObjectRequest
{
Bucket = bucketName,
Key = objectName,
Body = bodyStream
}).ConfigureAwait(false);
|
e486fd72
“wangming”
feat: implement i...
|
289
290
291
|
}
// 3. 调用增强版审核接口 ImageModeration
|
e486fd72
“wangming”
feat: implement i...
|
292
293
294
295
296
297
|
var serviceParameters = new Dictionary<string, object>
{
{ "ossBucketName", bucketName },
{ "ossObjectName", objectName },
{ "dataId", Guid.NewGuid().ToString() }
};
|
891ea9b6
“wangming”
refactor: update ...
|
298
299
300
301
302
303
|
// 中国区(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...
|
304
|
{
|
891ea9b6
“wangming”
refactor: update ...
|
305
306
|
Service = serviceName,
ServiceParameters = JsonConvert.SerializeObject(serviceParameters)
|
e486fd72
“wangming”
feat: implement i...
|
307
|
};
|
891ea9b6
“wangming”
refactor: update ...
|
308
309
310
311
312
313
314
315
316
317
|
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...
|
318
319
|
{
var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
|
891ea9b6
“wangming”
refactor: update ...
|
320
321
322
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
323
|
Message = Pt(failOnError, "审核接口无响应"),
|
e486fd72
“wangming”
feat: implement i...
|
324
325
326
327
|
RawResponse = rawResponse
};
}
|
891ea9b6
“wangming”
refactor: update ...
|
328
|
var code = moderationResponse.Body.Code;
|
e486fd72
“wangming”
feat: implement i...
|
329
330
331
|
if (code != 200)
{
var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
|
891ea9b6
“wangming”
refactor: update ...
|
332
333
334
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
335
|
Message = Pt(failOnError, moderationResponse.Body.Msg ?? $"审核接口返回错误:{code}"),
|
e486fd72
“wangming”
feat: implement i...
|
336
|
RawResponse = rawResponse,
|
891ea9b6
“wangming”
refactor: update ...
|
337
|
Details = moderationResponse.Body
|
e486fd72
“wangming”
feat: implement i...
|
338
339
340
|
};
}
|
891ea9b6
“wangming”
refactor: update ...
|
341
|
var data = moderationResponse.Body.Data;
|
e486fd72
“wangming”
feat: implement i...
|
342
343
344
|
if (data == null)
{
var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
|
891ea9b6
“wangming”
refactor: update ...
|
345
346
347
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
348
|
Message = Pt(failOnError, "审核结果数据为空"),
|
e486fd72
“wangming”
feat: implement i...
|
349
|
RawResponse = rawResponse,
|
891ea9b6
“wangming”
refactor: update ...
|
350
|
Details = moderationResponse.Body
|
e486fd72
“wangming”
feat: implement i...
|
351
352
353
|
};
}
|
891ea9b6
“wangming”
refactor: update ...
|
354
355
356
|
var results = data.Result;
var riskLevel = data.RiskLevel ?? "";
|
e486fd72
“wangming”
feat: implement i...
|
357
358
|
if (results == null || results.Count == 0)
{
|
891ea9b6
“wangming”
refactor: update ...
|
359
360
361
|
return new ModerationResult
{
Passed = true,
|
e486fd72
“wangming”
feat: implement i...
|
362
363
364
365
366
367
|
Message = "没有审核结果,默认通过",
RawResponse = rawResponse,
Details = data
};
}
|
e486fd72
“wangming”
feat: implement i...
|
368
369
|
var highRiskLabels = new List<string>();
var allResults = new List<object>();
|
891ea9b6
“wangming”
refactor: update ...
|
370
|
|
e486fd72
“wangming”
feat: implement i...
|
371
372
|
foreach (var item in results)
{
|
891ea9b6
“wangming”
refactor: update ...
|
373
374
375
376
377
|
var label = item.Label ?? "";
var confidence = item.Confidence;
var itemRiskLevel = item.RiskLevel ?? "";
var description = item.Description ?? "";
|
e486fd72
“wangming”
feat: implement i...
|
378
379
380
381
382
383
384
|
allResults.Add(new
{
label = label,
confidence = confidence,
riskLevel = itemRiskLevel,
description = description
});
|
891ea9b6
“wangming”
refactor: update ...
|
385
386
|
if (itemRiskLevel == "high" || (!string.IsNullOrEmpty(label) && !label.StartsWith("nonLabel")))
|
e486fd72
“wangming”
feat: implement i...
|
387
|
highRiskLabels.Add($"{label}({description})");
|
e486fd72
“wangming”
feat: implement i...
|
388
389
390
391
|
}
if (highRiskLabels.Count > 0 || riskLevel == "high")
{
|
891ea9b6
“wangming”
refactor: update ...
|
392
393
394
|
return new ModerationResult
{
Passed = false,
|
e486fd72
“wangming”
feat: implement i...
|
395
396
397
398
399
400
401
402
403
404
405
|
Message = $"图片审核未通过,风险等级:{riskLevel},违规标签:{string.Join(", ", highRiskLabels)}",
RawResponse = rawResponse,
Details = new
{
riskLevel = riskLevel,
highRiskLabels = highRiskLabels,
allResults = allResults
}
};
}
|
891ea9b6
“wangming”
refactor: update ...
|
406
407
408
|
return new ModerationResult
{
Passed = true,
|
e486fd72
“wangming”
feat: implement i...
|
409
410
411
412
413
414
415
416
417
|
Message = "图片审核通过",
RawResponse = rawResponse,
Details = new
{
riskLevel = riskLevel,
allResults = allResults
}
};
}
|
891ea9b6
“wangming”
refactor: update ...
|
418
419
420
421
422
423
424
425
426
|
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,
|
7606a6ad
“wangming”
fix: update produ...
|
427
|
Message = Pt(failOnError, msg),
|
891ea9b6
“wangming”
refactor: update ...
|
428
429
430
|
RawResponse = teaEx.ToString()
};
}
|
e486fd72
“wangming”
feat: implement i...
|
431
432
433
|
catch (Exception ex)
{
var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
|
891ea9b6
“wangming”
refactor: update ...
|
434
435
436
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
437
|
Message = Pt(failOnError, $"增强版审核异常:{ex.Message}"),
|
e486fd72
“wangming”
feat: implement i...
|
438
439
440
441
442
443
|
RawResponse = ex.ToString()
};
}
}
/// <summary>
|
e486fd72
“wangming”
feat: implement i...
|
444
445
446
|
/// 图片审核(老版 - green.xxx)
/// </summary>
/// <param name="imageBytes">图片字节数组</param>
|
7606a6ad
“wangming”
fix: update produ...
|
447
|
/// <param name="backgroundQueue">队列审核时为 true</param>
|
e486fd72
“wangming”
feat: implement i...
|
448
|
/// <returns>审核结果详情</returns>
|
7606a6ad
“wangming”
fix: update produ...
|
449
|
private async Task<ModerationResult> ScanImageLegacyAsync(byte[] imageBytes, bool backgroundQueue)
|
e486fd72
“wangming”
feat: implement i...
|
450
|
{
|
7606a6ad
“wangming”
fix: update produ...
|
451
|
string Pt(bool fo, string s) => PassThroughMessage(fo, s, backgroundQueue);
|
e486fd72
“wangming”
feat: implement i...
|
452
453
|
// 构建请求URL
var url = $"{_endpoint}/green/image/scan";
|
5652f5e0
“wangming”
refactor: update ...
|
454
455
456
457
458
459
|
// 构建请求体
var requestBody = new
{
scenes = new[] { "porn", "terrorism", "ad", "qrcode" },
tasks = new[]
|
e486fd72
“wangming”
feat: implement i...
|
460
|
{
|
e486fd72
“wangming”
feat: implement i...
|
461
462
463
464
465
466
|
new
{
dataId = Guid.NewGuid().ToString(),
url = Convert.ToBase64String(imageBytes)
}
}
|
5652f5e0
“wangming”
refactor: update ...
|
467
|
};
|
e486fd72
“wangming”
feat: implement i...
|
468
|
|
5652f5e0
“wangming”
refactor: update ...
|
469
470
471
472
|
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...
|
473
|
|
5652f5e0
“wangming”
refactor: update ...
|
474
475
476
477
478
479
480
481
482
483
|
// 计算 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...
|
484
|
|
5652f5e0
“wangming”
refactor: update ...
|
485
486
487
488
489
|
// 生成签名随机数
var signatureNonce = Guid.NewGuid().ToString();
// 构建规范化的请求头(x-acs- 开头的头,按字典序排序)
var acsHeaders = new SortedDictionary<string, string>
|
e486fd72
“wangming”
feat: implement i...
|
490
491
492
493
494
495
|
{
{ "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 ...
|
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
// 构建 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...
|
542
543
|
foreach (var header in acsHeaders)
{
|
5652f5e0
“wangming”
refactor: update ...
|
544
|
request.Headers.Add(header.Key, header.Value);
|
e486fd72
“wangming”
feat: implement i...
|
545
|
}
|
5652f5e0
“wangming”
refactor: update ...
|
546
547
548
549
550
551
|
// 添加 Authorization 头
request.Headers.Add("Authorization", authorization);
var response = await _httpClient.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
|
e486fd72
“wangming”
feat: implement i...
|
552
553
554
555
556
557
558
559
|
// 保存原始响应
var rawResponse = responseContent;
if (!response.IsSuccessStatusCode)
{
// HTTP请求失败,根据配置决定是否通过
var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
|
5652f5e0
“wangming”
refactor: update ...
|
560
561
562
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
563
|
Message = Pt(failOnError, $"HTTP请求失败:{response.StatusCode}"),
|
e486fd72
“wangming”
feat: implement i...
|
564
565
566
567
568
569
570
571
572
|
RawResponse = rawResponse
};
}
// 解析响应
var result = JsonConvert.DeserializeObject<JObject>(responseContent);
if (result == null)
{
var failOnError = App.Configuration["NCC_App:ImageModeration:FailOnError"] == "true";
|
5652f5e0
“wangming”
refactor: update ...
|
573
574
575
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
576
|
Message = Pt(failOnError, "响应解析失败"),
|
e486fd72
“wangming”
feat: implement i...
|
577
578
579
580
581
582
583
584
585
586
587
|
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";
|
7606a6ad
“wangming”
fix: update produ...
|
588
|
var strictMsg = apiCode == 596
|
e486fd72
“wangming”
feat: implement i...
|
589
590
|
? "内容安全服务未开通或当前账号无权限,请到阿里云控制台开通「内容安全」并确认当前 AccessKey 有权限"
: (apiMsg.Length > 0 ? apiMsg : "审核接口返回失败");
|
5652f5e0
“wangming”
refactor: update ...
|
591
592
593
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
594
|
Message = Pt(failOnError, strictMsg),
|
e486fd72
“wangming”
feat: implement i...
|
595
596
597
598
599
600
601
602
603
604
|
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 ...
|
605
606
607
|
return new ModerationResult
{
Passed = !failOnError,
|
7606a6ad
“wangming”
fix: update produ...
|
608
|
Message = Pt(failOnError, "审核结果数据为空"),
|
e486fd72
“wangming”
feat: implement i...
|
609
610
611
612
613
614
615
616
617
|
RawResponse = rawResponse,
Details = result
};
}
var taskResult = data[0];
var results = taskResult["results"] as JArray;
if (results == null || results.Count == 0)
{
|
5652f5e0
“wangming”
refactor: update ...
|
618
619
620
|
return new ModerationResult
{
Passed = true,
|
e486fd72
“wangming”
feat: implement i...
|
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
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 ...
|
641
|
|
e486fd72
“wangming”
feat: implement i...
|
642
643
644
645
646
647
648
649
|
if (suggestion == "block")
{
blockScenes.Add(scene ?? "unknown");
}
}
if (blockScenes.Count > 0)
{
|
5652f5e0
“wangming”
refactor: update ...
|
650
651
652
|
return new ModerationResult
{
Passed = false,
|
e486fd72
“wangming”
feat: implement i...
|
653
654
655
656
657
658
659
660
661
662
|
Message = $"图片审核未通过,违规场景:{string.Join(", ", blockScenes)}",
RawResponse = rawResponse,
Details = new
{
blockScenes = blockScenes,
allResults = allSuggestions
}
};
}
|
5652f5e0
“wangming”
refactor: update ...
|
663
664
665
|
return new ModerationResult
{
Passed = true,
|
e486fd72
“wangming”
feat: implement i...
|
666
667
668
669
670
671
672
673
674
675
676
|
Message = "图片审核通过",
RawResponse = rawResponse,
Details = new
{
allResults = allSuggestions
}
};
}
}
}
}
|