Here's my situation: I have a profile pic attached to my users. On mobile, when a user is registering, their profile image is set to a default image, and they have the opportunity to change it. Regardless of whether or not they do, whatever image the profile image is set to, we create a parse file object and attach it to the user.
Our web developer took the url for one of those users' default profile pictures and used that same file for all of the registering users who didn't select a picture.
At some point, the original that he used was deleted or something. Not sure when/how, but the link that that picture had now goes to an "access denied" page. It's a parsefile url, not an external url, so I'm not sure what's going on there.
Anyway, I created a background job that found all of the users with that broken url. Now I want to give them all a unique parse file pointing to their own url. I know that I can add files to my cloud code deploys, but I'm not sure how to access it to create the array of byte values I need to generate a Parse.File object in cloud code.
My question boils down to: Where do I store an image file so that I can access it in my cloud code, and how do I create an array of byte values from that image file?
Here's what I expect my code to look like. Any help filling in the missing steps would be appreciated:
Parse.Cloud.job("fixBrokenProfilePics", function( request, status )
{
Parse.Cloud.useMasterKey();
//The BrokenProfilePic object only contains a pointer to a user that has the broken profile pic link as it's ProfilePicture field
var brokeProfilePicQuery = new Parse.Query("BrokenProfilePic");
brokenProfilePicQuery.include("User");
brokenProfilePicQuery.each
(
function( brokenProfilePic )
{
var user = brokenProfilePic.get("User");
//****MISSING STEPS*****
var image = ???? //Get the image file
var byteArray = ???? //Get byte array from the image file
var imageName = "ProfilePic" + user.id;
var imageFile = new Parse.File(imageName, byteArray);
return imageFile.save().then
(
function( imageFile )
{
user.set("ProfilePicture", imageFile);
return user.save().then
(
function( user ){}, //Don't need to do anything
function( error )
{
var promise = new Parse.Promise();
return promise.reject("There was an error trying to save the user: " + error.message);
}
);
},
function( error )
{
var promise = new Parse.Promise();
return promise.reject("There was an error trying to save the parse file: " + error.message);
}
);
}
).then
(
function( results )
{
status.success("Updated the ProfilePics");
},
function( error )
{
status.error("There was an error trying to update the profile pics: " + error.message);
}
);
});
edit - I found this question which appears to be my same issue: https://parse.com/questions/access-denied-on-images-request-failed-forbidden-403
It looks like it's caused by using Parse's clean up files feature, although supposedly it only happens if your files are in an array or saved as a string on an object (weak references) rather than stored in a Parse File column (strong references), which mine are. I'm not sure why I'd be running into this issue.