com.dropbox.core.v2.files.WriteMode Java Examples

The following examples show how to use com.dropbox.core.v2.files.WriteMode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Dropbox.java    From financisto with GNU General Public License v2.0 6 votes vote down vote up
public FileMetadata uploadFile(File file) throws Exception {
    if (authSession()) {
        try {
            InputStream is = new FileInputStream(file);
            try {
                FileMetadata fileMetadata = dropboxClient.files().uploadBuilder("/" + file.getName()).withMode(WriteMode.ADD).uploadAndFinish(is);
                Log.i("Financisto", "Dropbox: The uploaded file's rev is: " + fileMetadata.getRev());
                return fileMetadata;
            } finally {
                IOUtil.closeInput(is);
            }
        } catch (Exception e) {
            Log.e("Financisto", "Dropbox: Something wrong", e);
            throw new ImportExportException(R.string.dropbox_error, e);
        }
    } else {
        throw new ImportExportException(R.string.dropbox_auth_error);
    }
}
 
Example #2
Source File: UploadFileTask.java    From dropbox-sdk-java with MIT License 6 votes vote down vote up
@Override
protected FileMetadata doInBackground(String... params) {
    String localUri = params[0];
    File localFile = UriHelpers.getFileForUri(mContext, Uri.parse(localUri));

    if (localFile != null) {
        String remoteFolderPath = params[1];

        // Note - this is not ensuring the name is a valid dropbox file name
        String remoteFileName = localFile.getName();

        try (InputStream inputStream = new FileInputStream(localFile)) {
            return mDbxClient.files().uploadBuilder(remoteFolderPath + "/" + remoteFileName)
                    .withMode(WriteMode.OVERWRITE)
                    .uploadAndFinish(inputStream);
        } catch (DbxException | IOException e) {
            mException = e;
        }
    }

    return null;
}
 
Example #3
Source File: DbxClientV2IT.java    From dropbox-sdk-java with MIT License 6 votes vote down vote up
@Test
public void testRangeDownload() throws Exception {
    DbxClientV2 client = ITUtil.newClientV2();

    byte [] contents = ITUtil.randomBytes(500);
    String path = ITUtil.path(getClass(), "/testRangeDownload.dat");

    FileMetadata metadata = client.files().uploadBuilder(path)
        .withAutorename(false)
        .withMode(WriteMode.OVERWRITE)
        .withMute(true)
        .uploadAndFinish(new ByteArrayInputStream(contents));

    assertEquals(metadata.getSize(), contents.length);

    assertRangeDownload(client, path, contents, 0, contents.length);
    assertRangeDownload(client, path, contents, 0, 200);
    assertRangeDownload(client, path, contents, 300, 200);
    assertRangeDownload(client, path, contents, 499, 1);
    assertRangeDownload(client, path, contents, 0, 600);
    assertRangeDownload(client, path, contents, 250, null);
}
 
Example #4
Source File: UploadFileWorkitemHandler.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
public void executeWorkItem(WorkItem workItem,
                            WorkItemManager workItemManager) {
    try {

        RequiredParameterValidator.validate(this.getClass(),
                                            workItem);

        if (auth == null) {
            auth = new DropboxAuth();
        }

        client = auth.authorize(clientIdentifier,
                                accessToken);

        String dropboxPath = (String) workItem.getParameter("Path");
        Document documentToUpload = (Document) workItem.getParameter("Document");

        ByteArrayInputStream docStream = new ByteArrayInputStream(documentToUpload.getContent());

        client.files().uploadBuilder(dropboxPath + "/" + documentToUpload.getName())
                .withMode(WriteMode.ADD)
                .withClientModified(documentToUpload.getLastModified())
                .uploadAndFinish(docStream);

        workItemManager.completeWorkItem(workItem.getId(),
                                         null);
    } catch (Exception e) {
        logger.error("Unable to upload file: " + e.getMessage());
        handleException(e);
    }
}
 
Example #5
Source File: DropboxWorkitemHandlerTest.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    try {
        testDoc = new DocumentImpl();
        testDoc.setName("testDoc.txt");
        testDoc.setIdentifier("testDoc");
        testDoc.setLastModified(new Date());
        testDoc.setContent(new String("test doc content").getBytes());

        InputStream testInputStream =
                IOUtils.toInputStream("test doc content",
                                      "UTF-8");

        when(auth.authorize(anyString(),
                            anyString())).thenReturn(client);
        when(client.files()).thenReturn(fileRequests);

        // upload
        when(fileRequests.uploadBuilder(anyString())).thenReturn(uploadBuilder);
        when(uploadBuilder.withMode(any(WriteMode.class))).thenReturn(uploadBuilder);
        when(uploadBuilder.withClientModified(any(Date.class))).thenReturn(uploadBuilder);
        when(uploadBuilder.uploadAndFinish(any(java.io.InputStream.class))).thenReturn(metaData);

        // download
        when(fileRequests.downloadBuilder(anyString())).thenReturn(downloadBuilder);
        when(downloadBuilder.start()).thenReturn(downloader);
        when(downloader.getInputStream()).thenReturn(testInputStream);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
 
Example #6
Source File: DbxClientV2IT.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
@Test
public void testDownloadBuilder() throws Exception {
    DbxClientV2 client = ITUtil.newClientV2();
    String now = ITUtil.format(new Date());

    byte [] rtfV1 = ITUtil.toBytes("{\rtf1 sample {\b v1} (" + now + ")}");
    byte [] rtfV2 = ITUtil.toBytes("{\rtf1 sample {\b v2} (" + now + ")}");

    String path = ITUtil.path(getClass(), "/testDownloadBuilder/" + now + ".rtf");

    FileMetadata metadataV1 = client.files().uploadBuilder(path)
        .withAutorename(false)
        .withMode(WriteMode.ADD)
        .withMute(true)
        .uploadAndFinish(new ByteArrayInputStream(rtfV1));

    assertEquals(metadataV1.getPathLower(), path.toLowerCase());

    FileMetadata metadataV2 = client.files().uploadBuilder(path)
        .withAutorename(false)
        .withMode(WriteMode.OVERWRITE)
        .withMute(true)
        .uploadAndFinish(new ByteArrayInputStream(rtfV2));

    assertEquals(metadataV2.getPathLower(), path.toLowerCase());

    // ensure we have separate revisions
    assertNotEquals(metadataV1.getRev(), metadataV2.getRev());

    // now use download builder to set revision and make sure it works properly
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    client.files().downloadBuilder(path)
        .withRev(metadataV1.getRev())
        .download(out);
    assertEquals(out.toByteArray(), rtfV1);

    out = new ByteArrayOutputStream();
    client.files().downloadBuilder(path)
        .withRev(metadataV2.getRev())
        .download(out);
    assertEquals(out.toByteArray(), rtfV2);

    // ensure we still keep the non-builder optional route in our generator (for
    // backwards-compatibility)
    out = new ByteArrayOutputStream();
    client.files().download(path, metadataV1.getRev()).download(out);
    assertEquals(out.toByteArray(), rtfV1);

    out = new ByteArrayOutputStream();
    client.files().download(path, metadataV2.getRev()).download(out);
    assertEquals(out.toByteArray(), rtfV2);

    // and ensure we keep the required-only route
    out = new ByteArrayOutputStream();
    client.files().download(path).download(out);
    assertEquals(out.toByteArray(), rtfV2);
}
 
Example #7
Source File: DbxClientV2IT.java    From dropbox-sdk-java with MIT License 3 votes vote down vote up
private void testUploadAndDownload(DbxClientV2 client, boolean trackProgress) throws Exception {
    final byte [] contents = ITUtil.randomBytes(1024 << 8);
    String filename = "testUploadAndDownload.dat";
    String path = ITUtil.path(getClass(), "/" + filename);

    ProgressListener progressListener = null;
    if (trackProgress) {
        progressListener = createTestListener(contents.length);
    }

    FileMetadata metadata = client.files().uploadBuilder(path)
            .withAutorename(false)
            .withMode(WriteMode.ADD)
            .withMute(true)
            .uploadAndFinish(new ByteArrayInputStream(contents), progressListener);

    assertEquals(metadata.getName(), filename);
    assertEquals(metadata.getPathLower(), path.toLowerCase());
    assertEquals(metadata.getSize(), contents.length);

    Metadata actual = client.files().getMetadata(path);
    assertTrue(actual instanceof FileMetadata, actual.getClass().getCanonicalName());
    assertEquals(actual, metadata);

    if (trackProgress) {
        progressListener = createTestListener(contents.length);
    }

    DbxDownloader<FileMetadata> downloader = client.files().download(path);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    downloader.download(out, progressListener);

    byte [] actualContents = out.toByteArray();
    FileMetadata actualResult = downloader.getResult();

    assertEquals(actualResult, metadata);
    assertEquals(actualContents, contents);
    assertEquals(downloader.getContentType(), "application/octet-stream");

    Metadata deleted = client.files().delete(path);
    assertEquals(deleted, metadata);
}