Skip to content

Commit 4957b51

Browse files
authored
Merge pull request #132 from BuildFire/handle-stock-images
fix(generate ai) : replace unsplash api with buildfire stock images
2 parents 1dd5d6f + 4e02a63 commit 4957b51

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

control/content/app.services.js

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,17 @@
221221
itemsList = data.data.items;
222222
//Check image URLs
223223
let items = itemsList.map((item, i) => {
224-
return new Promise((resolve, reject) => {
225-
elimanateNotFoundImages(item.listImage).then(res => {
226-
if (res.isValid) {
227-
item.listImage = res.newURL;
228-
item.order = i;
229-
orderedItems.push(item);
230-
resolve(item);
231-
} else {
232-
reject('image URL not valid');
233-
}
234-
})
235-
})
236-
})
224+
return elimanateNotFoundImages(item.listImage).then(res => {
225+
if (res.isValid) {
226+
item.listImage = res.newURL;
227+
item.order = i;
228+
orderedItems.push(item);
229+
return item;
230+
} else {
231+
throw new Error('image URL not valid');
232+
}
233+
});
234+
});
237235

238236
Promise.allSettled(items).then(results => {
239237
itemsList = [];
@@ -366,24 +364,25 @@
366364
}
367365

368366
let elimanateNotFoundImages = function(url) {
369-
const optimisedURL = url.replace('1080x720', '100x100');
370367
return new Promise((resolve) => {
371-
if (url.includes("http")){
368+
if (url.includes("http")) {
372369
const xhr = new XMLHttpRequest();
373-
xhr.open("GET", optimisedURL);
370+
xhr.open("GET", url);
374371
xhr.onerror = (error) => {
375-
console.warn('provided URL is not a valid image', error);
372+
console.warn('Provided URL is not a valid image', error);
376373
resolve({isValid: false, newURL: null});
377-
}
374+
};
378375
xhr.onload = () => {
379376
if (xhr.responseURL.includes('source-404') || xhr.status == 404) {
380-
return resolve({isValid: false ,newURL: null});
377+
resolve({isValid: false, newURL: null});
381378
} else {
382-
return resolve({isValid: true, newURL: xhr.responseURL.replace('h=100', 'h=720').replace('w=100', 'w=1080') });
379+
resolve({isValid: true, newURL: xhr.responseURL});
383380
}
384381
};
385382
xhr.send();
386-
} else resolve(false);
383+
} else {
384+
resolve({isValid: false, newURL: null});
385+
}
387386
});
388387
};
389388

@@ -428,27 +427,27 @@
428427
}
429428

430429
return {
431-
initStateSeeder: function() {
432-
stateSeederInstance = new buildfire.components.aiStateSeeder({
433-
generateOptions: {
434-
userMessage: `Generate a sample of redeemable items for a new [business-type].`,
435-
maxRecords: 5,
436-
systemMessage:
437-
'listImage URL related to title and the list type. use source.unsplash.com for image URL, URL should not have premium_photo or source.unsplash.com/random, cost to redeem which is a number greater than zero and less than 100, return description as HTML.',
438-
jsonTemplate: generateJSONTemplate,
439-
callback: handleAIReq.bind(this, false),
440-
hintText: 'Replace values between brackets to match your requirements.',
441-
},
442-
importOptions: {
443-
jsonTemplate: importJSONTemplate,
444-
sampleCSV: "Hotel Voucher, 50, 10, Redeem this voucher for a one-night stay at a luxurious hotel of your choice, https://source.unsplash.com/featured/?hotel\nFlight Upgrade, 30, 15, Upgrade your economy class ticket to business class, https://source.unsplash.com/featured/?flight\nCity Tour, 20, 5, Explore the city with a guided tour that covers all the major attractions and landmarks, https://source.unsplash.com/featured/?city\nAdventure Activity, 80, 30, Embark on an adrenaline-pumping adventure activity such as bungee jumping or skydiving, https://source.unsplash.com/featured/?adventure",
445-
maxRecords: 15,
446-
hintText: 'Each row should start with a loyalty item title, cost to redeem, points earned, description, and image URL.',
447-
systemMessage: ``,
448-
callback: handleAIReq.bind(this, true),
449-
},
450-
}).smartShowEmptyState();
451-
},
430+
initStateSeeder: function() {
431+
stateSeederInstance = new buildfire.components.aiStateSeeder({
432+
generateOptions: {
433+
userMessage: `Generate a sample of redeemable items for a new [business-type].`,
434+
maxRecords: 5,
435+
systemMessage:
436+
'listImage URL related to the title or the list type. Use https://app.buildfire.com/api/stockImages/?topic={topic}&imageType=medium, A maximum of 2 comma-separated topics can be used for each URL. cost to redeem which is a number greater than zero and less than 100, return description as HTML.',
437+
jsonTemplate: generateJSONTemplate,
438+
callback: handleAIReq.bind(this, false),
439+
hintText: 'Replace values between brackets to match your requirements.',
440+
},
441+
importOptions: {
442+
jsonTemplate: importJSONTemplate,
443+
sampleCSV: "Hotel Voucher, 50, 10, Redeem this voucher for a one-night stay at a luxurious hotel of your choice, https://app.buildfire.com/api/stockImages/?topic=hotel&imageType=medium\nFlight Upgrade, 30, 15, Upgrade your economy class ticket to business class, https://app.buildfire.com/api/stockImages/?topic=flight&imageType=medium\nCity Tour, 20, 5, Explore the city with a guided tour that covers all the major attractions and landmarks, https://app.buildfire.com/api/stockImages/?topic=city&imageType=medium\nAdventure Activity, 80, 30, Embark on an adrenaline-pumping adventure activity such as bungee jumping or skydiving, https://app.buildfire.com/api/stockImages/?topic=adventure&imageType=medium",
444+
maxRecords: 15,
445+
hintText: 'Each row should start with a loyalty item title, cost to redeem, points earned, description, and image URL.',
446+
systemMessage: ``,
447+
callback: handleAIReq.bind(this, true),
448+
},
449+
}).smartShowEmptyState();
450+
},
452451
}
453452
}])
454453
})(window.angular, window.buildfire);

0 commit comments

Comments
 (0)