Search in sources :

Example 1 with DbxException

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);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileMetadata(com.dropbox.core.v2.files.FileMetadata) IOException(java.io.IOException) Uri(android.net.Uri) FileInputStream(java.io.FileInputStream) DbxException(com.dropbox.core.DbxException)

Example 2 with DbxException

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;
}
Also used : ArrayList(java.util.ArrayList) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) ListFolderResult(com.dropbox.core.v2.files.ListFolderResult) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) IOException(java.io.IOException) Uri(android.net.Uri) DbxException(com.dropbox.core.DbxException) GetMetadataErrorException(com.dropbox.core.v2.files.GetMetadataErrorException)

Example 3 with DbxException

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);
    }
}
Also used : FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) DbxException(com.dropbox.core.DbxException)

Example 4 with DbxException

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 "";
    }
}
Also used : FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) DbxException(com.dropbox.core.DbxException)

Example 5 with DbxException

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);
    }
}
Also used : ArrayList(java.util.ArrayList) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) ListFolderResult(com.dropbox.core.v2.files.ListFolderResult) DbxException(com.dropbox.core.DbxException)

Aggregations

DbxException (com.dropbox.core.DbxException)31 FileMetadata (com.dropbox.core.v2.files.FileMetadata)14 IOException (java.io.IOException)14 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)10 Metadata (com.dropbox.core.v2.files.Metadata)9 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)8 FolderMetadata (com.dropbox.core.v2.files.FolderMetadata)8 DbxWebAuth (com.dropbox.core.DbxWebAuth)7 File (java.io.File)6 JsonReader (com.dropbox.core.json.JsonReader)5 DeletedMetadata (com.dropbox.core.v2.files.DeletedMetadata)5 ListFolderResult (com.dropbox.core.v2.files.ListFolderResult)5 FileInputStream (java.io.FileInputStream)5 InputStream (java.io.InputStream)5 DbxAuthInfo (com.dropbox.core.DbxAuthInfo)4 BufferedReader (java.io.BufferedReader)4 FileOutputStream (java.io.FileOutputStream)4 InputStreamReader (java.io.InputStreamReader)4 Uri (android.net.Uri)3 DbxAppInfo (com.dropbox.core.DbxAppInfo)3