Reputation: 4343
I'm looking for a solution where I can provide URL to specific image and then with Javascript I retrieve data of that image. I alread found that I can't just pull file through, so maybe byte array or base64 will do?
To be specific before someone downvote that question: I want to parse URL and get image to my server in any form. There is a lot similar questions, but none of them answers this one.
What I need that for? I have access to API where I also am provided with image url's, but I want them to be uploaded to my server via Background Job
in Parse.com service (something like CRON job). I know how to link file with ParseObject
, but can't find solution how to download image directly to ParseCloud
and link it.
var Image = require("parse-image");
Parse.Cloud.job("getImagesJob", function(request, status) {
Parse.Cloud.useMasterKey();
var beerObj = Parse.Object.extend("My_class");
var query = new Parse.Query(beerObj);
query.first().then(function(objs) {
var brew = objs.get("brewery");
var bname = objs.get("beer_name");
//var fullName = brew + " " + bname;
var fullName = "Browar Name";
console.log(fullName);
return Parse.Cloud.httpRequest({
method: 'GET',
url: 'api server address',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
params: {
q : fullName,
client_id : '...',
client_secret : '...'
}
}).then(function(httpResponse) {
var json = JSON.parse(httpResponse.text);
if(json.meta.code === 200){
if(json.response.beers.count > 0){
if(json.response.beers.items[0].beer.beer_label === "/default.png"){
console.log("brak etykiety dla " + fullName);
} else {
console.log("znaleziono etykietę dla " + fullName);
Parse.Cloud.httpRequest({ //NOT REACHING
url: json.response.beers.items[0].beer.beer_label,
success: function(response) {
// The file contents are in response.buffer.
var image = new Image();
return image.setData(response.buffer, {
success: function() {
objs.set("logo", image.data());
console.log("udalo sie dolaczyc");
},
error: function(error) {
// The image data was invalid.
console.error(error);
}
})
},
error: function(error) {
// The networking request failed.
}
});
}
} else {
// daj cokolwiek żeby się nie zacięło na jednym
console.log("Brak danych dla piwa: " + fullName);
}
}
}, function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status + httpResponse.text);
});
}).then(function(obj) {
status.success("Zrobione");
}, function(error) {
status.error(error);
});
});
Upvotes: 1
Views: 1653
Reputation: 1856
You can use parse-image
module in Cloud Code as in their documentation
var Image = require("parse-image");
Parse.Cloud.httpRequest({
url: YOUR_URL,
success: function(response) {
// The file contents are in response.buffer.
var image = new Image();
return image.setData(response.buffer, {
success: function() {
console.log("Image is " + image.width() + "x" + image.height() + ".");
},
error: function(error) {
// The image data was invalid.
}
})
},
error: function(error) {
// The networking request failed.
}
});
With above code, you can get image data in Buffer using image.data()
. To get base64 data, use image.data().toString("base64")
Upvotes: 2