Skip to content

Commit 1d4f573

Browse files
author
doripjonov
committed
fixed blob related issue
1 parent d6cff10 commit 1d4f573

File tree

6 files changed

+49
-37
lines changed

6 files changed

+49
-37
lines changed

endpoints/common_endpoints.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* permissions and limitations under the License.
1515
*/
1616
import axios from 'axios';
17+
import fs from 'fs';
1718
import FormData from 'form-data';
1819

1920
// Collection of common endpoints that used by almost all services

endpoints/verification_endpoints.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,13 @@ const verification_endpoints = {
110110
path_is_relative[0] = "target_image";
111111
path_is_relative[1] = target_image_path;
112112
}else{
113-
path_is_url = "target_image";
113+
path_is_url[0] = "target_image";
114114
path_is_url[1] = target_image_path;
115115

116-
path_is_relative = "source_image";
116+
path_is_relative[0] = "source_image";
117117
path_is_relative[1] = source_image_path;
118118
}
119-
120-
bodyFormData.append(path_is_relative[0], fs.createReadStream(path_is_relative[1]), { knownLength: fs.statSync(target_image_path).size });
119+
bodyFormData.append(path_is_relative[0], fs.createReadStream(path_is_relative[1]), { knownLength: fs.statSync(path_is_relative[1]).size });
121120

122121
return new Promise( async (resolve, reject) => {
123122
await axios.get(path_is_url[1], { responseType: 'stream' })
@@ -245,7 +244,7 @@ const verification_endpoints = {
245244
path_is_relative[1] = source_image_path;
246245
}
247246

248-
bodyFormData.append(path_is_relative[0], fs.createReadStream(path_is_relative[1]), { knownLength: fs.statSync(target_image_path).size });
247+
bodyFormData.append(path_is_relative[0], fs.createReadStream(path_is_relative[1]), { knownLength: fs.statSync(path_is_relative[1]).size });
249248
bodyFormData.append(path_is_blob[0], path_is_blob[1], 'example.jpg');
250249

251250
return new Promise( async (resolve, reject) => {

functions/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ const common_functions = {
3737
return isUrl;
3838
},
3939

40+
/**
41+
* Check whether string is relative path or not
42+
* @param {String} path
43+
* @returns
44+
*/
45+
isPathRelative(path) {
46+
if(typeof path !== 'string') return false
47+
let isAbsolute = /^(?:\/|[a-z]+:\/\/)/.test(path)
48+
return !isAbsolute;
49+
},
50+
4051
/**
4152
* Add extra options to url
4253
* @param {String} url

services/detection_service.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DetectionService {
3333
* @returns
3434
*/
3535
detect(image_path, localOptions){
36-
const { get_full_url, add_options_to_url, isUrl } = common_functions;
36+
const { get_full_url, add_options_to_url, isUrl, isPathRelative } = common_functions;
3737
const { upload_blob, upload_path, upload_url } = common_endpoints;
3838
// add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
3939
// add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
@@ -59,16 +59,16 @@ class DetectionService {
5959
.catch(error => {
6060
reject(error)
6161
})
62-
}else if(image_path instanceof Blob) {
63-
upload_blob(image_path, url, this.key)
62+
}else if(isPathRelative(image_path)) {
63+
upload_path(image_path, url, this.key)
6464
.then(response => {
6565
resolve(response.data)
6666
})
6767
.catch(error => {
6868
reject(error)
6969
})
7070
}else {
71-
upload_path(image_path, url, this.key)
71+
upload_blob(image_path, url, this.key)
7272
.then(response => {
7373
resolve(response.data)
7474
})

services/recognition_service.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class RecognitionService {
3333
* @returns {Promise}
3434
*/
3535
recognize(image_path, options){
36-
const{ get_full_url, add_options_to_url, isUrl } = common_functions;
36+
const{ get_full_url, add_options_to_url, isUrl, isPathRelative } = common_functions;
3737
const { upload_blob, upload_path, upload_url } = common_endpoints;
3838
// add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
3939
// add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
@@ -61,16 +61,16 @@ class RecognitionService {
6161
.catch(error => {
6262
reject(error)
6363
})
64-
}else if(image_path instanceof Blob){
65-
upload_blob(image_path, url, this.key)
64+
}else if(isPathRelative(image_path)){
65+
upload_path(image_path, url, this.key)
6666
.then(response => {
6767
resolve(response.data)
6868
})
6969
.catch(error => {
7070
reject(error)
7171
})
7272
}else {
73-
upload_path(image_path, url, this.key)
73+
upload_blob(image_path, url, this.key)
7474
.then(response => {
7575
resolve(response.data)
7676
})
@@ -87,7 +87,7 @@ class RecognitionService {
8787
* @returns {Object}
8888
*/
8989
getFaceCollection(){
90-
const{ get_full_url, add_options_to_url, isUrl } = common_functions;
90+
const{ get_full_url, add_options_to_url, isUrl, isPathRelative } = common_functions;
9191
const { upload_blob, upload_path, upload_url } = common_endpoints;
9292

9393
let url = get_full_url(this.base_url, this.server, this.port)
@@ -138,16 +138,17 @@ class RecognitionService {
138138
.catch(error => {
139139
reject(error)
140140
})
141-
}else if(image_path instanceof Blob){
142-
upload_blob(image_path, url, key)
141+
}else if(isPathRelative(image_path)){
142+
upload_path(image_path, url, key)
143143
.then(response => {
144144
resolve(response.data)
145145
})
146146
.catch(error => {
147147
reject(error)
148148
})
149+
149150
}else {
150-
upload_path(image_path, url, key)
151+
upload_blob(image_path, url, key)
151152
.then(response => {
152153
resolve(response.data)
153154
})
@@ -190,24 +191,23 @@ class RecognitionService {
190191
.catch(error => {
191192
reject(error)
192193
})
193-
}else if(image_path instanceof Blob){
194-
upload_blob(image_path, url, this.key)
194+
}else if(isPathRelative(image_path)){
195+
upload_path(image_path, url, key)
195196
.then(response => {
196197
resolve(response.data)
197198
})
198199
.catch(error => {
199200
reject(error)
200201
})
201202
}else {
202-
upload_path(image_path, url, key)
203+
upload_blob(image_path, url, this.key)
203204
.then(response => {
204205
resolve(response.data)
205206
})
206207
.catch(error => {
207208
reject(error)
208209
})
209210
}
210-
211211
})
212212
},
213213

services/verification_service.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class VerificationService {
2727
}
2828

2929
verify(source_image_path, target_image_path, options){
30-
const { get_full_url, add_options_to_url, isUrl } = common_functions;
30+
const { get_full_url, add_options_to_url, isUrl, isPathRelative } = common_functions;
3131
// add extra parameter(s) name with true value if it is referenced in API documentation for particular endpoint
3232
// add_options_to_url() adds this parameter to url if user passes some value as option otherwise function ignores this parameter
3333
let required_url_parameters = {
@@ -44,8 +44,8 @@ class VerificationService {
4444
let isSourceImageUrl = isUrl(source_image_path);
4545
let isTargetImageUrl = isUrl(target_image_path);
4646

47-
let isSourceBlob = source_image_path instanceof Blob;
48-
let isTargetBlob = target_image_path instanceof Blob;
47+
let isSourceRelativePath = isPathRelative(source_image_path);
48+
let isTargetRelativePath = isPathRelative(target_image_path);
4949

5050
return new Promise((resolve, reject) => {
5151
if(isSourceImageUrl){
@@ -57,42 +57,43 @@ class VerificationService {
5757
.catch(error => {
5858
reject(error)
5959
})
60-
}else if(isTargetBlob){
61-
verification_endpoints.url_blob_request(source_image_path, isSourceImageUrl, target_image_path, url, this.key)
60+
}else if(isTargetRelativePath){
61+
verification_endpoints.one_url_request(source_image_path, isSourceImageUrl, target_image_path, url, this.key)
6262
.then(response => {
6363
resolve(response.data)
6464
})
6565
.catch(error => {
6666
reject(error)
6767
})
6868
}else {
69-
verification_endpoints.one_url_request(source_image_path, isSourceImageUrl, target_image_path, url, this.key)
69+
verification_endpoints.url_blob_request(source_image_path, isSourceImageUrl, target_image_path, url, this.key)
7070
.then(response => {
7171
resolve(response.data)
7272
})
7373
.catch(error => {
7474
reject(error)
7575
})
7676
}
77-
}else if(isSourceBlob){
77+
}else if(isSourceRelativePath){
7878
if(isTargetImageUrl){
79-
verification_endpoints.url_blob_request(source_image_path, isSourceImageUrl, target_image_path, url, this.key)
79+
console.log("hey I'm here", isSourceImageUrl)
80+
verification_endpoints.one_url_request(source_image_path, isSourceImageUrl, target_image_path, url, this.key)
8081
.then(response => {
8182
resolve(response.data)
8283
})
8384
.catch(error => {
8485
reject(error)
8586
})
86-
}else if(isTargetBlob){
87-
verification_endpoints.both_blob_request(source_image_path, target_image_path, url, this.key)
87+
}else if(isTargetRelativePath){
88+
verification_endpoints.verify_face_request(source_image_path, target_image_path, url, this.key)
8889
.then(response => {
8990
resolve(response.data)
9091
})
9192
.catch(error => {
9293
reject(error)
9394
})
9495
}else {
95-
verification_endpoints.one_blob_request(source_image_path, isSourceBlob, target_image_path, url, this.key)
96+
verification_endpoints.one_blob_request(source_image_path, false, target_image_path, url, this.key)
9697
.then(response => {
9798
resolve(response.data)
9899
})
@@ -102,23 +103,23 @@ class VerificationService {
102103
}
103104
}else {
104105
if(isTargetImageUrl){
105-
verification_endpoints.one_url_request(source_image_path, isSourceImageUrl, target_image_path, url, this.key)
106+
verification_endpoints.url_blob_request(source_image_path, isSourceImageUrl, target_image_path, url, this.key)
106107
.then(response => {
107108
resolve(response.data)
108109
})
109110
.catch(error => {
110111
reject(error)
111112
})
112-
}else if(isTargetBlob){
113-
verification_endpoints.one_blob_request(source_image_path, target_image_path, url, this.key)
113+
}else if(isTargetRelativePath){
114+
verification_endpoints.one_blob_request(source_image_path, true, target_image_path, url, this.key)
114115
.then(response => {
115116
resolve(response.data)
116117
})
117118
.catch(error => {
118119
reject(error)
119-
})
120+
})
120121
}else {
121-
verification_endpoints.verify_face_request(source_image_path, target_image_path, url, this.key)
122+
verification_endpoints.both_blob_request(source_image_path, target_image_path, url, this.key)
122123
.then(response => {
123124
resolve(response.data)
124125
})

0 commit comments

Comments
 (0)