use of com.dropbox.core.DbxEntry.WithChildren in project openhab1-addons by openhab.
the class DropboxSynchronizer method syncLocalToDropbox.
/**
* Synchronizes all changes from the local filesystem into Dropbox. Changes
* are identified by the files' <code>lastModified</code> attribute. If there
* are less files locally the additional files will be deleted from the
* Dropbox. New files will be uploaded or overwritten if they exist already.
*
* @throws DbxException if there are technical or application level
* errors in the Dropbox communication
* @throws IOException
*/
public void syncLocalToDropbox(DbxClient client) throws DbxException, IOException {
logger.debug("Started synchronization from local to Dropbox ...");
Map<String, Long> dropboxEntries = new HashMap<String, Long>();
WithChildren metadata = client.getMetadataWithChildren("/");
File dropboxEntryFile = new File(DBX_FOLDER + DROPBOX_ENTRIES_FILE_NAME);
if (!dropboxEntryFile.exists() || !metadata.hash.equals(lastHash)) {
collectDropboxEntries(client, dropboxEntries, "/");
serializeDropboxEntries(dropboxEntryFile, dropboxEntries);
lastHash = metadata.hash;
// TODO: TEE: we could think about writing the 'lastHash' to a file?
// let's see what daily use brings whether this a necessary feature!
} else {
logger.trace("Dropbox entry file '{}' exists -> extract content", dropboxEntryFile.getPath());
dropboxEntries = extractDropboxEntries(dropboxEntryFile);
}
Map<String, Long> localEntries = new HashMap<String, Long>();
collectLocalEntries(localEntries, contentDir);
logger.debug("There are '{}' local entries that met the upload filters ...", localEntries.size());
boolean isChanged = false;
for (java.util.Map.Entry<String, Long> entry : localEntries.entrySet()) {
if (dropboxEntries.containsKey(entry.getKey())) {
if (entry.getValue().compareTo(dropboxEntries.get(entry.getKey())) > 0) {
logger.trace("Local file '{}' is newer - upload to Dropbox!", entry.getKey());
if (!fakeMode) {
uploadFile(client, entry.getKey(), true);
}
isChanged = true;
}
} else {
logger.trace("Local file '{}' doesn't exist in Dropbox - upload to Dropbox!", entry.getKey());
if (!fakeMode) {
uploadFile(client, entry.getKey(), false);
}
isChanged = true;
}
dropboxEntries.remove(entry.getKey());
}
// so delete them from Dropbox!
for (String path : dropboxEntries.keySet()) {
for (String filter : uploadFilterElements) {
if (path.matches(filter)) {
if (!fakeMode) {
client.delete(path);
}
isChanged = true;
logger.debug("Successfully deleted file '{}' from Dropbox", path);
} else {
logger.trace("Skipped file '{}' since it doesn't match the given filter arguments.", path);
}
}
}
// which causes a new generation during the next sync
if (isChanged) {
boolean success = FileUtils.deleteQuietly(dropboxEntryFile);
if (!success) {
logger.warn("Couldn't delete file '{}'", dropboxEntryFile.getPath());
} else {
logger.debug("Deleted cache file '{}' since there are changes. It will be recreated on the next synchronization loop.", dropboxEntryFile.getPath());
}
// since there are changes we have to update the lastCursor (and
// the corresponding file) to have the right starting point for the
// next synchronization loop
DbxDelta<DbxEntry> delta = client.getDelta(lastCursor);
writeDeltaCursor(delta.cursor);
} else {
logger.debug("No files changed locally. No deltas to upload to Dropbox ...");
}
}
Aggregations