Search code examples
javamd5google-docs-api

Google Docs API "setMd5Checksum" not working


Recently I've implemented an application in Java that uses the Google Docs API v3.0. New entries are created like this:

DocumentListEntry newEntry = new  DocumentListEntry();
newEntry.setFile(file, Common.resolveMimeType(file)); //Common is a custom class
newEntry.setFilename(entryTitle.getPlainText()); //entryTitle is a TextConstruct
newEntry.setTitle(entryTitle); 
newEntry.setDraft(false);
newEntry.setHidden(file.isHidden());
newEntry.setMd5Checksum(Common.getMD5HexDigest(file));

Trust me when I tell you that Common.getMD5HexDigest(file) returns a valid and unique MD5 Hexadecimal hash.

Now, the file uploads properly yet when retrieving the file and checking the MD5 checksum through the entry.getMd5Checksum() method, it always returns null.

I've tried EVERYTHING, even set the ETag, ResourceID and VersionID but they all get override with default values (null or server generated strings).


Solution

  • After struggling a few weeks with the MD5 checksum problem (to verify if the content of the file changed over time), I came up with a solution that doesn't rely on the MD5 checksum of the file but on the client last-update attribute of the file.

    This solution goes to everyone that wants to check if a file has changed over time. However, "an update" on any operating system can be considered as the act of opening the file and saving the file, with or without making any changes to the content of the file. So, it's not perfect but does save some time and bandwidth.

    Solution:

    long lastModified = new DateTime(
        new Date(file.lastModified()), TimeZone.getDefault()
    ).getValue();
    
    if(lastModified > entry.getUpdated().getValue()) {
        //update the file
    }
    

    Where file is a File instance of the desired file and entry is the DocumentListEntry associated to the local file.