@joliegg/moderation 0.3.9 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ const speech_1 = require("@google-cloud/speech");
10
10
  const sharp_1 = __importDefault(require("sharp"));
11
11
  const url_blacklist_json_1 = __importDefault(require("./url-blacklist.json"));
12
12
  const url_shorteners_json_1 = __importDefault(require("./url-shorteners.json"));
13
+ const MAX_IMAGE_SIZE = 5 * 1024 * 1024; // 5MB in bytes for Rekognition limit
13
14
  /**
14
15
  * Moderation Client
15
16
  *
@@ -111,6 +112,29 @@ class ModerationClient {
111
112
  // Download image as binary data
112
113
  buffer = Buffer.from(data, 'binary');
113
114
  }
115
+ // Ensure image is not larger than 5MB (Rekognition limit)
116
+ if (buffer.length > MAX_IMAGE_SIZE) {
117
+ try {
118
+ // Calculate new dimensions to reduce size
119
+ const metadata = await (0, sharp_1.default)(buffer).metadata();
120
+ const { width, height } = metadata;
121
+ if (typeof width !== 'number' || typeof height !== 'number') {
122
+ throw new Error('Invalid image metadata');
123
+ }
124
+ // Calculate the scaling factor
125
+ const scalingFactor = Math.sqrt(MAX_IMAGE_SIZE / buffer.length);
126
+ // Calculate new dimensions
127
+ const newWidth = Math.floor(width * scalingFactor);
128
+ const newHeight = Math.floor(height * scalingFactor);
129
+ const resizedBuffer = await (0, sharp_1.default)(buffer)
130
+ .resize(Math.round(newWidth), Math.round(newHeight))
131
+ .toBuffer();
132
+ buffer = resizedBuffer;
133
+ }
134
+ catch (error) {
135
+ // We can't resize the image. We'll skip the resize and try to process it as is
136
+ }
137
+ }
114
138
  const { ModerationLabels } = await this.rekognitionClient.detectModerationLabels({
115
139
  Image: {
116
140
  Bytes: buffer
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@joliegg/moderation",
3
- "version": "0.3.9",
3
+ "version": "0.4.0",
4
4
  "description": "A set of tools for chat moderation",
5
5
  "author": "Diana Islas Ocampo",
6
6
  "main": "dist/index.js",
package/src/index.ts CHANGED
@@ -17,6 +17,8 @@ type IRecognitionConfig = protos.google.cloud.speech.v1.IRecognitionConfig;
17
17
 
18
18
  type ISpeechRecognitionResult = protos.google.cloud.speech.v1.ISpeechRecognitionResult;
19
19
 
20
+ const MAX_IMAGE_SIZE = 5 * 1024 * 1024; // 5MB in bytes for Rekognition limit
21
+
20
22
  /**
21
23
  * Moderation Client
22
24
  *
@@ -136,6 +138,35 @@ class ModerationClient {
136
138
  buffer = Buffer.from(data, 'binary');
137
139
  }
138
140
 
141
+ // Ensure image is not larger than 5MB (Rekognition limit)
142
+ if (buffer.length > MAX_IMAGE_SIZE) {
143
+ try {
144
+ // Calculate new dimensions to reduce size
145
+ const metadata = await sharp(buffer).metadata();
146
+
147
+ const { width, height } = metadata;
148
+
149
+ if (typeof width !== 'number' || typeof height !== 'number') {
150
+ throw new Error('Invalid image metadata');
151
+ }
152
+
153
+ // Calculate the scaling factor
154
+ const scalingFactor = Math.sqrt(MAX_IMAGE_SIZE / buffer.length);
155
+
156
+ // Calculate new dimensions
157
+ const newWidth = Math.floor(width * scalingFactor);
158
+ const newHeight = Math.floor(height * scalingFactor);
159
+
160
+ const resizedBuffer = await sharp(buffer)
161
+ .resize(Math.round(newWidth), Math.round(newHeight))
162
+ .toBuffer();
163
+
164
+ buffer = resizedBuffer;
165
+ } catch (error) {
166
+ // We can't resize the image. We'll skip the resize and try to process it as is
167
+ }
168
+ }
169
+
139
170
  const { ModerationLabels } = await this.rekognitionClient.detectModerationLabels({
140
171
  Image: {
141
172
  Bytes: buffer