using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.IO;

public class LogUploader : MonoBehaviour
{
    private static string LOG_UPLOAD_URL = "http://localhost:18800/upload_log.php";

    public static void StartUploadLog(string logFilePath, string desc)
    {
        var go = new GameObject("LogUploader");
        var bhv = go.AddComponent<LogUploader>();
        // 开启一个协程上传日志
        bhv.StartCoroutine(bhv.UploadLog(logFilePath, LOG_UPLOAD_URL, desc));
    }

    private IEnumerator UploadLog(string logFilePath, string url, string desc)
    {
        var fileName = Path.GetFileName(logFilePath);
        var data = ReadLogFile(logFilePath);
        WWWForm form = new WWWForm();
        // 塞入描述字段，字段名与服务端约定好
        form.AddField("desc", desc);
        // 塞入日志字节流字段，字段名与服务端约定好
        form.AddBinaryData("logfile", data, fileName, "application/x-gzip");
        // 使用UnityWebRequest
        UnityWebRequest request = UnityWebRequest.Post(url, form);
        var result = request.SendWebRequest();

        while (!result.isDone)
        {
            yield return null;
        }
        if (!string.IsNullOrEmpty(request.error))
        {
            GameLogger.LogError(request.error);
        }
        else
        {
            GameLogger.LogError("日志上传完毕, 服务器返回信息: " + request.downloadHandler.text);
        }
        request.Dispose();
    }

    private byte[] ReadLogFile(string logFilePath)
    {
        byte[] data = null;
        using (FileStream fs = File.OpenRead(logFilePath))
        {
            int index = 0;
            long len = fs.Length;
            data = new byte[len];
            // 根据你的需求进行限流读取
            int offset = data.Length > 1024 ? 1024 : data.Length;
            while (index < len)
            {
                int readByteCnt = fs.Read(data, index, offset);
                index += readByteCnt;
                long leftByteCnt = len - index;
                offset = leftByteCnt > offset ? offset : (int)leftByteCnt;
            }
        }
        return data;
    }
}