11using Azure ;
2+ using Azure . AI . ContentSafety ;
23using Azure . Storage . Blobs ;
34using Azure . Storage . Blobs . Models ;
45using Evently . Server . Common . Domains . Interfaces ;
56using Evently . Server . Common . Domains . Models ;
7+ using Evently . Server . Common . Extensions ;
68using Microsoft . Extensions . Options ;
79
810namespace Evently . Server . Features . Files . Services ;
@@ -11,6 +13,9 @@ namespace Evently.Server.Features.Files.Services;
1113public sealed class ObjectStorageService ( IOptions < Settings > settings , ILogger < ObjectStorageService > logger ) : IObjectStorageService {
1214 private readonly BlobServiceClient _blobServiceClient =
1315 new ( settings . Value . StorageAccount . AzureStorageConnectionString ) ;
16+ private readonly ContentSafetyClient _contentSafetyClient = new (
17+ endpoint : new Uri ( settings . Value . AzureAiFoundry . ContentSafetyEndpoint ) ,
18+ credential : new AzureKeyCredential ( settings . Value . AzureAiFoundry . ContentSafetyKey ) ) ;
1419
1520 public async Task < Uri > UploadFile ( string containerName , string fileName , BinaryData binaryData ,
1621 string mimeType = "application/octet-stream" ) {
@@ -65,4 +70,23 @@ public async Task<BinaryData> GetFile(string containerName, string fileName) {
6570 BinaryData data = BinaryData . FromBytes ( bytes ) ;
6671 return data ;
6772 }
73+
74+ public async Task < bool > PassesContentModeration ( BinaryData binaryData ) {
75+ ContentSafetyImageData image = new ( binaryData ) ;
76+ AnalyzeImageOptions request = new ( image ) ;
77+ Response < AnalyzeImageResult > response ;
78+ try {
79+ response = await _contentSafetyClient . AnalyzeImageAsync ( request ) ;
80+ } catch ( RequestFailedException ex ) {
81+ logger . LogContentModerationError ( ex . Status . ToString ( ) , ex . ErrorCode ?? "" , ex . Message ) ;
82+ throw ;
83+ }
84+
85+ AnalyzeImageResult result = response . Value ;
86+ int ? score = result . CategoriesAnalysis
87+ . Select ( v => v . Severity )
88+ . Aggregate ( ( a , b ) => a + b )
89+ ?? 0 ;
90+ return score == 0 ;
91+ }
6892}
0 commit comments