package dropbox;

import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.v2.files.FileMetadata;
import com.sendgrid.Attachments;
import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
/*
    Add Dropbox dependencies for compiling code
    Gradle : compile 'com.dropbox.core:dropbox-core-sdk:3.0.5'
    maven :
    <dependency>
       <groupId>com.dropbox.core</groupId>
       <artifactId>dropbox-core-sdk</artifactId>
       <version>3.0.5</version>
    </dependency>
 */
public class UploadToDropBox {
    public static void main(String[] args) {
        String fileName = "book3.txt";
        String type = "text/plain";
        String content = "This test checks if the builder works fine";
        InputStream contentStream = new ByteArrayInputStream(content.getBytes(Charset.forName("UTF-8")));
        String contentId = "someId";
        String dispositon = "someDisposition";
        String accessToken = "user's access token";
        String path = "/path/to/folder";

        Attachments attachments = new Attachments.Builder(fileName, contentStream)
                .withType(type)
                .withContentId(contentId)
                .withDisposition(dispositon)
                .build();
        String returnPath = uploadToDropbox(attachments, accessToken, path);
        System.out.println("Path: " + returnPath);
    }

    /**
     * Uploads attachment to Dropbox
     *
     * @param attachments attachment to be uploaded
     * @param accessToken user's Dropbox access token
     * @param path path of the folder in which attachment needs to be added. Should not end with /
     * @return the full path to the uploaded file
     */
    private static String uploadToDropbox(Attachments attachments, String accessToken, String path){
        try {
            DbxRequestConfig config = DbxRequestConfig.newBuilder("sendgrid/0.1").build();
            DbxClientV2 client = new DbxClientV2(config, accessToken);
            FileMetadata uploadedFile = client.files().upload(String.format("%s/%s", path, attachments.getFilename()))
                    .uploadAndFinish(new ByteArrayInputStream((Base64.decodeBase64(attachments.getContent()))));
            return uploadedFile.getPathDisplay();
        }catch(Exception ex) {
            throw new RuntimeException("Error while uploading to Dropbox", ex);
        }
    }

}


@@ -319,16 +319,17 @@
 harset;%0A
+%0A
 /*%0A    A
@@ -1206,28 +1206,24 @@
             
-    
 .withType(ty
@@ -1226,20 +1226,16 @@
 e(type)%0A
-    
         
@@ -1272,28 +1272,24 @@
             
-    
 .withDisposi
@@ -1305,20 +1305,16 @@
 ositon)%0A
-    
         
@@ -1639,16 +1639,23 @@
 am path 
+       
 path of 
@@ -1883,16 +1883,17 @@
 ng path)
+ 
 %7B%0A      
@@ -2197,20 +2197,16 @@
         
-    
 .uploadA
@@ -2353,13 +2353,15 @@
    %7D
+ 
 catch
+ 
 (Exc
@@ -2473,8 +2473,6 @@
   %7D%0A
-%0A
 %7D%0A
-%0A

format example