Search in sources :

Example 1 with FolderMetadata

use of com.dropbox.core.v2.files.FolderMetadata in project dropbox-sdk-java by dropbox.

the class Main method testEnumeratedSubtypeSerialization.

private static void testEnumeratedSubtypeSerialization(DbxClientV2 client) throws DbxException, IOException {
    String rootPath = "/test/proguard-tests";
    try {
        FolderMetadata root = client.files().createFolder(rootPath);
        assertNotNull(root);
        assertEquals(root.getPathLower(), rootPath);
        assertEquals(root.getPathDisplay(), rootPath);
    } catch (CreateFolderErrorException ex) {
        if (ex.errorValue.isPath() && ex.errorValue.getPathValue().isConflict() && ex.errorValue.getPathValue().getConflictValue() == WriteConflictError.FOLDER) {
        // ignore duplicate folder exception
        } else {
            throw ex;
        }
    }
    Map<String, byte[]> files = new LinkedHashMap<String, byte[]>();
    files.put(rootPath + "/foo.blob", bytes(1024));
    files.put(rootPath + "/bar.blob", bytes(512));
    files.put(rootPath + "/sub/a.dat", bytes(4096));
    files.put(rootPath + "/sub/b/c.dat", bytes(64));
    files.put(rootPath + "/pics/cat.rawb", bytes(8196));
    try {
        for (Map.Entry<String, byte[]> entry : files.entrySet()) {
            String path = entry.getKey();
            byte[] data = entry.getValue();
            FileMetadata file = client.files().uploadBuilder(path).withMode(WriteMode.OVERWRITE).withAutorename(false).withMute(true).uploadAndFinish(new ByteArrayInputStream(data));
            assertNotNull(file);
            assertEquals(file.getPathLower(), path);
            assertEquals(file.getSize(), data.length);
            Metadata metadata = client.files().getMetadata(path);
            assertEquals(metadata, file);
        }
        for (String path : files.keySet()) {
            Metadata file = client.files().delete(path);
            assertNotNull(file);
            assertEquals(file.getPathLower(), path);
            assertTrue(file instanceof FileMetadata);
            Metadata deleted = client.files().getMetadataBuilder(path).withIncludeDeleted(true).start();
            assertNotNull(deleted);
            assertTrue(deleted instanceof DeletedMetadata, deleted.getClass().toString());
            assertEquals(deleted.getPathLower(), path);
        }
    } finally {
        client.files().delete(rootPath);
    }
}
Also used : CreateFolderErrorException(com.dropbox.core.v2.files.CreateFolderErrorException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileMetadata(com.dropbox.core.v2.files.FileMetadata) 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) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with FolderMetadata

use of com.dropbox.core.v2.files.FolderMetadata in project dropbox-sdk-java by dropbox.

the class DropboxBrowse method doBrowse.

// -------------------------------------------------------------------------------------------
// GET /browse?path=...
// -------------------------------------------------------------------------------------------
// The page that lets you browse your Dropbox account.  Makes Dropbox API calls to figure out
// the contents of your files and folders and display them to you.
public void doBrowse(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    if (!common.checkGet(request, response))
        return;
    User user = common.getLoggedInUser(request);
    if (user == null) {
        common.pageSoftError(response, "Can't do /browse.  Nobody is logged in.");
        return;
    }
    DbxClientV2 dbxClient = requireDbxClient(request, response, user);
    if (dbxClient == null)
        return;
    String path = request.getParameter("path");
    if (path == null || path.length() == 0) {
        renderFolder(response, user, dbxClient, "");
    } else {
        String pathError = DbxPathV2.findError(path);
        if (pathError != null) {
            response.sendError(400, "Invalid path: " + jq(path) + ": " + pathError);
            return;
        }
        Metadata metadata;
        try {
            metadata = dbxClient.files().getMetadata(path);
        } catch (GetMetadataErrorException ex) {
            if (ex.errorValue.isPath()) {
                LookupError le = ex.errorValue.getPathValue();
                if (le.isNotFound()) {
                    response.sendError(400, "Path doesn't exist on Dropbox: " + jq(path));
                    return;
                }
            }
            common.handleException(response, ex, "getMetadata(" + jq(path) + ")");
            return;
        } catch (DbxException ex) {
            common.handleDbxException(response, user, ex, "getMetadata(" + jq(path) + ")");
            return;
        }
        path = DbxPathV2.getParent(path) + "/" + metadata.getName();
        if (metadata instanceof FolderMetadata) {
            renderFolder(response, user, dbxClient, path);
        } else {
            renderFile(response, path, (FileMetadata) metadata);
        }
    }
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) 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) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) LookupError(com.dropbox.core.v2.files.LookupError) DbxException(com.dropbox.core.DbxException) GetMetadataErrorException(com.dropbox.core.v2.files.GetMetadataErrorException)

Example 3 with FolderMetadata

use of com.dropbox.core.v2.files.FolderMetadata in project dropbox-sdk-java by dropbox.

the class Main method printChanges.

/**
     * Prints changes made to a folder in Dropbox since the given
     * cursor was retrieved.
     *
     * @param dbxClient Dropbox client to use for fetching folder changes
     * @param cursor lastest cursor received since last set of changes
     *
     * @return latest cursor after changes
     */
private static String printChanges(DbxClientV2 client, String cursor) throws DbxApiException, DbxException {
    while (true) {
        ListFolderResult result = client.files().listFolderContinue(cursor);
        for (Metadata metadata : result.getEntries()) {
            String type;
            String details;
            if (metadata instanceof FileMetadata) {
                FileMetadata fileMetadata = (FileMetadata) metadata;
                type = "file";
                details = "(rev=" + fileMetadata.getRev() + ")";
            } else if (metadata instanceof FolderMetadata) {
                FolderMetadata folderMetadata = (FolderMetadata) metadata;
                type = "folder";
                details = folderMetadata.getSharingInfo() != null ? "(shared)" : "";
            } else if (metadata instanceof DeletedMetadata) {
                type = "deleted";
                details = "";
            } else {
                throw new IllegalStateException("Unrecognized metadata type: " + metadata.getClass());
            }
            System.out.printf("\t%10s %24s \"%s\"\n", type, details, metadata.getPathLower());
        }
        // update cursor to fetch remaining results
        cursor = result.getCursor();
        if (!result.getHasMore()) {
            break;
        }
    }
    return cursor;
}
Also used : FileMetadata(com.dropbox.core.v2.files.FileMetadata) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) Metadata(com.dropbox.core.v2.files.Metadata) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) ListFolderResult(com.dropbox.core.v2.files.ListFolderResult) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) DeletedMetadata(com.dropbox.core.v2.files.DeletedMetadata)

Example 4 with FolderMetadata

use of com.dropbox.core.v2.files.FolderMetadata in project dropbox-sdk-java by dropbox.

the class FilesActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String path = getIntent().getStringExtra(EXTRA_PATH);
    mPath = path == null ? "" : path;
    setContentView(R.layout.activity_files);
    Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            performWithPermissions(FileAction.UPLOAD);
        }
    });
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.files_list);
    mFilesAdapter = new FilesAdapter(PicassoClient.getPicasso(), new FilesAdapter.Callback() {

        @Override
        public void onFolderClicked(FolderMetadata folder) {
            startActivity(FilesActivity.getIntent(FilesActivity.this, folder.getPathLower()));
        }

        @Override
        public void onFileClicked(final FileMetadata file) {
            mSelectedFile = file;
            performWithPermissions(FileAction.DOWNLOAD);
        }
    });
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(mFilesAdapter);
    mSelectedFile = null;
}
Also used : FileMetadata(com.dropbox.core.v2.files.FileMetadata) FloatingActionButton(android.support.design.widget.FloatingActionButton) RecyclerView(android.support.v7.widget.RecyclerView) FolderMetadata(com.dropbox.core.v2.files.FolderMetadata) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) Toolbar(android.support.v7.widget.Toolbar)

Aggregations

FileMetadata (com.dropbox.core.v2.files.FileMetadata)4 FolderMetadata (com.dropbox.core.v2.files.FolderMetadata)4 DeletedMetadata (com.dropbox.core.v2.files.DeletedMetadata)3 Metadata (com.dropbox.core.v2.files.Metadata)3 FloatingActionButton (android.support.design.widget.FloatingActionButton)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Toolbar (android.support.v7.widget.Toolbar)1 View (android.view.View)1 DbxException (com.dropbox.core.DbxException)1 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)1 CreateFolderErrorException (com.dropbox.core.v2.files.CreateFolderErrorException)1 GetMetadataErrorException (com.dropbox.core.v2.files.GetMetadataErrorException)1 ListFolderResult (com.dropbox.core.v2.files.ListFolderResult)1 LookupError (com.dropbox.core.v2.files.LookupError)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1