0

I'm trying to use Cloud Code to check whether a user-submitted image is in a supported file type and not too big.

I know I need to do this verification server-side and I think I should do it with Cloud Code using beforeSave – the doc even has a specific example about data validation, but it doesn't explain how to handle files and I couldn't figure it out.

I've tried the documented method for saving files, ie.

file = fileUploadControl.files[0];
var parseFile = new Parse.File(name, file);
currentUser.set("picture", parseFile);
currentUser.save();

and in the Cloud Code, Parse.Cloud.beforeSave(Parse.User, (request, response) => { // code here });

But 1. this still actually saves the file on my server, right? I want to check the file size first to avoid saving too many big files...

And 2. Even then, I don't know what to do in the beforeSave callback. It seems I can only access the URL of the saved image (proof that it has been uploaded), and it seems very counter-intuitive to have to do another https request to check the file size and type before deciding whether to proceed with attaching the file to the User object.

(I'm currently using remote-file-size and file-type to check the size and type of the uploaded file, but no success here either).

I also tried calling a Cloud function, but it feels like I'm not doing the right thing, and besides I'm running into the same issues. I can call a Cloud function and pass a saved ParseFile as a parameter, and then I know how to save it to the User object from the Cloud Code using the masterkey, but as above it still involves uploading the file to the server and then re-fetching it using its URL.

Am I missing anything here? Is there no way to do something like a beforeSave on Parse.File, and then stop the file from being saved if it doesn't meet certain criteria?

Cheers.

2 Answers 2

0

If you have to do something with files, parse lets you overwrite the file adapter to handle file operations.

You can indicate the file adapter to use in your ParseServer instatiation:

    var FSStoreAdapter = require('./file_adapter');
    var api = new ParseServer({
      databaseURI: databaseUri ,
      cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
      appId: process.env.APP_ID,
      filesAdapter: fs_store_adapter, // YOUR FILE ADAPTER
      masterKey: process.env.MASTER_KEY, //Add your master key here. Keep it secret!
      serverURL: "https://yourUrl",  // Don't forget to change to https if needed
      publicServerURL: "https://yourUrl",
      liveQuery: {
        classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
      }
      maxUploadSize: "500mb"  //you will now have 500mb limit :)
    });

That said, you can also specify a maxUploadSize in your instatiation as you can see in the last line.

2
  • Thanks for the reply! I wouldn't know where to start with writing my own file adapter though. Besides, my server is hosted on Nodechef and while they let me change instantiation options and adapters, I don't have a lot of visibility on what's going on in the background, where the files are saved etc. so I imagine it's gonna be hell to debug...
    – Acccent
    Feb 16, 2019 at 1:28
  • I understand.. anyway if you think my answer is ok can you accept it? Feb 26, 2019 at 14:26
0

you have to use save in background

file = ParseFile("filename", file)
file?.saveInBackground({ e ->
if (e == null) {

} else {
  Toast.makeText(applicationContext, "Error: $e", Toast.LENGTH_SHORT).show()

  e.printStackTrace()
  Log.d("DEBUG", "file " + e.code)
}
}, { percentDone ->
  Log.d("DEBUG", "file:" + percentDone!!)
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.