-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaws.ts
67 lines (60 loc) · 1.76 KB
/
aws.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Rekognition } from 'aws-sdk';
const rekognition = new Rekognition({
region: process.env.region,
accessKeyId: process.env.accessKeyId,
secretAccessKey: process.env.secretAccessKey,
});
// update the allowed labels
// refer https://docs.aws.amazon.com/rekognition/latest/dg/moderation.html
const allowedLabels = [
'Gambling',
'Tobacco',
'Tobacco Products',
'Smoking',
'Suggestive',
'Male Swimwear Or Underwear',
'Female Swimwear Or Underwear',
];
/**
* Validate image using AWS Rekognition
* @param imageUrl - image URL
* @returns if the image is unsafe and the key
*/
export const validateImage = async (imageUrl: string) => {
const splitted = imageUrl.split('/');
const imageName = splitted[splitted.length - 1];
const { ModerationLabels = [] } = await rekognition
.detectModerationLabels({
Image: {
S3Object: {
Bucket: process.env.bucketName,
Name: imageName,
},
},
MinConfidence: 90,
})
.promise();
// const dirtyLabels =
// ModerationLabels?.filter(
// (label) =>
// label.ParentName !== '' &&
// label.Name !== '' &&
// !(
// allowedLabels.includes(label.Name!) ||
// allowedLabels.includes(label.ParentName!)
// ),
// ) ?? [];
// return { unsafe: dirtyLabels.length !== 0, key: imageName };
return {
unsafe: shouldDelete(ModerationLabels),
key: imageName,
};
};
function shouldDelete(images: Rekognition.ModerationLabels) {
if (images.length === 0) return false;
return images.some((label) => {
if (label.ParentName === '' && label.Name === 'Suggestive') return false;
if (allowedLabels.includes(label.Name!)) return false;
return !allowedLabels.includes(label.ParentName!);
});
}