MinIoXR.cs 10 KB

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