Pregunta

My Android app updates a Google Drive document. The file can be modified also elsewhere (e.g. through Drive web interface), so there may be a conflict on file upload. However, this should rarely happen. That's why I don't want my app to first query the revision history (since this is in most cases unnecessary) and only after that, update the file. How can I detect that there is a conflict when updating the file?

My investigation so far reveals that getHeadRevisionId() returns null although the null head revision id has been reported fixed. Another thing I tried was setEtag() on the file before update(). It should have given me error on update, but the upload was successful even the file had been changed remotely! Is this the correct way to use ETag?

¿Fue útil?

Solución

Set the If-Match HTTP header:

final Update update = mService.files().update(mDriveFile.getId(), mDriveFile, byteContent);
final HttpHeaders headers = update.getRequestHeaders();
headers.setIfMatch(mDriveFile.getEtag());
update.setRequestHeaders(headers);
mDriveFile = update.execute();

In case the document has changed meanwhile, the update will get rejected with a response something like:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 412 Precondition Failed
{
  "code": 412,
  "errors": [
    {
      "domain": "global",
      "location": "If-Match",
      "locationType": "header",
      "message": "Precondition Failed",
      "reason": "conditionNotMet"
    }
  ],
  "message": "Precondition Failed"
}

Note that ETag might change even if the content does not.

Otros consejos

"Is this the correct way to use ETag?"

Yes

Also, for non-Docs files, you should also check md5Checksum for changes to the content.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top