DownLoadXRManager.cs 11 KB

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