package up6;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.jsp.JspWriter;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import up6.model.FileInf;
import up6.store.MinioAuthorization;
import up6.store.MinioTool;

public class tester {
	
	/**
	 * https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html
	 * @param w
	 */
	public void s3_put_object_test(JspWriter w) {
		/*
		 * try { w.write("<br/>s3 put object 测试2<br/>"); } catch (IOException e1) { //
		 * TODO Auto-generated catch block e1.printStackTrace(); }
		 * 
		 * 
		 * OkHttpClient c = new OkHttpClient(); //2.创建Request对象，设置一个url地址（百度地址）,设置请求方式。
		 * String url = "http://192.168.0.111:9001/test//123.txt?uploads"; MediaType
		 * JSON = MediaType.parse("application/octet-stream; charset=utf-8"); String
		 * json="123456"; RequestBody body = RequestBody.create(JSON,json);
		 * 
		 * 
		 * MinioAuthorization auth = new MinioAuthorization(); auth.setMethod("POST")
		 * .setData(json) .setUrl(url)
		 * .setContentType("application/octet-stream; charset=utf-8");
		 * 
		 * try { w.write(auth.getTimeIso()+"<br/>"); String authstr =
		 * auth.Authorization();
		 * 
		 * Request r = new Request.Builder() .url(url) .post(body)
		 * .header("Content-Length", "6") //.header("Content-Md5", auth.dataMd5)
		 * .header("x-amz-date", auth.getTimeIso()) .header("x-amz-content-sha256",
		 * auth.data_sha256_Hash) //.header("x-amz-content-sha256", auth.getDataHash())
		 * .header("Authorization", authstr) .build(); Call call = c.newCall(r);
		 * 
		 * w.write("签名值："+authstr+"<br/>");
		 * 
		 * w.write("<br/>开始请求<br/>"); //同步调用,返回Response,会抛出IO异常 Response res =
		 * call.execute(); w.write("请求结束<br/>"); w.write( res.body().string() );
		 * w.write("返回头：\n"+res.headers().toString());
		 * System.out.println("返回头：\n"+res.headers().toString());
		 * 
		 * } catch (Exception e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */
	}
	
	public void putObject_test() {
		MinioTool.putObject("1234.txt", "aabbccdd".getBytes());
	}
	
	public void createMultipartUploader_test() {
		String key="daemon4304-lite.exe";
		String id = MinioTool.CreateMultipartUpload(key);
		String pathLoc="D:\\daemon4304-lite.exe";
		int len = 5242880;		
		byte[] data = new byte[len];//5MB		
		File f = new File(pathLoc);
		long fileLen = f.length();
		RandomAccessFile raf;
		try {
			raf = new RandomAccessFile(f,"r");
			raf.read(data, 0, len);
			MinioTool.UploadPart(key, 1, id, data);
			raf.seek(len);
			raf.close();			
			//data = new byte[(int)fileLen-len];
			//raf.read(data, len-1, (int)fileLen-len-1);
			//MinioTool.UploadPart(key, 2, id, data);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * API:
	 * https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html
	 * @param w
	 */
	public void create_part_test(JspWriter w) {
		OkHttpClient c = new OkHttpClient();
		//2.创建Request对象，设置一个url地址（百度地址）,设置请求方式。
		String url = "http://192.168.0.111:9001/test/test1.txt?uploads";
		String sk = "minioadmin";
		String ak = "minioadmin";

		MinioAuthorization auth = new MinioAuthorization();		
		try {
			w.write(auth.getTimeIso()+"<br/>");			
			w.write(auth.Authorization()+"<br/>");
			Request r = new Request.Builder()
					.url(url)
					.header("x-amz-date", auth.getTimeIso())
					.header("Authorization", auth.Authorization())
					.build();
			Call call = c.newCall(r);

			w.write("<br/>开始请求<br/>");
            //同步调用,返回Response,会抛出IO异常
            Response res = call.execute();
            w.write("请求结束<br/>");
			w.write( res.body().string() );		
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void run(JspWriter w) 
	{
		try {
			w.write("开始请求3<br/>");
			w.write(Md5Tool.base16("m")+"<br/>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//this.putObject_test();
		//this.createMultipartUploader_test();
		
		FileInf f = new FileInf();
		f.pathSvr="F:/etags.txt";
		System.out.println(f.etags());
	}
}