Skip to content

Commit c9d636b

Browse files
committed
chore: image moderation
1 parent 3f458b9 commit c9d636b

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

src/Evently.Server/Common/Domains/Interfaces/IObjectStorageService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ public interface IObjectStorageService {
1010
Task<Uri> GetFileUri(string containerName, string fileName);
1111
Task<BinaryData> GetFile(string containerName, string fileName);
1212
Task<bool> IsFileExists(string containerName, string fileName);
13-
Task<bool> IsContentSafe(BinaryData binaryData);
13+
Task<bool> PassesContentModeration(BinaryData binaryData);
1414
}

src/Evently.Server/Evently.Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Azure.AI.ContentSafety" Version="1.0.0" />
15+
<PackageReference Include="Azure.AI.ContentSafety" Version="1.0.0"/>
1616
<PackageReference Include="Azure.Storage.Blobs" Version="12.25.0"/>
1717
<PackageReference Include="FluentValidation" Version="12.0.0"/>
1818
<PackageReference Include="HtmlRenderer.PdfSharp" Version="1.5.0.6"/>

src/Evently.Server/Features/Files/Services/ObjectStorageService.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public sealed class ObjectStorageService(IOptions<Settings> settings, ILogger<Ob
1313
private readonly BlobServiceClient _blobServiceClient =
1414
new(settings.Value.StorageAccount.AzureStorageConnectionString);
1515
private readonly ContentSafetyClient _contentSafetyClient = new(
16-
endpoint: new Uri(settings.Value.AzureAiFoundry.ContentSafetyKey),
17-
credential: new AzureKeyCredential(settings.Value.AzureAiFoundry.ContentSafetyEndpoint));
18-
16+
endpoint: new Uri(settings.Value.AzureAiFoundry.ContentSafetyEndpoint),
17+
credential: new AzureKeyCredential(settings.Value.AzureAiFoundry.ContentSafetyKey));
18+
1919
public async Task<Uri> UploadFile(string containerName, string fileName, BinaryData binaryData,
2020
string mimeType = "application/octet-stream") {
2121
BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
@@ -70,7 +70,7 @@ public async Task<BinaryData> GetFile(string containerName, string fileName) {
7070
return data;
7171
}
7272

73-
public async Task<bool> IsContentSafe(BinaryData binaryData) {
73+
public async Task<bool> PassesContentModeration(BinaryData binaryData) {
7474
ContentSafetyImageData image = new(binaryData);
7575
AnalyzeImageOptions request = new(image);
7676
Response<AnalyzeImageResult> response;
@@ -80,11 +80,12 @@ public async Task<bool> IsContentSafe(BinaryData binaryData) {
8080
Console.WriteLine("Analyze image failed.\nStatus code: {0}, Error code: {1}, Error message: {2}", ex.Status, ex.ErrorCode, ex.Message);
8181
throw;
8282
}
83-
84-
AnalyzeImageResult value = response.Value;
85-
if (value.CategoriesAnalysis.Count == 0) {
86-
return false;
87-
}
88-
return value.CategoriesAnalysis[0].Severity == 0;
83+
84+
AnalyzeImageResult result = response.Value;
85+
int? score = result.CategoriesAnalysis
86+
.Select(v => v.Severity)
87+
.Aggregate((a, b) => a + b)
88+
?? 0;
89+
return result.CategoriesAnalysis.Count != 0 && score == 0;
8990
}
9091
}

src/Evently.Server/Features/Gatherings/Controllers/GatheringsController.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ public async Task<ActionResult<Gathering>> DeleteGathering(long gatheringId) {
115115
private async Task<Uri> UploadCoverImage(long gatheringId, IFormFile coverImg) {
116116
string fileName = $"gatherings/{gatheringId}/cover-image{Path.GetExtension(coverImg.FileName)}";
117117
BinaryData binaryData = await coverImg.ToBinaryData();
118+
bool isContentSafe = await objectStorageService.PassesContentModeration(binaryData);
119+
if (!isContentSafe) {
120+
return new Uri(string.Empty, UriKind.RelativeOrAbsolute);
121+
}
118122
return await objectStorageService.UploadFile(_containerName,
119123
fileName,
120124
binaryData,

src/evently.client/src/lib/services/gathering-service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ function toFormData(gatheringDto: GatheringReqDto, coverImg?: File | null): Form
7575
formData.set(key, value);
7676
}
7777
}
78+
7879
formData.delete("gatheringCategoryDetails");
80+
if (gatheringDto.gatheringCategoryDetails.length === 0) {
81+
formData.set("gatheringCategoryDetails", "[]");
82+
}
7983

8084
for (let i = 0; i < gatheringDto.gatheringCategoryDetails.length; i++) {
8185
const detail: GatheringCategoryDetailReqDto = gatheringDto.gatheringCategoryDetails[i];

0 commit comments

Comments
 (0)