nrek
nrek

Reputation: 271

How can I judge whether the file (read from dropbox webpage) has been changed or not in Java?

Now I'm doing a distributed systems homework in Java, so I need to access one copy of configuration file from several computers. And now I could read and parse a shared file from dropbox webpage, like this one: https://www.dropbox.com/s/ysn9yivqj7kwo0w/config.yaml. What I want to do is to add a daemon thread to detect whether this file has been changed or not, if changed, I need to re-config every node of system.

But how can I judge whether this file has been changed or not IN PROGRAM, without downloading the whole file and then to do some diff? I think dropbox should add something like timestamps to files, but how can I get access to this timestamp?

Any suggestion is welcome, much thanks!

Upvotes: 4

Views: 1814

Answers (3)

Rishi Dua
Rishi Dua

Reputation: 2334

Solution 1

According to the API documentation, /metadata retrieves file and folder metadata. Compare hash (md5) to check

URL Structure: https://api.dropbox.com/1/metadata/auto/<path>

Returns the metadata for the file or folder at the given . If represents a folder and the list parameter is true, the metadata will also include a listing of metadata for the folder's contents.

Using in Java

From Java SDK documentation

public DbxEntry getMetadata(String path)
                     throws DbxException
Get the file or folder metadata for a given path.
 DbxClient dbxClient = ...
 DbxEntry entry = dbxClient.getMetadata("/Photos");
 if (entry == null) {
     System.out.println("No file or folder at that path.");
 } else {
     System.out.print(entry.toStringMultiline());
 }

Parameters

path - The path to the file or folder (see DbxPath).

Returns

If there is a file or folder at the given path, return the metadata for that path. If there is no file or folder there, return null.

Throws

DbxException

Update

Solution 2 (Hacky workaround)

Unfortunately Dropbox doesn't give hash for files, it only gives for directories. So if you're developing using dropbox API for sync, you can do one of the following

  • When downloading the file, copy the rev parameter as that contains the revision number
  • Compare last modified for the local and cloud file (Note: Not guaranteed to work always. If 2 people edit file at the same time locally at the exact same time and one of them overwrites the draft file, you might get a false positive).

Upvotes: 0

Jintian DENG
Jintian DENG

Reputation: 3202

If you are using the sdk form dropbox, you can get the metadata of a file via

    meta = api.metadata(path, 1, null, false, null);

and check the last modified date or hash of the file via

    meta.hash;
    meta.modified;

Upvotes: 1

Paul U
Paul U

Reputation: 681

I'd look at the content-md5. So you keep an md5 of your previous version and if they don't match, then download the file.

Upvotes: 2

Related Questions