DropboxResult result = new DropboxFileUploadResult();
//a map representing for each path the result of the put operation
Map<String, DropboxResultCode> resultEntries = null;
//in case the remote path is not specified, the remotePath = localPath
String dropboxPath = remotePath == null ? localPath : remotePath;
DbxEntry entry = null;
try {
entry = DropboxAPIFacade.client.getMetadata(dropboxPath);
} catch (DbxException e) {
throw new DropboxException(dropboxPath + " does not exist or can't obtain metadata");
}
File fileLocalPath = new File(localPath);
//verify uploading of a single file
if (fileLocalPath.isFile()) {
//check if dropbox file exists
if (entry != null && !entry.isFile()) {
throw new DropboxException(dropboxPath + " exists on dropbox and is not a file!");
}
//in case the entry not exists on dropbox check if the filename should be appended
if (entry == null) {
if (dropboxPath.endsWith(DROPBOX_FILE_SEPARATOR)) {
dropboxPath = dropboxPath + fileLocalPath.getName();
}
}
resultEntries = new HashMap<String, DropboxResultCode>(1);
try {
DbxEntry.File uploadedFile = putSingleFile(fileLocalPath, dropboxPath, mode);
if (uploadedFile == null) {
resultEntries.put(dropboxPath, DropboxResultCode.KO);
} else {
resultEntries.put(dropboxPath, DropboxResultCode.OK);
}
} catch (Exception ex) {
resultEntries.put(dropboxPath, DropboxResultCode.KO);
} finally {
result.setResultEntries(resultEntries);
}
return result;
} else { //verify uploading of a list of files inside a dir
LOG.info("uploading a dir...");
//check if dropbox folder exists
if (entry != null && !entry.isFolder()) {
throw new DropboxException(dropboxPath + " exists on dropbox and is not a folder!");
}
if (!dropboxPath.endsWith(DROPBOX_FILE_SEPARATOR)) {
dropboxPath = dropboxPath + DROPBOX_FILE_SEPARATOR;
}