package com.jld.vod.utils.http;

import android.content.Context;
import android.util.Log;
import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by zhaofuxin on 2019/9/12.
 */

public class HttpFileUtil {
    private static OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(10000, TimeUnit.MILLISECONDS)
            .readTimeout(10000,TimeUnit.MILLISECONDS)
            .writeTimeout(10000, TimeUnit.MILLISECONDS).
                    build();

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8");
    public static void postFile(Context mContext,String url, final ProgressListener listener, okhttp3.Callback callback, File videopath,Long mid,Long resId){
        okHttpClient.dispatcher().setMaxRequests(1);
        okHttpClient.dispatcher().setMaxRequestsPerHost(5);
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);


        //第一个参数要与Servlet中的一致
        builder.addFormDataPart("file",videopath.getName(), RequestBody.create(MediaType.parse("application/octet-stream"),videopath));
        builder.addFormDataPart("mid",mid.toString());
        builder.addFormDataPart("resId",resId.toString());

        MultipartBody multipartBody = builder.build();
        Request request  = new Request.Builder().url(url).post(new ProgressRequestBody(multipartBody,listener)).build();
        okHttpClient.newCall(request).enqueue(callback);

    }
}