123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using COSXML;
- using COSXML.Auth;
- using COSXML.Model.Object;
- using COSXML.Model.Bucket;
- using COSXML.CosException;
- using System;
- using Newtonsoft.Json;
- using LitJson;
- using System.Threading.Tasks;
- using COSXML.Transfer;
- using System.IO;
- using COSXML.Model;
- using Newtonsoft.Json.Linq;
- public class COSDownLoad:MonoSingleton<COSDownLoad>
- {
-
- public SuccessCOS materialSuccessCos;
- public SuccessCOS sceneSuccessCos;
- public SuccessCOS imageSuccessCos;
- public SuccessCOS publicSuccessCos;
-
- CosXml materialCosXml;
- CosXml sceneCosXml;
- CosXml imageCosXml;
- CosXml publicCosXml;
-
-
- private void Start()
- {
-
-
- }
- public void Credential()
- {
- Credential credential = new Credential();
- credential.type = "material";
- string data = JsonConvert.SerializeObject(credential);
-
- HttpTool.Instance.Post("/storage/credential/create", data, CredentialMaterialCallBack);
- Credential credential2 = new Credential();
- credential2.type = "scene";
- string data2 = JsonConvert.SerializeObject(credential2);
- HttpTool.Instance.Post("/storage/credential/create", data2, CredentialSceneCallBack);
- Credential credential3 = new Credential();
- credential3.type = "image";
- string data3 = JsonConvert.SerializeObject(credential3);
- HttpTool.Instance.Post("/storage/credential/create", data3, CredentialImageCallBack);
- }
- private void InitMaterialCos()
- {
- CosXmlConfig config = new CosXmlConfig.Builder()
- .IsHttps(true)
- .SetRegion(materialSuccessCos.region)
- .SetDebugLog(true)
- .Build();
- QCloudCredentialProvider cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(
- materialSuccessCos.credentials.tmpSecretId, materialSuccessCos.credentials.tmpSecretKey, materialSuccessCos.expiredTime, materialSuccessCos.credentials.sessionToken);
- materialCosXml = new CosXmlServer(config, cosCredentialProvider);
- }
- private void InitImageCos()
- {
- CosXmlConfig config = new CosXmlConfig.Builder()
- .IsHttps(true)
- .SetRegion(imageSuccessCos.region)
- .SetDebugLog(true)
- .Build();
- QCloudCredentialProvider cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(
- imageSuccessCos.credentials.tmpSecretId, imageSuccessCos.credentials.tmpSecretKey, imageSuccessCos.expiredTime, imageSuccessCos.credentials.sessionToken);
- imageCosXml = new CosXmlServer(config, cosCredentialProvider);
- }
- private void InitSceneCos()
- {
- CosXmlConfig config = new CosXmlConfig.Builder()
- .IsHttps(true)
- .SetRegion(sceneSuccessCos.region)
- .SetDebugLog(true)
- .Build();
- QCloudCredentialProvider cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(
- sceneSuccessCos.credentials.tmpSecretId, sceneSuccessCos.credentials.tmpSecretKey, sceneSuccessCos.expiredTime, sceneSuccessCos.credentials.sessionToken);
- sceneCosXml = new CosXmlServer(config, cosCredentialProvider);
- }
- private void CredentialMaterialCallBack( string msg)
- {
- Debug.Log(msg);
- JObject jObject = JObject.Parse(msg);
- if (jObject["code"].ToString() == "200")
- {
- materialSuccessCos = JsonConvert.DeserializeObject<SuccessCOS>(jObject["data"].ToString());
- InitMaterialCos();
- }
- else
- {
- Debug.LogError(msg);
- }
- }
- private void CredentialSceneCallBack(string msg)
- {
- Debug.Log(msg);
- JObject jObject = JObject.Parse(msg);
- if (jObject["code"].ToString() == "200")
- {
- sceneSuccessCos = JsonConvert.DeserializeObject<SuccessCOS>(jObject["data"].ToString());
- InitSceneCos();
- }
- else
- {
- Debug.LogError(msg);
- }
- }
- private void CredentialImageCallBack(string msg)
- {
- Debug.Log(msg);
- JObject jObject = JObject.Parse(msg);
- if (jObject["code"].ToString() == "200")
- {
- imageSuccessCos = JsonConvert.DeserializeObject<SuccessCOS>(jObject["data"].ToString());
- InitImageCos();
- }
- else
- {
- Debug.LogError(msg);
- }
- }
- public static int showDownLoadCount;
- public static int showDownEndLoadCount;
- public static float showDownLoadPre;
- public async void TestDownloadObject( string downloadPath,string localDir)
- {
- TransferConfig transferConfig = new TransferConfig();
- TransferManager transferManager;
- String bucket;
- transferManager = new TransferManager(publicCosXml, transferConfig);
- bucket = publicSuccessCos.bucket;
- string localFileName = Path.GetFileName(downloadPath);
-
-
- COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, downloadPath,
- localDir, localFileName);
-
-
- downloadTask.progressCallback = delegate (long completed, long total)
- {
-
-
- };
- Debug.Log(downloadPath + "||||||||| DGJ");
-
-
- try
- {
-
- COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = await
- transferManager.DownloadAsync(downloadTask);
- Console.WriteLine(result.GetResultInfo());
- string eTag = result.eTag;
- }
- catch (COSXML.CosException.CosClientException clientEx)
- {
-
- Debug.LogError(downloadPath + " CosClientException: " + clientEx);
- DownloadResManager.Instance.DownLoadFaild(downloadTask.localFileName);
-
- }
- catch (COSXML.CosException.CosServerException serverEx)
- {
-
- Debug.LogError(downloadPath + " CosServerException: " + serverEx.GetInfo());
- DownloadResManager.Instance.DownLoadFaild(downloadTask.localFileName);
-
- }
- }
-
-
-
-
-
-
-
- public async void TransferDownloadObject(DownLoadMaterial downloadMaterial, string localDir)
- {
-
- TransferConfig transferConfig = new TransferConfig();
-
-
-
-
- TransferManager transferManager;
- String bucket;
- Debug.Log("Hjj TransferDownloadObject===>"+ downloadMaterial.type);
- switch (downloadMaterial.type)
- {
- case "-1":
- transferManager = new TransferManager(sceneCosXml, transferConfig);
- bucket = sceneSuccessCos.bucket;
- break;
- case "-2":
- transferManager = new TransferManager(imageCosXml, transferConfig);
- bucket = imageSuccessCos.bucket;
- break;
-
- default:
- transferManager = new TransferManager(materialCosXml, transferConfig);
- if(materialSuccessCos!=null)
- bucket = materialSuccessCos.bucket;
- else
- {
- showDownEndLoadCount++;
- return;
- }
- break;
- }
-
-
-
-
-
- string localFileName = Path.GetFileName(downloadMaterial.localLoadPath);
- localDir = downloadMaterial.localLoadPath.Substring(0, downloadMaterial.localLoadPath.Length - localFileName.Length);
-
- COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, downloadMaterial.downLoadPath,
- localDir, localFileName);
-
-
- downloadTask.progressCallback = delegate (long completed, long total)
- {
-
- showDownLoadPre = completed * 100.0f / total;
- };
- Debug.Log(downloadMaterial.downLoadPath +"||||||||| DGJ");
- downloadTask.onState += OnState;
- listDownLoadTask.Add(downloadTask);
- try
- {
- COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = await
- transferManager.DownloadAsync(downloadTask);
- Console.WriteLine(result.GetResultInfo());
- string eTag = result.eTag;
- }
- catch (COSXML.CosException.CosClientException clientEx)
- {
-
- Debug.LogError(downloadMaterial.downLoadPath + " CosClientException: " + clientEx);
- DownloadResManager.Instance.DownLoadFaild(downloadTask.localFileName);
- listDownLoadTask.Remove(downloadTask);
- showDownEndLoadCount++;
- }
- catch (COSXML.CosException.CosServerException serverEx)
- {
-
- Debug.LogError(downloadMaterial.downLoadPath+ " CosServerException: " + serverEx.GetInfo());
- DownloadResManager.Instance.DownLoadFaild(downloadTask.localFileName);
- listDownLoadTask.Remove(downloadTask);
- showDownEndLoadCount++;
- }
-
- }
- private List<COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult> listResult = new List<COSXMLDownloadTask.DownloadTaskResult>();
- private List<COSXMLDownloadTask> listDownLoadTask = new List<COSXMLDownloadTask>();
-
-
-
-
-
- public async void TransferBatchDownloadObjects( List<DownLoadMaterial> listDLMaterial , string localDir)
- {
- if (listDLMaterial.Count == 0 || listDLMaterial == null)
- return;
-
- TransferConfig transferConfig = new TransferConfig();
-
- TransferManager transferManager = new TransferManager(materialCosXml, transferConfig);
-
- string bucket = materialSuccessCos.bucket;
-
- Debug.Log(listDLMaterial.Count);
- for (int i = 0; i < listDLMaterial.Count; i++)
- {
- string cosPath = listDLMaterial[i].downLoadPath;
- Debug.Log(cosPath);
- if(cosPath==null||cosPath=="")
- {
- DownloadResManager.Instance.DownLoadFaild(listDLMaterial[i].localLoadPath);
- continue;
- }
- string localFileName = Path.GetFileName(listDLMaterial[i].localLoadPath);
- Debug.Log(cosPath + " " + localDir + localFileName);
- COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath,
- localDir, localFileName);
- downloadTask.failCallback = delegate (CosClientException clientException, CosServerException serverException)
- {
- Debug.LogError("clientException.errorCode : " + clientException.errorCode + " serverException.errorCode: " + serverException.errorCode +
- " serverException.errorMessage : " + serverException.errorMessage);
- };
- downloadTask.onState += OnState;
- try
- {
- await transferManager.DownloadAsync(downloadTask);
- }
- catch (COSXML.CosException.CosClientException clientEx)
- {
-
- Debug.LogError("CosClientException: " + clientEx);
- DownloadResManager.Instance.DownLoadFaild(downloadTask.localFileName);
- listDownLoadTask.RemoveAt(0);
- }
- catch (COSXML.CosException.CosServerException serverEx)
- {
-
- Debug.LogError("CosServerException: " + serverEx.GetInfo());
- DownloadResManager.Instance.DownLoadFaild(downloadTask.localFileName);
- listDownLoadTask.RemoveAt(0);
- }
-
- listDownLoadTask.Add(downloadTask);
- }
- }
- public Queue<TaskState> taskQueue = new Queue<TaskState>();
- private void OnState(TaskState state)
- {
- taskQueue.Enqueue(state);
- }
- private void Update()
- {
- if(taskQueue.Count>0)
- {
- var state = taskQueue.Dequeue();
- if (state == TaskState.Completed || state == TaskState.Failed)
- {
- int ct = listDownLoadTask.Count - 1;
- for (int i = ct; i >= 0; i--)
- {
- if (listDownLoadTask[i].State() == TaskState.Completed)
- {
- showDownEndLoadCount++;
- Debug.Log(listDownLoadTask.Count + " " + listDownLoadTask[i].State() + " " + listDownLoadTask[i].localFileName);
- DownloadResManager.Instance.DownLoadCompleted(listDownLoadTask[i].localFileName);
- listDownLoadTask.RemoveAt(i);
- }else if (listDownLoadTask[i].State() == TaskState.Failed)
- {
- showDownEndLoadCount++;
- Debug.Log(listDownLoadTask.Count + " " + listDownLoadTask[i].State() + " " + listDownLoadTask[i].localFileName);
- DownloadResManager.Instance.DownLoadFaild(listDownLoadTask[i].localFileName);
- listDownLoadTask.RemoveAt(i);
- }
- }
- }
- }
- }
- }
- public class Credential
- {
- public string type { get; set; }
- }
- public class SuccessCOS
- {
- public CredentialsCos credentials { get; set; }
- public long startTime { get; set; }
- public long expiredTime { get; set; }
- public string bucket { get; set; }
- public string path { get; set; }
- public string region { get; set; }
- }
- public class CredentialsCos
- {
- public string tmpSecretId { get; set; }
- public string tmpSecretKey { get; set; }
- public string sessionToken { get; set; }
- }
|