parse-community / Parse-SDK-Android

The Android SDK for Parse Platform
https://parseplatform.org/
Other
1.88k stars 739 forks source link

Add option to pass additional parameters to backend when saving/uploading file #1201

Open evtimDev opened 8 months ago

evtimDev commented 8 months ago

New Feature / Enhancement Checklist

Current Limitation

Currently, when we call file.saveInBackground(), etc. we can't pass any additional parameters to be sent together with the POST request to the backend for the server-side code to examine/act upon.

Feature / Enhancement Description

Let's add a version of file.saveInBackground() with additional JSON params to be send with the file POST request to the server-side.

Example Use Case

Server-side association of file with database object:

  1. Client needs to upload a file and update an object field with the file
  2. Client calls file.saveInBackgroundWithParams(<JSON with parseClass, objectId and filed>)
  3. On server-side, in Parse.Cloud.afterSaveFile, devs can write custom code that examines the params, perform any validation required, and update the object field with the newly uploaded file as appropriate. If validation fails, the server-side can also delete the file and return appropriate error to client.

The advantage is fewer number of calls to backend, simpler implementation of file validation, simpler bookkeeping and ensuring no unreferenced files get stored.

Alternatives / Workarounds

There are two alternatives currently:

  1. Client makes multiple calls - save file, then another call to update object with saved file. This is less efficient, more error prone and causes potentially multiple copies of the file to be stored in cases where the client retires the file upload operation (if operation succeeded, but client failed to receive response of success for example). Additional work-around to handle those cases would be on the server-side to always create some temporary record for any file saved, then have some sort of background job running with certain interval to delete the files that have not been associated in reasonable time.

  2. Using REST API to implement custom file POST request with params passed in either as additional header, or following the JS SDK example, leveraging the metadata/tags format, see https://github.com/parse-community/parse-server/issues/6771

3rd Party References

This is somewhat similar to the JS Parse SDK File.addMetadata(), https://docs.parseplatform.org/js/guide/#adding-metadata-and-tags

parse-github-assistant[bot] commented 8 months ago

Thanks for opening this issue!