use of com.dropbox.core.DbxException in project orgzly-android by orgzly.
the class DropboxClient method upload.
/**
* Upload file to Dropbox.
*/
public VersionedRook upload(File file, Uri repoUri, String fileName) throws IOException {
linkedOrThrow();
Uri bookUri = repoUri.buildUpon().appendPath(fileName).build();
if (file.length() > UPLOAD_FILE_SIZE_LIMIT * 1024 * 1024) {
throw new IOException(LARGE_FILE);
}
FileMetadata metadata;
InputStream in = new FileInputStream(file);
try {
metadata = dbxClient.files().uploadBuilder(bookUri.getPath()).withMode(WriteMode.OVERWRITE).uploadAndFinish(in);
} catch (DbxException e) {
if (e.getMessage() != null) {
throw new IOException("Failed overwriting " + bookUri.getPath() + " on Dropbox: " + e.getMessage());
} else {
throw new IOException("Failed overwriting " + bookUri.getPath() + " on Dropbox: " + e.toString());
}
}
String rev = metadata.getRev();
long mtime = metadata.getServerModified().getTime();
return new VersionedRook(repoUri, bookUri, rev, mtime);
}
use of com.dropbox.core.DbxException in project orgzly-android by orgzly.
the class DropboxClient method getBooks.
public List<VersionedRook> getBooks(Uri repoUri) throws IOException {
linkedOrThrow();
List<VersionedRook> list = new ArrayList<>();
String path = repoUri.getPath();
/* Fix root path. */
if (path == null || path.equals("/")) {
path = ROOT_PATH;
}
/* Strip trailing slashes. */
path = path.replaceAll("/+$", "");
try {
if (ROOT_PATH.equals(path) || dbxClient.files().getMetadata(path) instanceof FolderMetadata) {
/* Get folder content. */
ListFolderResult result = dbxClient.files().listFolder(path);
while (true) {
for (Metadata metadata : result.getEntries()) {
if (metadata instanceof FileMetadata) {
FileMetadata file = (FileMetadata) metadata;
if (BookName.isSupportedFormatFileName(file.getName())) {
Uri uri = repoUri.buildUpon().appendPath(file.getName()).build();
VersionedRook book = new VersionedRook(repoUri, uri, file.getRev(), file.getServerModified().getTime());
list.add(book);
}
}
}
if (!result.getHasMore()) {
break;
}
result = dbxClient.files().listFolderContinue(result.getCursor());
}
} else {
throw new IOException("Not a directory: " + repoUri);
}
} catch (DbxException e) {
e.printStackTrace();
/* If we get NOT_FOUND from Dropbox, just return the empty list. */
if (e instanceof GetMetadataErrorException) {
if (((GetMetadataErrorException) e).errorValue.getPathValue() == LookupError.NOT_FOUND) {
return list;
}
}
throw new IOException("Failed getting the list of files in " + repoUri + " listing " + path + ": " + (e.getMessage() != null ? e.getMessage() : e.toString()));
}
return list;
}
use of com.dropbox.core.DbxException in project keepass2android by PhilippC.
the class DropboxV2Storage method getFileEntry.
@Override
public FileEntry getFileEntry(String filename) throws Exception {
try {
filename = removeProtocol(filename);
Log.d("KP2AJ", "getFileEntry(), " + filename);
// querying root is not supported
if ((filename.equals("")) || (filename.equals("/")))
return getRootFileEntry();
if (filename.endsWith("/"))
filename = filename.substring(0, filename.length() - 1);
Metadata dbEntry = dbxClient.files().getMetadata(filename);
return convertToFileEntry(dbEntry);
} catch (DbxException e) {
throw convertException(e);
}
}
use of com.dropbox.core.DbxException in project keepass2android by PhilippC.
the class DropboxV2Storage method getCurrentFileVersionFast.
public String getCurrentFileVersionFast(String path) {
try {
path = removeProtocol(path);
Metadata entry = dbxClient.files().getMetadata(path);
return String.valueOf(entry.hashCode());
} catch (DbxException e) {
Log.d(TAG, e.toString());
return "";
}
}
use of com.dropbox.core.DbxException in project keepass2android by PhilippC.
the class DropboxV2Storage method listFiles.
@Override
public List<FileEntry> listFiles(String parentPath) throws Exception {
try {
parentPath = removeProtocol(parentPath);
if (parentPath.equals("/"))
// Dropbox is a bit picky here
parentPath = "";
ListFolderResult dirEntry = dbxClient.files().listFolder(parentPath);
List<FileEntry> result = new ArrayList<FileEntry>();
while (true) {
for (Metadata e : dirEntry.getEntries()) {
FileEntry fileEntry = convertToFileEntry(e);
result.add(fileEntry);
}
if (!dirEntry.getHasMore()) {
break;
}
dirEntry = dbxClient.files().listFolderContinue(dirEntry.getCursor());
}
return result;
} catch (DbxException e) {
throw convertException(e);
}
}
Aggregations