MinIoXR.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. 
  2. using Minio;
  3. using Minio.DataModel.Args;
  4. using Minio.Exceptions;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using UnityEngine;
  12. using UnityEngine.Networking;
  13. public class MinIoXR
  14. {
  15. IMinioClient minio;
  16. MinIOConfig minioconfig;
  17. int projectId;
  18. Thread thread;
  19. private void Start()
  20. {
  21. thread = new Thread(DoSomeWork);
  22. thread.Start();
  23. }
  24. int miao = 0;
  25. bool isminioUpdate;
  26. bool isUpdate;
  27. void DoSomeWork()
  28. {
  29. while (true)
  30. {
  31. if (minio == null)
  32. {
  33. }
  34. else
  35. {
  36. if (Nowfd != null && !isUpdate)
  37. {
  38. isUpdate = true;
  39. Run().Wait();
  40. }
  41. if ( gfdQueue.Count > 0 && !isDownLoad)
  42. {
  43. isDownLoad = true;
  44. nowgfd = gfdQueue.Dequeue();
  45. RunFile().Wait();
  46. }
  47. }
  48. Thread.Sleep(1000);
  49. miao++;
  50. if(miao>2400)
  51. {
  52. miao = 0;
  53. minio = null;
  54. }
  55. }
  56. }
  57. public class MinIOConfig
  58. {
  59. public string token;
  60. public string tmpSecretId;
  61. public string tmpSecretKey;
  62. public string host;
  63. public string bucket;
  64. public string path;
  65. }
  66. void initMinIo(MinIOConfig d,Action<bool> isSuccess)
  67. {
  68. minioconfig = d;
  69. try
  70. {
  71. bool isHttps = d.host.Contains("https");
  72. var endpoint = d.host.Split("//")[1];
  73. var accessKey = d.tmpSecretId;//"tr6Nh5D8bnlGaLJE6vb5";
  74. var secretKey = d.tmpSecretKey;// "aVOYdXLnX4MCiKbit8aomZNWvAx8YSpzhiwzFhrI";
  75. minio = new MinioClient()
  76. .WithEndpoint(endpoint)
  77. .WithCredentials(accessKey, secretKey)
  78. .WithSessionToken(d.token)
  79. .WithSSL(isHttps)
  80. .Build();
  81. isSuccess.Invoke(true);
  82. }
  83. catch
  84. {
  85. isSuccess.Invoke(false);
  86. }
  87. }
  88. Queue<FileData> fdQueue = new Queue<FileData>();
  89. void putFile(string filePath, string fileName, Action<string> callBack)
  90. {
  91. FileData fd = new FileData();
  92. fd.filePath = filePath;
  93. fd.fileName = fileName;
  94. fd.callBack = callBack;
  95. fdQueue.Enqueue(fd);
  96. }
  97. string DicName = "XunJian";
  98. public void saveFile(byte[] bytes, int projectId, string Index, int imgIndex, Action<string> callBack)
  99. {
  100. this.projectId = projectId;
  101. if (!Directory.Exists(Application.persistentDataPath + "/" + DicName))
  102. Directory.CreateDirectory(Application.persistentDataPath + "/" + DicName);
  103. string fileName = DicName + "_" + projectId + "_" + Index + "_" + imgIndex + ".png";
  104. string filePathname = Application.persistentDataPath + "/" + DicName + "/" + fileName;
  105. // if (File.Exists(filePathname))
  106. // File.Delete(filePathname);
  107. File.WriteAllBytes(filePathname, bytes);
  108. // PlayerPrefs.SetString(fileName, filePathname);
  109. putFile(filePathname, fileName, callBack);
  110. }
  111. /// <summary>
  112. /// 运行模式下Texture转换成Texture2D
  113. /// </summary>
  114. /// <param name="texture"></param>
  115. /// <returns></returns>
  116. private Texture2D TextureToTexture2D(Texture texture)
  117. {
  118. Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
  119. RenderTexture currentRT = RenderTexture.active;
  120. RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
  121. Graphics.Blit(texture, renderTexture);
  122. RenderTexture.active = renderTexture;
  123. texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  124. texture2D.Apply();
  125. RenderTexture.active = currentRT;
  126. RenderTexture.ReleaseTemporary(renderTexture);
  127. return texture2D;
  128. }
  129. /// <summary>
  130. /// 获取文件
  131. /// </summary>
  132. /// <param name="projectId">巡检ID</param>
  133. /// <param name="Index">步骤ID</param>
  134. /// <param name="imgIndex">步骤ID内图片ID</param>
  135. /// <param name="callBack"></param>
  136. public void getFile(int projectId, string Index, int imgIndex, Action<Texture2D> callBack)//, string path
  137. {
  138. this.projectId = projectId;
  139. string fileName = DicName + "_" + projectId + "_" + Index + "_" + imgIndex + ".png";
  140. string filePathname = PlayerPrefs.GetString(fileName);
  141. if (filePathname != "")
  142. {
  143. // StartCoroutine(_GetTexture(filePathname, callBack));
  144. }
  145. else
  146. {
  147. GetFileData gfd = new GetFileData();
  148. gfd.bucket = minioconfig.bucket;
  149. gfd.objectName = DicName + "_" + projectId + "_" + Index + "_" + imgIndex + ".png";
  150. gfd.callBack = callBack;
  151. gfdQueue.Enqueue(gfd);
  152. }
  153. }
  154. GetFileData nowgfd;
  155. Queue<byte[]> backQueue = new Queue<byte[]>();
  156. Queue<GetFileData> gfdQueue = new Queue<GetFileData>();
  157. // File uploader task.
  158. private async Task RunFile()
  159. {
  160. try
  161. {
  162. StatObjectArgs statObjectArgs = new StatObjectArgs()
  163. .WithBucket(minioconfig.bucket)
  164. .WithObject(minioconfig.path + "/" + nowgfd.objectName);
  165. await minio.StatObjectAsync(statObjectArgs);
  166. // Get input stream to have content of 'my-objectname' from 'my-bucketname'
  167. GetObjectArgs getObjectArgs = new GetObjectArgs()
  168. .WithBucket(minioconfig.bucket)
  169. .WithObject(minioconfig.path + "/" + nowgfd.objectName)
  170. // .WithHeaders(metaData)
  171. .WithCallbackStream(async (stream) =>
  172. {
  173. backQueue.Enqueue(StreamToBytes(stream));
  174. });
  175. await minio.GetObjectAsync(getObjectArgs);
  176. }
  177. catch
  178. {
  179. backQueue.Enqueue(null);
  180. }
  181. }
  182. static byte[] StreamToBytes(Stream stream)
  183. {
  184. byte[] bytes = new byte[stream.Length];
  185. stream.Read(bytes, 0, bytes.Length);
  186. stream.Seek(0, SeekOrigin.Begin);
  187. return bytes;
  188. }
  189. /// <summary>
  190. /// 请求图片
  191. /// </summary>
  192. /// <param name="url">图片地址,like 'http://www.my-server.com/image.png '</param>
  193. /// <param name="action">请求发起后处理回调结果的委托,处理请求结果的图片</param>
  194. /// <returns></returns>
  195. IEnumerator _GetTexture(string url, Action<Texture2D> actionResult)
  196. {
  197. #if UNITY_ANDROID
  198. //Debug.LogError("请求图片:"+url);
  199. WWW w = new WWW("file://" + url);
  200. yield return w;
  201. if (w.isDone)
  202. actionResult?.Invoke(w.texture);
  203. #else
  204. UnityWebRequest uwr = new UnityWebRequest(url);
  205. DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
  206. uwr.downloadHandler = downloadTexture;
  207. yield return uwr.SendWebRequest();
  208. Texture2D t = null;
  209. if (!(uwr.isNetworkError || uwr.isHttpError))
  210. {
  211. t = downloadTexture.texture;
  212. }
  213. else
  214. {
  215. Debug.Log("下载失败,请检查网络,或者下载地址是否正确: " + uwr.error);
  216. }
  217. if (actionResult != null)
  218. {
  219. actionResult(t);
  220. }
  221. #endif
  222. }
  223. // File uploader task.
  224. private async Task Run()
  225. {
  226. try
  227. {
  228. var getListBucketsTask = await minio.ListBucketsAsync().ConfigureAwait(false);
  229. var bucketName = minioconfig.bucket;
  230. var objectName = minioconfig.path + "/" + Nowfd.fileName;
  231. var filePath = Nowfd.filePath;
  232. var contentType = "application/octet-stream";
  233. // Make a bucket on the server, if not already present.
  234. // Upload a file to bucket.
  235. var putObjectArgs = new PutObjectArgs()
  236. .WithBucket(bucketName)
  237. .WithObject(objectName)
  238. .WithFileName(filePath)
  239. .WithContentType(contentType);
  240. await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
  241. Debug.Log("33333333333333333");
  242. PlayerPrefs.SetString(Nowfd.fileName, Nowfd.filePath);
  243. isUpdate = false;
  244. Nowfd.callBack?.Invoke(Nowfd.fileName);
  245. Nowfd = null;
  246. }
  247. catch
  248. {
  249. Nowfd.callBack?.Invoke(null);
  250. isUpdate = false;
  251. Nowfd = null;
  252. }
  253. }
  254. bool isDownLoad;
  255. FileData Nowfd;
  256. private void Update()
  257. {
  258. if (fdQueue.Count > 0 && Nowfd == null)
  259. {
  260. Nowfd = fdQueue.Dequeue();
  261. }
  262. if (minio == null)
  263. {
  264. if (projectId != 0 && !isminioUpdate)
  265. {
  266. isminioUpdate = true;
  267. initMinIo(minioconfig,(bool b) =>
  268. {
  269. isminioUpdate = false;
  270. });
  271. }
  272. }
  273. if(backQueue.Count>0)
  274. {
  275. byte[] bytes = backQueue.Dequeue();
  276. if(bytes!=null)
  277. {
  278. Debug.Log("444444444444");
  279. Texture2D t2d = new Texture2D(10, 10);
  280. t2d.LoadImage(bytes);
  281. t2d.Apply();
  282. nowgfd.callBack?.Invoke(t2d);
  283. }else
  284. {
  285. Debug.Log("55555555555555");
  286. nowgfd.callBack?.Invoke(null);
  287. }
  288. isDownLoad = false;
  289. nowgfd = null;
  290. }
  291. }
  292. public class FileData
  293. {
  294. public string filePath;
  295. public string fileName;
  296. public Action<string> callBack;
  297. }
  298. public class GetFileData
  299. {
  300. public string bucket;
  301. public string objectName;
  302. public Action<Texture2D> callBack;
  303. }
  304. }