* in case of "force" the file already existing with the same name will be overridden.
* @return a DropboxResult object reporting for each remote path the result of the operation.
* @throws DropboxException
*/
public DropboxResult put(String localPath, String remotePath, DropboxUploadMode mode) throws DropboxException {
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;
}
//revert to old path
String oldDropboxPath = dropboxPath;
//list all files in a dir
Collection<File> listFiles = FileUtils.listFiles(fileLocalPath, null, true);
if (listFiles == null || listFiles.isEmpty()) {
throw new DropboxException(localPath + " doesn't contain any files");
}
resultEntries = new HashMap<String, DropboxResultCode>(listFiles.size());
for (File file : listFiles) {
String absPath = file.getAbsolutePath();
int indexRemainingPath = localPath.length();
if (!localPath.endsWith("/")) {
indexRemainingPath += 1;
}
String remainingPath = absPath.substring(indexRemainingPath);
dropboxPath = dropboxPath + remainingPath;
try {
LOG.info("uploading:" + fileLocalPath + "," + dropboxPath);
DbxEntry.File uploadedFile = putSingleFile(file, dropboxPath, mode);
if (uploadedFile == null) {
resultEntries.put(dropboxPath, DropboxResultCode.KO);
} else {
resultEntries.put(dropboxPath, DropboxResultCode.OK);
}
} catch (Exception ex) {
resultEntries.put(dropboxPath, DropboxResultCode.KO);
}
dropboxPath = oldDropboxPath;
}
result.setResultEntries(resultEntries);
return result;
}
}