1010namespace Evently . Server . Features . Files . Services ;
1111
1212// Based on https://tinyurl.com/5pam66xn
13- public sealed class ObjectStorageService ( IOptions < Settings > settings , ILogger < ObjectStorageService > logger ) : IObjectStorageService {
14- private readonly BlobServiceClient _blobServiceClient =
15- 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 ) ) ;
13+ public sealed class ObjectStorageService : IObjectStorageService {
14+ private readonly BlobServiceClient _blobServiceClient ;
15+ private readonly ContentSafetyClient ? _contentSafetyClient ;
16+ private readonly ILogger < ObjectStorageService > _logger ;
17+
18+ public ObjectStorageService ( IOptions < Settings > settings , ILogger < ObjectStorageService > logger ) {
19+ _logger = logger ;
20+ _blobServiceClient =
21+ new BlobServiceClient ( settings . Value . StorageAccount . AzureStorageConnectionString ) ;
22+
23+ try {
24+ _contentSafetyClient = new ContentSafetyClient (
25+ endpoint : new Uri ( settings . Value . AzureAiFoundry . ContentSafetyEndpoint ) ,
26+ credential : new AzureKeyCredential ( settings . Value . AzureAiFoundry . ContentSafetyKey ) ) ;
27+ } catch ( Exception ex ) {
28+ // silence the error
29+ _logger . LogError ( "error creating content safety client: {message}. Content moderation skipped." , ex . Message ) ;
30+ }
31+ }
1932
2033 public async Task < Uri > UploadFile ( string containerName , string fileName , BinaryData binaryData ,
2134 string mimeType = "application/octet-stream" ) {
@@ -63,7 +76,7 @@ public async Task<BinaryData> GetFile(string containerName, string fileName) {
6376 try {
6477 await blobClient . DownloadToAsync ( ms ) ;
6578 } catch ( Exception ex ) {
66- logger . LogError ( "error getting file: {}" , ex . Message ) ;
79+ _logger . LogError ( "error getting file: {}" , ex . Message ) ;
6780 }
6881
6982 byte [ ] bytes = ms . ToArray ( ) ;
@@ -72,21 +85,25 @@ public async Task<BinaryData> GetFile(string containerName, string fileName) {
7285 }
7386
7487 public async Task < bool > PassesContentModeration ( BinaryData binaryData ) {
88+ if ( _contentSafetyClient is null ) {
89+ return true ;
90+ }
91+
7592 ContentSafetyImageData image = new ( binaryData ) ;
7693 AnalyzeImageOptions request = new ( image ) ;
7794 Response < AnalyzeImageResult > response ;
7895 try {
7996 response = await _contentSafetyClient . AnalyzeImageAsync ( request ) ;
8097 } catch ( RequestFailedException ex ) {
81- logger . LogContentModerationError ( ex . Status . ToString ( ) , ex . ErrorCode ?? "" , ex . Message ) ;
98+ _logger . LogContentModerationError ( ex . Status . ToString ( ) , ex . ErrorCode ?? "" , ex . Message ) ;
8299 throw ;
83100 }
84101
85102 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 ;
103+ int dangerScore = result . CategoriesAnalysis
104+ . Select ( v => v . Severity ?? 0 )
105+ . DefaultIfEmpty ( 0 )
106+ . Aggregate ( ( a , b ) => a + b ) ;
107+ return dangerScore == 0 ;
91108 }
92109}
0 commit comments