DownLoadXRManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using COSXML;
  2. using LitJson;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using UnityEngine;
  9. using XRTool.Util;
  10. using static XunJianDataManager;
  11. public class DownLoadXRManager
  12. {
  13. public static CredentialConfig cospingzheng;
  14. public static Dictionary<string, byte[]> downLoadCache = new Dictionary<string, byte[]>();
  15. public static bool DownLoadForBytes(DownLoadResources msg, Action<byte[]> bytes, Action<float> presson)
  16. {
  17. getdata((CredentialConfig c) => {
  18. choosedownloadtype(msg, bytes, presson);
  19. });
  20. return true;
  21. }
  22. static void choosedownloadtype(DownLoadResources msg, Action<byte[]> bytes, Action<float> presson)
  23. {
  24. DownLoadConfig config = new DownLoadConfig();
  25. config.bytes = bytes;
  26. config.presson = presson;
  27. config.data = msg;
  28. GameObject go = null;
  29. if(cospingzheng!=null)
  30. {
  31. switch (cospingzheng.drive)
  32. {
  33. case "minio":
  34. #if ISLOCAL
  35. go = GameObject.Instantiate(Resources.Load<GameObject>("DownLoadMinIOXRingItem"));
  36. #else
  37. go = GameObject.Instantiate(GameInit.ablist.LoadAsset<GameObject>("DownLoadMinIOXRingItem"));
  38. #endif
  39. DownLoadMinIOXRItem downLoadMinIOXRItem = go.GetComponent<DownLoadMinIOXRItem>();
  40. downLoadMinIOXRItem.startDownload(config);
  41. /*
  42. //string bucket = data["data"]["bucket"].ToString();
  43. //string objectName = data["data"]["bucket"].ToString();
  44. string Url = data["url"].ToString();
  45. string uuid = data["uuid"].ToString();
  46. Debug.Log("DGJ ===> DownLoadForBytes " + msg);
  47. RoomFileMinio.Instance.getFile(RoomFileMinio.Instance.bucket, Url, uuid, (RoomFileMinio.RoomFileData roomFile) =>
  48. {
  49. bytes?.Invoke(roomFile.bytes);
  50. });*/
  51. break;
  52. case "COS":
  53. #if ISLOCAL
  54. go = GameObject.Instantiate(Resources.Load<GameObject>("DownLoadCOSXRingItem"));
  55. #else
  56. go = GameObject.Instantiate(GameInit.ablist.LoadAsset<GameObject>("DownLoadCOSXRingItem"));
  57. #endif
  58. DownLoadCOSXRItem downLoadCOSXRItem = go.GetComponent<DownLoadCOSXRItem>();
  59. downLoadCOSXRItem.startDownload(config);
  60. break;
  61. default:
  62. #if ISLOCAL
  63. go = GameObject.Instantiate(Resources.Load<GameObject>("DownLoadURLXRingItem"));
  64. #else
  65. go = GameObject.Instantiate(GameInit.ablist.LoadAsset<GameObject>("DownLoadURLXRingItem"));
  66. #endif
  67. DownLoadURLXRItem downLoadURLXRItem = go.GetComponent<DownLoadURLXRItem>();
  68. downLoadURLXRItem.startDownload(config);
  69. break;
  70. }
  71. }
  72. else
  73. {
  74. #if ISLOCAL
  75. go = GameObject.Instantiate(Resources.Load<GameObject>("DownLoadURLXRingItem"));
  76. #else
  77. go = GameObject.Instantiate(GameInit.ablist.LoadAsset<GameObject>("DownLoadURLXRingItem"));
  78. #endif
  79. DownLoadURLXRItem downLoadURLXRItem = go.GetComponent<DownLoadURLXRItem>();
  80. downLoadURLXRItem.startDownload(config);
  81. }
  82. }
  83. public static bool DownLoadForFilePath(DownLoadResources msg, Action<string> bytes, Action<float> presson)
  84. {
  85. string Url = "";
  86. string uuid = "";
  87. Url = msg.path;
  88. uuid = msg.uuid;
  89. if (PlayerPrefs.HasKey(Url))
  90. {
  91. if (PlayerPrefs.HasKey(Url + "_"+ uuid))
  92. {
  93. if(File.Exists(PlayerPrefs.GetString(Url + "_" + uuid)))
  94. {
  95. bytes?.Invoke(PlayerPrefs.GetString(Url + "_" + uuid));
  96. return true;
  97. }
  98. }
  99. }
  100. return DownLoadForBytes(msg, (byte[] fileBytes) => {
  101. if(fileBytes!=null)
  102. {
  103. if (PlayerPrefs.HasKey(Url) && PlayerPrefs.HasKey(Url + "_" + uuid)&& File.Exists(PlayerPrefs.GetString(Url + "_" + uuid)))
  104. {
  105. bytes?.Invoke(PlayerPrefs.GetString(Url + "_" + uuid));
  106. }
  107. else
  108. {
  109. if (PlayerPrefs.HasKey(Url) && File.Exists(PlayerPrefs.GetString(Url )))
  110. {
  111. File.Delete(PlayerPrefs.GetString(Url));
  112. }
  113. //文件流信息
  114. //StreamWriter sw;
  115. Stream sw;
  116. string path = Application.persistentDataPath + "/DownLoadXR/" + Url.Split("/")[Url.Split("/").Length - 1];
  117. Debug.Log("准备存文件===》" + path);
  118. FileInfo file = new FileInfo(path);
  119. try
  120. {
  121. if (file.Exists)
  122. {
  123. file.Delete();
  124. }
  125. if (!Directory.Exists(Application.persistentDataPath + "/DownLoadXR"))
  126. {
  127. Directory.CreateDirectory(Application.persistentDataPath + "/DownLoadXR");
  128. }
  129. //如果此文件存在则打开
  130. //sw = file .Append();
  131. //如果此文件不存在则创建
  132. sw = file.Create();
  133. //以行的形式写入信息
  134. //sw.WriteLine(info);
  135. sw.Write(fileBytes, 0, fileBytes.Length);
  136. sw.Close();
  137. sw.Dispose();
  138. }
  139. catch
  140. {
  141. /*
  142. JsonData data = new JsonData();
  143. data["type"] = "fileError";
  144. List<string> backTip = new List<string>();
  145. backTip.Add(data.ToJson());
  146. backTip.Add(data.ToJson());
  147. backTip.Add(data.ToJson());
  148. WindowsManager.Instance.show(WindowConfig.windowType.Error, false, WindowsManager.Instance.getErrorData("文件提示", "文件存储失败"+ path, Color.white, "icon", backTip, false, "", 5, "知道了.", "", "").ToJson());
  149. */
  150. }
  151. bytes?.Invoke(path);
  152. PlayerPrefs.SetString(Url, path);
  153. PlayerPrefs.SetString(Url + "_" + uuid, path);
  154. }
  155. }
  156. else
  157. {
  158. bytes?.Invoke(null);
  159. }
  160. }, presson);
  161. }
  162. public static bool DownLoadForTexture(DownLoadResources msg, Action<Texture2D> bytes, Action<float> presson)
  163. {
  164. return DownLoadForBytes(msg, (byte[] fileBytes) => {
  165. if (fileBytes != null)
  166. {
  167. Texture2D tex = new Texture2D(10,10);
  168. tex.LoadImage(fileBytes);
  169. bytes?.Invoke(tex);
  170. }
  171. else
  172. {
  173. bytes?.Invoke(null);
  174. }
  175. }, presson);
  176. }
  177. public static DownLoadResources getTestData(string url)
  178. {
  179. DownLoadResources dlr = new DownLoadResources();
  180. dlr.path = XunJianDataManager.Instance.chooseXunJian.url;
  181. return dlr;
  182. }
  183. public static void PutFile(string filePath, string fileName, string objectName, string uuid, int fileSize, Action<string> callBack)
  184. {
  185. getdata((CredentialConfig c) => {
  186. GameObject go = null;
  187. PutFileData putFileData = new PutFileData();
  188. putFileData.filePath = filePath;
  189. putFileData.fileName = fileName;
  190. putFileData.fileSize = fileSize;
  191. putFileData.callBack = callBack;
  192. putFileData.objectName = objectName;
  193. putFileData.uuid = uuid;
  194. switch (cospingzheng.drive)
  195. {
  196. case "minio":
  197. #if ISLOCAL
  198. go = GameObject.Instantiate(Resources.Load<GameObject>("DownLoadMinIOXRingItem"));
  199. #else
  200. go = GameObject.Instantiate(GameInit.ablist.LoadAsset<GameObject>("DownLoadMinIOXRingItem"));
  201. #endif
  202. DownLoadMinIOXRItem downLoadMinIOXRItem = go.GetComponent<DownLoadMinIOXRItem>();
  203. downLoadMinIOXRItem.startUpLoad(putFileData);
  204. break;
  205. case "COS":
  206. #if ISLOCAL
  207. go = GameObject.Instantiate(Resources.Load<GameObject>("DownLoadCOSXRingItem"));
  208. #else
  209. go = GameObject.Instantiate(GameInit.ablist.LoadAsset<GameObject>("DownLoadCOSXRingItem"));
  210. #endif
  211. DownLoadCOSXRItem downLoadCOSXRItem = go.GetComponent<DownLoadCOSXRItem>();
  212. downLoadCOSXRItem.startUpLoad(putFileData);
  213. break;
  214. }
  215. Debug.Log("DGJ PutFile " + filePath);
  216. });
  217. }
  218. public static string getTestData(DownLoadResources url,string drive)
  219. {
  220. JsonData data = new JsonData();
  221. data["drive"] = drive;
  222. data["url"] = url.path;
  223. data["uuid"] = url.uuid;
  224. return data.ToJson();
  225. }
  226. static void getdata(Action<CredentialConfig> callback)
  227. {
  228. if (cospingzheng == null && HttpEdustryAction.Token != "")
  229. {
  230. //获取上传凭证, 根据上传凭证获取的数据初始化 MInio
  231. WindowsManager.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpEdustryAction.storage_projectCredential, "", (string str) =>
  232. {
  233. JsonData data = JsonMapper.ToObject(str);
  234. cospingzheng = JsonConvert.DeserializeObject<CredentialConfig>(data["data"].ToJson());
  235. callback.Invoke(cospingzheng);
  236. TimerMgr.Instance.CreateTimer(() => {
  237. cospingzheng = null;
  238. }, 1800);
  239. }));
  240. }
  241. else
  242. {
  243. callback.Invoke(null);
  244. }
  245. }
  246. public class DownLoadUrlConfig
  247. {
  248. public string url;
  249. public Action<byte[]> bytes;
  250. public Action<float> presson;
  251. }
  252. public class DownLoadMinIoConfig
  253. {
  254. public string url;
  255. public Action<byte[]> bytes;
  256. public Action<float> presson;
  257. public DownLoadResources data;
  258. }
  259. public class PutFileData
  260. {
  261. public string filePath;
  262. public string fileName;
  263. public int fileSize;
  264. public string objectName;
  265. public string uuid;
  266. public Action<string> callBack;
  267. }
  268. public class DownLoadConfig
  269. {
  270. public Action<byte[]> bytes;
  271. public Action<float> presson;
  272. public DownLoadResources data;
  273. }
  274. public class Credential
  275. {
  276. public string type { get; set; }
  277. }
  278. public class CredentialConfig
  279. {
  280. public string drive { get; set; }
  281. public Credentials credentials { get; set; }
  282. public long startTime { get; set; }
  283. public long expiredTime { get; set; }
  284. public string bucket { get; set; }
  285. public string path { get; set; }
  286. public string region { get; set; }
  287. public string host { get; set; }
  288. }
  289. public class Credentials
  290. {
  291. public string tmpSecretId { get; set; }
  292. public string tmpSecretKey { get; set; }
  293. public string token { get; set; }
  294. }
  295. public class DownLoadResources
  296. {
  297. public int id { get; set; }
  298. public string path { get; set; }
  299. public string uuid { get; set; }
  300. public int loadingMethod { get; set; }
  301. // public string name { get; set; }
  302. public string info { get; set; }
  303. }
  304. }