Skip to content

Commit 6aa2e0e

Browse files
author
agrandiere
committed
Merge branch 'dev'
2 parents d656d79 + 6113021 commit 6aa2e0e

File tree

4 files changed

+95
-31
lines changed

4 files changed

+95
-31
lines changed

README.MD

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ Several moderation engines are available for you to choose from (nudity detectio
3333

3434
```javascript
3535
// Detect nudity in an image
36-
sightengine.check(['nudity']).image('http://img09.deviantart.net/2bd0/i/2009/276/c/9/magic_forrest_wallpaper_by_goergen.jpg').then(function(result) {
36+
sightengine.check(['nudity']).set_url('http://img09.deviantart.net/2bd0/i/2009/276/c/9/magic_forrest_wallpaper_by_goergen.jpg').then(function(result) {
3737
// The result of the API
3838
}).catch(function(err) {
3939
// Error
4040
});
4141

4242
// Detect nudity, weapons, alcohol, drugs and faces in an image, along with image properties and type
43-
sightengine.check(['nudity', 'type', 'properties','wad','face']).image('http://img09.deviantart.net/2bd0/i/2009/276/c/9/magic_forrest_wallpaper_by_goergen.jpg').then(function(result) {
43+
sightengine.check(['nudity', 'type', 'properties','wad','face']).set_url('http://img09.deviantart.net/2bd0/i/2009/276/c/9/magic_forrest_wallpaper_by_goergen.jpg').then(function(result) {
4444
// The result of the API
4545
}).catch(function(err) {
4646
// Error
@@ -50,14 +50,31 @@ sightengine.check(['nudity', 'type', 'properties','wad','face']).image('http://i
5050
## Moderate a local image:
5151
```javascript
5252
// Detect nudity in an image
53-
sightengine.check(['nudity']).image('/full/path/to/image.jpg').then(function(result) {
53+
sightengine.check(['nudity']).set_file('/full/path/to/image.jpg').then(function(result) {
5454
// The result of the API
5555
}).catch(function(err) {
5656
// Error
5757
});
5858

5959
// Detect nudity, weapons, alcohol, drugs and faces in an image, along with image properties and type
60-
sightengine.check(['nudity', 'type', 'properties','wad','face']).image('/full/path/to/image.jpg').then(function(result) {
60+
sightengine.check(['nudity', 'type', 'properties','wad','face']).set_file('/full/path/to/image.jpg').then(function(result) {
61+
// The result of the API
62+
}).catch(function(err) {
63+
// Error
64+
});
65+
```
66+
67+
## Moderate a binary image:
68+
```javascript
69+
// Detect nudity in an image
70+
sightengine.check(['nudity']).set_bytes(binary_image).then(function(result) {
71+
// The result of the API
72+
}).catch(function(err) {
73+
// Error
74+
});
75+
76+
// Detect nudity, weapons, alcohol, drugs and faces in an image, along with image properties and type
77+
sightengine.check(['nudity', 'type', 'properties','wad','face']).set_bytes(binary_image).then(function(result) {
6178
// The result of the API
6279
}).catch(function(err) {
6380
// Error

example.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,26 @@ sightengine.feedback('nudity', 'safe', 'https://sightengine.com/assets/img/examp
1010

1111
// moderation image
1212

13-
sightengine.check(['nudity', 'type', 'properties','wad','face']).image('https://sightengine.com/assets/img/examples/example5.jpg').then(function(result) {
13+
sightengine.check(['nudity', 'type', 'properties','wad','face']).set_url('https://sightengine.com/assets/img/examples/example5.jpg').then(function(result) {
1414
console.log(result)
1515
}).catch(function(error) {
1616
console.log(error)
1717
});
1818

19+
sightengine.check(['nudity', 'type', 'properties','wad','face']).set_file('/assets/image.jpg').then(function(result) {
20+
console.log(result)
21+
}).catch(function(error) {
22+
console.log(error)
23+
});
24+
25+
26+
sightengine.check(['nudity', 'type', 'properties','wad','face']).set_bytes(imageBinary).then(function(result) {
27+
console.log(result)
28+
}).catch(function(error) {
29+
console.log(error)
30+
});
31+
32+
1933
// moderation video
2034

2135
sightengine.check(['nudity', 'type', 'properties','wad','face']).video('http://www.quirksmode.org/html5/videos/big_buck_bunny.webm', 'http://requestb.in/1d097l71').then(function(result) {

sightengine.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,48 @@ function makeClient(api_user, api_secret) {
1515
return client;
1616
};
1717

18-
client.image = (image) => {
18+
client.set_url = (imageUrl) => {
19+
const data = { 'models': _models.join(), 'url': imageUrl, 'api_user': apiUser, 'api_secret': apiSecret };
20+
const querystring = encodeQueryData(data);
1921
var url = endpoint + '1.0/check.json';
2022

21-
if (image.indexOf("http://") == 0 || image.indexOf("https://") == 0) {
22-
const data = { 'models': _models.join(), 'url': image, 'api_user': apiUser, 'api_secret': apiSecret };
23-
const querystring = encodeQueryData(data);
23+
return fetch(url + '?' + querystring, { headers: { 'user-agent': 'SE-SDK-NODEJS' + version} }).then((res) => {
24+
return res.json();
25+
}).catch((error) => {
26+
return error.json();
27+
});
28+
};
2429

25-
return fetch(url + '?' + querystring, { headers: { 'user-agent': 'SE-SDK-NODEJS' + version} }).then((res) => {
26-
return res.json();
27-
}).catch((error) => {
28-
return error.json();
29-
});
30-
} else {
31-
var form = new FormData();
30+
client.set_file = (file) => {
31+
var form = new FormData();
32+
var url = endpoint + '1.0/check.json';
3233

33-
form.append('api_user', apiUser);
34-
form.append('api_secret', apiSecret);
35-
form.append('models', _models.join());
36-
form.append('media', fs.createReadStream(image));
34+
form.append('api_user', apiUser);
35+
form.append('api_secret', apiSecret);
36+
form.append('models', _models.join());
37+
form.append('media', fs.createReadStream(file));
3738

38-
return fetch(url, { method: 'POST', body: form, headers: { 'user-agent': 'SE-SDK-NODEJS' + version}}).then(function(res) {
39-
return res.json();
40-
}).catch((error) => {
41-
return error.json();
42-
});
43-
}
39+
return fetch(url, { method: 'POST', body: form, headers: { 'user-agent': 'SE-SDK-NODEJS' + version}}).then(function(res) {
40+
return res.json();
41+
}).catch((error) => {
42+
return error.json();
43+
});
44+
};
45+
46+
client.set_bytes = (binaryImage) => {
47+
const form = new FormData();
48+
var url = endpoint + '1.0/check.json';
49+
50+
form.append('api_user', apiUser);
51+
form.append('api_secret', apiSecret);
52+
form.append('models', _models.join());
53+
form.append('media', binaryImage);
54+
55+
return fetch(url, { method: 'POST', body: form, headers: { 'user-agent': 'SE-SDK-NODEJS' + version}}).then(function(res) {
56+
return res.json();
57+
}).catch((error) => {
58+
return error.json();
59+
});
4460
};
4561

4662
client.video = (video, callback) => {

tests.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var sightengine = require('./sightengine')('1234','test');
22
var assert = require('assert');
3+
var fs = require('fs');
34

45
describe('test feedback', function() {
56
it('should return success', function() {
@@ -18,31 +19,47 @@ describe('test feedback', function() {
1819

1920
describe('test image moderation', function() {
2021
it('should return success', function() {
21-
sightengine.check(['nudity', 'type', 'properties','wad','face', 'celebrities']).image('https://sightengine.com/assets/img/examples/example5.jpg').then(function(result) {
22+
sightengine.check(['nudity', 'type', 'properties','wad','face', 'celebrities']).set_url('https://sightengine.com/assets/img/examples/example5.jpg').then(function(result) {
2223
assert.equal('success', result.status);
2324
})
2425
});
2526

2627
it('should return success', function() {
27-
sightengine.check(['nudity']).image('https://sightengine.com/assets/img/examples/example5.jpg').then(function(result) {
28+
sightengine.check(['nudity']).set_url('https://sightengine.com/assets/img/examples/example5.jpg').then(function(result) {
2829
assert.equal('success', result.status);
2930
})
3031
});
3132

3233
it('should return success', function() {
33-
sightengine.check(['nudity', 'type', 'properties','wad','face', 'celebrities']).image('/assets/image.jpg').then(function(result) {
34+
sightengine.check(['nudity', 'type', 'properties','wad','face', 'celebrities']).set_file('/assets/image.jpg').then(function(result) {
3435
assert.equal('success', result.status);
3536
})
3637
});
3738

3839
it('should return success', function() {
39-
sightengine.check(['nudity']).image('/assets/image.jpg').then(function(result) {
40+
sightengine.check(['nudity']).set_file('/assets/image.jpg').then(function(result) {
41+
assert.equal('success', result.status);
42+
})
43+
});
44+
45+
it('should return success', function() {
46+
var binaryImage = fs.createReadStream('/assets/image.jpg');
47+
48+
sightengine.check(['nudity']).set_bytes(binaryImage).then(function(result) {
49+
assert.equal('success', result.status);
50+
})
51+
});
52+
53+
it('should return success', function() {
54+
var binaryImage = fs.createReadStream('/assets/image.jpg');
55+
56+
sightengine.check(['nudity', 'type', 'properties','wad','face', 'celebrities']).set_bytes(binaryImage).then(function(result) {
4057
assert.equal('success', result.status);
4158
})
4259
});
4360

4461
it('should return error', function() {
45-
sightengine.check(['nudity', 'type', 'properties','wad','face']).image('https://sightengine.com/assets/img/examples/example99999.jpg').then(function(result) {
62+
sightengine.check(['nudity', 'type', 'properties','wad','face']).set_url('https://sightengine.com/assets/img/examples/example99999.jpg').then(function(result) {
4663
assert.equal('failure', result.status);
4764
assert.equal('media_error', result.error.type);
4865
})

0 commit comments

Comments
 (0)