Skip to content

Commit 7734f96

Browse files
author
agrandiere
committed
added set_url, set_file, set_bytes
1 parent 45973af commit 7734f96

File tree

3 files changed

+74
-27
lines changed

3 files changed

+74
-27
lines changed

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)