DownLoadXRManager.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. public class DownLoadXRManager
  8. {
  9. public static Dictionary<string, byte[]> downLoadCache = new Dictionary<string, byte[]>();
  10. public static bool DownLoadForBytes(string msg, Action<byte[]> bytes, Action<float> presson)
  11. {
  12. JsonData data = JsonMapper.ToObject(msg);
  13. if(data["drive"].ToString()== "minio")
  14. {
  15. //string bucket = data["data"]["bucket"].ToString();
  16. //string objectName = data["data"]["bucket"].ToString();
  17. string Url = data["url"].ToString();
  18. Debug.Log("DGJ ===> DownLoadForBytes " + msg);
  19. RoomFileMinio.Instance.getFile(RoomFileMinio.Instance.bucket, Url, (RoomFileMinio.RoomFileData roomFile) =>
  20. {
  21. bytes?.Invoke(roomFile.bytes);
  22. });
  23. }
  24. else if (data["drive"].ToString() == "COS")
  25. {
  26. return false;
  27. }
  28. else
  29. {
  30. DownLoadUrlConfig config = new DownLoadUrlConfig();
  31. config.url = data["url"].ToString();
  32. config.bytes = bytes;
  33. config.presson = presson;
  34. GameObject go = GameObject.Instantiate(Resources.Load<GameObject>("DownLoadURLXRingItem"));
  35. DownLoadURLXRItem dlitem = go.GetComponent<DownLoadURLXRItem>();
  36. dlitem.startDownload(config);
  37. }
  38. return true;
  39. }
  40. public static bool DownLoadForFilePath(string msg, Action<string> bytes, Action<float> presson)
  41. {
  42. string Url = "";
  43. JsonData data = JsonMapper.ToObject(msg);
  44. if (data["drive"].ToString() == "minio")
  45. {
  46. Url = data["url"].ToString();
  47. }
  48. else if (data["drive"].ToString() == "COS")
  49. {
  50. return false;
  51. }
  52. else
  53. {
  54. Url = data["url"].ToString();
  55. }
  56. if (PlayerPrefs.HasKey(Url))
  57. {
  58. bytes?.Invoke(PlayerPrefs.GetString("DownLoadXR_" + Url));
  59. return true;
  60. }
  61. return DownLoadForBytes(msg, (byte[] fileBytes) => {
  62. if(fileBytes!=null)
  63. {
  64. if (PlayerPrefs.HasKey(Url))
  65. {
  66. bytes?.Invoke(PlayerPrefs.GetString("DownLoadXR_" + Url));
  67. }else
  68. {
  69. //文件流信息
  70. //StreamWriter sw;
  71. Stream sw;
  72. string path = Application.persistentDataPath + "/DownLoadXR/" + Url.Split("/")[Url.Split("/").Length - 1];
  73. Debug.Log("准备存文件===》"+path);
  74. FileInfo file = new FileInfo(path);
  75. try
  76. {
  77. if (file.Exists)
  78. {
  79. file.Delete();
  80. }
  81. if (!Directory.Exists(Application.persistentDataPath + "/DownLoadXR"))
  82. {
  83. Directory.CreateDirectory(Application.persistentDataPath + "/DownLoadXR");
  84. }
  85. //如果此文件存在则打开
  86. //sw = file .Append();
  87. //如果此文件不存在则创建
  88. sw = file.Create();
  89. //以行的形式写入信息
  90. //sw.WriteLine(info);
  91. sw.Write(fileBytes, 0, fileBytes.Length);
  92. sw.Close();
  93. sw.Dispose();
  94. }
  95. catch
  96. {
  97. /*
  98. JsonData data = new JsonData();
  99. data["type"] = "fileError";
  100. List<string> backTip = new List<string>();
  101. backTip.Add(data.ToJson());
  102. backTip.Add(data.ToJson());
  103. backTip.Add(data.ToJson());
  104. WindowsManager.Instance.show(WindowConfig.windowType.Error, false, WindowsManager.Instance.getErrorData("文件提示", "文件存储失败"+ path, Color.white, "icon", backTip, false, "", 5, "知道了.", "", "").ToJson());
  105. */
  106. }
  107. bytes?.Invoke(path);
  108. PlayerPrefs.SetString("DownLoadXR_" + Url, path);
  109. }
  110. }
  111. else
  112. {
  113. bytes?.Invoke(null);
  114. }
  115. }, presson);
  116. }
  117. public static bool DownLoadForTexture(string msg, Action<Texture2D> bytes, Action<float> presson)
  118. {
  119. string Url = "";
  120. JsonData data = JsonMapper.ToObject(msg);
  121. if (data["drive"].ToString() == "minio")
  122. {
  123. }
  124. else if (data["drive"].ToString() == "COS")
  125. {
  126. return false;
  127. }
  128. else
  129. {
  130. Url = data["url"].ToString();
  131. }
  132. return DownLoadForBytes(msg, (byte[] fileBytes) => {
  133. if (fileBytes != null)
  134. {
  135. Texture2D tex = new Texture2D(10,10);
  136. tex.LoadImage(fileBytes);
  137. bytes?.Invoke(tex);
  138. }
  139. else
  140. {
  141. bytes?.Invoke(null);
  142. }
  143. }, presson);
  144. }
  145. public static string getTestData(string url)
  146. {
  147. JsonData data = new JsonData();
  148. data["drive"] = "URL";
  149. data["url"] = url;
  150. return data.ToJson();
  151. }
  152. public static string getTestData(string url,string drive)
  153. {
  154. JsonData data = new JsonData();
  155. data["drive"] = drive;
  156. data["url"] = url;
  157. return data.ToJson();
  158. }
  159. public class DownLoadUrlConfig
  160. {
  161. public string url;
  162. public Action<byte[]> bytes;
  163. public Action<float> presson;
  164. }
  165. public class DownLoadMinIoConfig
  166. {
  167. public string url;
  168. public Action<byte[]> bytes;
  169. public Action<float> presson;
  170. }
  171. public class DownLoadCosConfig
  172. {
  173. public string url;
  174. public Action<byte[]> bytes;
  175. public Action<float> presson;
  176. }
  177. }