package seaweedfs.client;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class SeaweedWrite {

    public static void writeData(FilerProto.Entry.Builder entry,
                                 final String replication,
                                 final FilerGrpcClient filerGrpcClient,
                                 final long offset,
                                 final byte[] bytes,
                                 final long bytesOffset, final long bytesLength) throws IOException {
        FilerProto.AssignVolumeResponse response = filerGrpcClient.getBlockingStub().assignVolume(
            FilerProto.AssignVolumeRequest.newBuilder()
                .setCollection("")
                .setReplication(replication)
                .setDataCenter("")
                .setReplication("")
                .setTtlSec(0)
                .build());
        String fileId = response.getFileId();
        String url = response.getUrl();
        String targetUrl = String.format("http://%s/%s", url, fileId);

        String etag = multipartUpload(targetUrl, bytes, bytesOffset, bytesLength);

        entry.addChunks(FilerProto.FileChunk.newBuilder()
            .setFileId(fileId)
            .setOffset(offset)
            .setSize(bytesLength)
            .setMtime(System.currentTimeMillis() / 10000L)
            .setETag(etag)
        );

    }

    public static void writeMeta(final FilerGrpcClient filerGrpcClient,
                                 final String parentDirectory, final FilerProto.Entry.Builder entry) {
        filerGrpcClient.getBlockingStub().createEntry(
            FilerProto.CreateEntryRequest.newBuilder()
                .setDirectory(parentDirectory)
                .setEntry(entry)
                .build()
        );
    }

    private static String multipartUpload(String targetUrl,
                                          final byte[] bytes,
                                          final long bytesOffset, final long bytesLength) throws IOException {

        CloseableHttpClient client = HttpClientBuilder.create().setUserAgent("hdfs-client").build();

        InputStream inputStream = new ByteArrayInputStream(bytes, (int) bytesOffset, (int) bytesLength);

        HttpPost post = new HttpPost(targetUrl);

        post.setEntity(MultipartEntityBuilder.create()
            .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
            .addBinaryBody("upload", inputStream)
            .build());

        try {
            HttpResponse response = client.execute(post);

            String etag = response.getLastHeader("ETag").getValue();

            if (etag != null && etag.startsWith("\"") && etag.endsWith("\"")) {
                etag = etag.substring(1, etag.length() - 1);
            }

            return etag;
        } finally {
            client.close();
        }

    }
}

@@ -958,32 +958,36 @@
 me(%0A            
+    
 FilerProto.Assig
@@ -1026,24 +1026,32 @@
             
+        
 .setCollecti
@@ -1065,32 +1065,40 @@
                 
+ 
+       
 .setReplication(
@@ -1122,24 +1122,32 @@
             
+      
+  
 .setDataCent
@@ -1149,24 +1149,32 @@
 aCenter(%22%22)%0A
+        
             
@@ -1209,24 +1209,32 @@
             
+   
+     
 .setTtlSec(0
@@ -1235,16 +1235,24 @@
 lSec(0)%0A
+        
         
@@ -1352,24 +1352,66 @@
 e.getUrl();%0A
+        String auth = response.getAuth();%0A
         Stri
@@ -1518,16 +1518,22 @@
 rgetUrl,
+ auth,
  bytes, 
@@ -1626,24 +1626,28 @@
             
+    
 .setFileId(f
@@ -1661,24 +1661,28 @@
             
+   
+ 
 .setOffset(o
@@ -1688,16 +1688,20 @@
 offset)%0A
+    
         
@@ -1734,24 +1734,28 @@
             
+  
+  
 .setMtime(Sy
@@ -1789,16 +1789,20 @@
 10000L)%0A
+    
         
@@ -2073,32 +2073,36 @@
 ry(%0A            
+    
 FilerProto.Creat
@@ -2140,24 +2140,32 @@
             
+        
 .setDirector
@@ -2195,24 +2195,32 @@
             
+  
+      
 .setEntry(en
@@ -2220,24 +2220,32 @@
 ntry(entry)%0A
+        
             
@@ -2335,16 +2335,71 @@
 getUrl,%0A
+                                          String auth,%0A
         
@@ -2820,16 +2820,139 @@
 getUrl);
+%0A        if (auth != null && auth.length() != 0) %7B%0A            post.addHeader(%22Authorization%22, %22BEARER %22 + auth);%0A        %7D
 %0A%0A      
@@ -3000,16 +3000,20 @@
 reate()%0A
+    
         
@@ -3075,16 +3075,20 @@
         
+    
 .addBina
@@ -3117,16 +3117,20 @@
 Stream)%0A
+    
         

add jwt auth for uploading