Search in sources :

Example 6 with FileMetadata

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

the class Main method chunkedUploadFile.

/**
     * Uploads a file in chunks using multiple requests. This approach is preferred for larger files
     * since it allows for more efficient processing of the file contents on the server side and
     * also allows partial uploads to be retried (e.g. network connection problem will not cause you
     * to re-upload all the bytes).
     *
     * @param dbxClient Dropbox user authenticated client
     * @param localFIle local file to upload
     * @param dropboxPath Where to upload the file to within Dropbox
     */
private static void chunkedUploadFile(DbxClientV2 dbxClient, File localFile, String dropboxPath) {
    long size = localFile.length();
    // below to simplify the logic.
    if (size < CHUNKED_UPLOAD_CHUNK_SIZE) {
        System.err.println("File too small, use upload() instead.");
        System.exit(1);
        return;
    }
    long uploaded = 0L;
    DbxException thrown = null;
    // Chunked uploads have 3 phases, each of which can accept uploaded bytes:
    //
    //    (1)  Start: initiate the upload and get an upload session ID
    //    (2) Append: upload chunks of the file to append to our session
    //    (3) Finish: commit the upload and close the session
    //
    // We track how many bytes we uploaded to determine which phase we should be in.
    String sessionId = null;
    for (int i = 0; i < CHUNKED_UPLOAD_MAX_ATTEMPTS; ++i) {
        if (i > 0) {
            System.out.printf("Retrying chunked upload (%d / %d attempts)\n", i + 1, CHUNKED_UPLOAD_MAX_ATTEMPTS);
        }
        try (InputStream in = new FileInputStream(localFile)) {
            // if this is a retry, make sure seek to the correct offset
            in.skip(uploaded);
            // (1) Start
            if (sessionId == null) {
                sessionId = dbxClient.files().uploadSessionStart().uploadAndFinish(in, CHUNKED_UPLOAD_CHUNK_SIZE).getSessionId();
                uploaded += CHUNKED_UPLOAD_CHUNK_SIZE;
                printProgress(uploaded, size);
            }
            UploadSessionCursor cursor = new UploadSessionCursor(sessionId, uploaded);
            // (2) Append
            while ((size - uploaded) > CHUNKED_UPLOAD_CHUNK_SIZE) {
                dbxClient.files().uploadSessionAppendV2(cursor).uploadAndFinish(in, CHUNKED_UPLOAD_CHUNK_SIZE);
                uploaded += CHUNKED_UPLOAD_CHUNK_SIZE;
                printProgress(uploaded, size);
                cursor = new UploadSessionCursor(sessionId, uploaded);
            }
            // (3) Finish
            long remaining = size - uploaded;
            CommitInfo commitInfo = CommitInfo.newBuilder(dropboxPath).withMode(WriteMode.ADD).withClientModified(new Date(localFile.lastModified())).build();
            FileMetadata metadata = dbxClient.files().uploadSessionFinish(cursor, commitInfo).uploadAndFinish(in, remaining);
            System.out.println(metadata.toStringMultiline());
            return;
        } catch (RetryException ex) {
            thrown = ex;
            // RetryExceptions are never automatically retried by the client for uploads. Must
            // catch this exception even if DbxRequestConfig.getMaxRetries() > 0.
            sleepQuietly(ex.getBackoffMillis());
            continue;
        } catch (NetworkIOException ex) {
            thrown = ex;
            // network issue with Dropbox (maybe a timeout?) try again
            continue;
        } catch (UploadSessionLookupErrorException ex) {
            if (ex.errorValue.isIncorrectOffset()) {
                thrown = ex;
                // server offset into the stream doesn't match our offset (uploaded). Seek to
                // the expected offset according to the server and try again.
                uploaded = ex.errorValue.getIncorrectOffsetValue().getCorrectOffset();
                continue;
            } else {
                // Some other error occurred, give up.
                System.err.println("Error uploading to Dropbox: " + ex.getMessage());
                System.exit(1);
                return;
            }
        } catch (UploadSessionFinishErrorException ex) {
            if (ex.errorValue.isLookupFailed() && ex.errorValue.getLookupFailedValue().isIncorrectOffset()) {
                thrown = ex;
                // server offset into the stream doesn't match our offset (uploaded). Seek to
                // the expected offset according to the server and try again.
                uploaded = ex.errorValue.getLookupFailedValue().getIncorrectOffsetValue().getCorrectOffset();
                continue;
            } else {
                // some other error occurred, give up.
                System.err.println("Error uploading to Dropbox: " + ex.getMessage());
                System.exit(1);
                return;
            }
        } catch (DbxException ex) {
            System.err.println("Error uploading to Dropbox: " + ex.getMessage());
            System.exit(1);
            return;
        } catch (IOException ex) {
            System.err.println("Error reading from file \"" + localFile + "\": " + ex.getMessage());
            System.exit(1);
            return;
        }
    }
    // if we made it here, then we must have run out of attempts
    System.err.println("Maxed out upload attempts to Dropbox. Most recent error: " + thrown.getMessage());
    System.exit(1);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileMetadata(com.dropbox.core.v2.files.FileMetadata) NetworkIOException(com.dropbox.core.NetworkIOException) IOException(java.io.IOException) RetryException(com.dropbox.core.RetryException) FileInputStream(java.io.FileInputStream) Date(java.util.Date) NetworkIOException(com.dropbox.core.NetworkIOException) UploadSessionLookupErrorException(com.dropbox.core.v2.files.UploadSessionLookupErrorException) UploadSessionFinishErrorException(com.dropbox.core.v2.files.UploadSessionFinishErrorException) UploadSessionCursor(com.dropbox.core.v2.files.UploadSessionCursor) CommitInfo(com.dropbox.core.v2.files.CommitInfo) DbxException(com.dropbox.core.DbxException)

Example 7 with FileMetadata

use of com.dropbox.core.v2.files.FileMetadata 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 8 with FileMetadata

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

the class DropboxBrowse method doUpload.

// -------------------------------------------------------------------------------------------
// POST /upload
// -------------------------------------------------------------------------------------------
public void doUpload(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    MultipartConfigElement multipartConfigElement = new MultipartConfigElement("/tmp");
    request.setAttribute("org.eclipse.multipartConfig", multipartConfigElement);
    if (!common.checkPost(request, response))
        return;
    User user = common.requireLoggedInUser(request, response);
    if (user == null)
        return;
    DbxClientV2 dbxClient = requireDbxClient(request, response, user);
    if (dbxClient == null)
        return;
    try {
        // Just call getParts() to trigger the too-large exception.
        request.getParts();
    } catch (IllegalStateException ex) {
        response.sendError(400, "Request too large");
        return;
    }
    String targetFolder = slurpUtf8Part(request, response, "targetFolder", 1024);
    if (targetFolder == null)
        return;
    Part filePart = request.getPart("file");
    if (filePart == null) {
        response.sendError(400, "Field \"file\" is missing.");
        return;
    }
    String fileName = filePart.getName();
    if (fileName == null) {
        response.sendError(400, "Field \"file\" has no name.");
        return;
    }
    // Upload file to Dropbox
    String fullTargetPath = targetFolder + "/" + fileName;
    FileMetadata metadata;
    try {
        metadata = dbxClient.files().upload(fullTargetPath).uploadAndFinish(filePart.getInputStream());
    } catch (DbxException ex) {
        common.handleDbxException(response, user, ex, "upload(" + jq(fullTargetPath) + ", ...)");
        return;
    } catch (IOException ex) {
        response.sendError(400, "Error getting file data from you.");
        return;
    }
    // Display uploaded file metadata.
    response.setContentType("text/html");
    response.setCharacterEncoding("utf-8");
    PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
    out.println("<html>");
    out.println("<head><title>File uploaded: " + escapeHtml4(metadata.getPathLower()) + "</title></head>");
    out.println("<body>");
    out.println("<h2>File uploaded: " + escapeHtml4(metadata.getPathLower()) + "</h2>");
    out.println("<pre>");
    out.print(escapeHtml4(metadata.toStringMultiline()));
    out.println("</pre>");
    out.println("</body>");
    out.println("</html>");
    out.flush();
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) MultipartConfigElement(javax.servlet.MultipartConfigElement) Part(javax.servlet.http.Part) FileMetadata(com.dropbox.core.v2.files.FileMetadata) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) DbxException(com.dropbox.core.DbxException) PrintWriter(java.io.PrintWriter)

Example 9 with FileMetadata

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

the class DbxClientV2IT method testDownloadBuilder.

@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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileMetadata(com.dropbox.core.v2.files.FileMetadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 10 with FileMetadata

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

the class DbxClientV2Test method testRetrySuccessWithBackoff.

@Test
public void testRetrySuccessWithBackoff() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    FileMetadata expected = new FileMetadata("banana.png", "id:eRsVsAya9YAAAAAAAAAAAQ", new Date(1456173312172L), new Date(1456173312172L), "89df885732c38", 12345L);
    // 503 twice, then return result
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(// no backoff
    createEmptyResponse(503)).thenReturn(// backoff 1 sec
    createRateLimitResponse(1)).thenReturn(// backoff 2 sec
    createRateLimitResponse(2)).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    long start = System.currentTimeMillis();
    Metadata actual = client.files().getMetadata(expected.getId());
    long end = System.currentTimeMillis();
    // no way easy way to properly test this, but request should
    // have taken AT LEAST 3 seconds due to backoff.
    assertTrue((end - start) >= 3000L, "duration: " + (end - start) + " millis");
    // should have been called 4 times: initial call + 3 retries
    verify(mockRequestor, times(4)).startPost(anyString(), anyHeaders());
    assertEquals(actual.getName(), expected.getName());
    assertTrue(actual instanceof FileMetadata, actual.getClass().toString());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

FileMetadata (com.dropbox.core.v2.files.FileMetadata)16 Metadata (com.dropbox.core.v2.files.Metadata)7 Date (java.util.Date)6 DbxException (com.dropbox.core.DbxException)5 Test (org.testng.annotations.Test)5 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)4 FolderMetadata (com.dropbox.core.v2.files.FolderMetadata)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 HttpRequestor (com.dropbox.core.http.HttpRequestor)3 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)3 DeletedMetadata (com.dropbox.core.v2.files.DeletedMetadata)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 NetworkIOException (com.dropbox.core.NetworkIOException)2 ListFolderResult (com.dropbox.core.v2.files.ListFolderResult)2 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 FloatingActionButton (android.support.design.widget.FloatingActionButton)1