Search code examples
javaandroiddropbox-apidropbox-sdk

Is there a way to upload/download a file from/to Dropbox while keeping the modification date of the file?


I'm trying to build a sync system for my writing application, so that I can synchronize my text files with a Dropbox folder and edit them from my computer.

Thing is, when a file is uploaded, its modification date corresponds to that of the upload time, not the last time the file's content was modified, and it looks as if the Dropbox file was modified more recently than the local file. Same thing for download, as the local version gets a more recent modification date than the Dropbox one.

This makes things complicated when I want to compare dates to determine which version is the most recent one between the local and the network versions, and if I need to upload the local version or download the network one to be up-to-date.

Is there a way to keep the modification date of the original file ? Currently, I'm using these functions, but maybe I should use a completely different method.

public void uploadFile(String local_path, String db_path) {
    try {
        InputStream in = new FileInputStream(local_path);
        client.files().uploadBuilder(db_path)
            .withMode(WriteMode.OVERWRITE)
            .uploadAndFinish(in);
    }
    catch (FileNotFoundException fne) { fne.printStackTrace(); }
    catch (IOException ioe) { ioe.printStackTrace(); }
    catch (DbxException dbxe) { dbxe.printStackTrace(); }
}
public void downloadFile(String db_path, String local_path) {
    try {
        File dest = new File(local_path);
        try (OutputStream outputStream = new FileOutputStream(dest)) {
            client.files().download(db_path).download(outputStream);
        }
    }
    catch (DbxException e) { e.printStackTrace(); }
    catch (IOException e) { e.printStackTrace(); }
}

Solution

  • You can set the clientModified date using UploadBuilder.withClientModified. It's not possible to override the serverModified date though.