0

I'm currently developing an iOS app where user will be able to upload images to my Parse Server as it stands they can upload any number of images to the database but i want to put a cap on their uploads lets say allocate 1gb to every user.

I also want to be able to lift the limit for specific users as well so that some people are able to have more storage than others.

Any help would be appreciated.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253

1 Answers1

0

After some quick searching I do not see any straightforward was to go about that. What I would do is:

  1. Create a new field for a PFUser called usedSpace. This will be the amount of megabyte a user is able to have.
  2. Create a new Cloud Code function that accepts both an image and an image file size.
  3. When you go to upload, you can find the file size in megabytes by following this answer.
  4. Pass the image data and file size to your new function.
  5. In your new function check to see if the user has enough free space to save the new image. If so, save and update the usedSpace parameter. If not, return an error.

You Cloud Code function would look something like this (pseudo-ish code):

// For example:
Parse.Cloud.define("saveImageWithSize", function(request, response) {

  // Grab var's from params
  var imageData = request.params.imageData;
  var imageSize = request.params.imageSize;

  if user has enough free space {
    // save the imageSize
    // update the `usedSpace` field
    response.success("success");
  }
  else  user does not have enough space {
    response("not enough space");
  }
});

Then you would call it from your app like this:

    PFCloud.callFunctionInBackground("saveImageWithSize", withParameters: ["imageData" : yourImageData, "imageSize" : youImageSize]) { (response, error) in
        if let err = error {
            print(err)
        }
        else {
            print(complete)
        }
    }

hope this helps.

Community
  • 1
  • 1
random
  • 8,568
  • 12
  • 50
  • 85