123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- using LitJson;
- using Minio;
- using Minio.DataModel.Args;
- using Minio.Exceptions;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading;
- using System.Threading.Tasks;
- using UnityEngine;
- using UnityEngine.Networking;
- using XRTool.Util;
- public class TestMinIO : MonoSingleton<TestMinIO>
- {
- private IMinioClient minio;
- private string path;
- private string bucket;
- private string host;
- private string tmpSecretId;
- string tmpSecretKey;
- private string minioToken;
- private int projectId;
- private bool isminioUpdate;
- private bool isUpdate;
- private Thread thread;
- private void Start()
- {
- thread = new Thread(DoSomeWork);
- thread.Start();
- }
- void DoSomeWork()
- {
- while (true)
- {
- if (minio == null)
- {
- }
- else
- {
- if (Nowfd != null && !isUpdate)
- {
- isUpdate = true;
- Run().Wait();
- }
- if (gfdQueue.Count > 0 && !isDownLoad)
- {
- isDownLoad = true;
- nowgfd = gfdQueue.Dequeue();
- RunFile().Wait();
- }
- }
- Thread.Sleep(1000);
- }
- }
- void initMinIo(Action<bool> callBack)
- {
- JsonData data = new JsonData();
- data["inspectionId"] = projectId;
- GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.storage_inspectionCredential, data.ToJson(), async (string str) =>
- {
- try
- {
- JsonData d = JsonMapper.ToObject(str);
- minioToken = d["data"]["credentials"]["token"].ToString();
- tmpSecretId = d["data"]["credentials"]["tmpSecretId"].ToString();
- tmpSecretKey = d["data"]["credentials"]["tmpSecretKey"].ToString();
- host = d["data"]["host"].ToString();
- bucket = d["data"]["bucket"].ToString();
- path = d["data"]["path"].ToString();
- Debug.Log(str);
- bool isHttps = host.Contains("https");
- var endpoint = host.Split("//")[1];
- var accessKey = tmpSecretId;//"tr6Nh5D8bnlGaLJE6vb5";
- var secretKey = tmpSecretKey;// "aVOYdXLnX4MCiKbit8aomZNWvAx8YSpzhiwzFhrI";
- Debug.Log("endpoint===>" + endpoint + " isHttps==>" + isHttps);
- minio = new MinioClient()
- .WithEndpoint(endpoint)
- .WithCredentials(accessKey, secretKey)
- .WithSessionToken(minioToken)
- .WithSSL(isHttps)
- .Build();
- TimerMgr.Instance.CreateTimer(() =>
- {
- minio = null;
- }, 1200);
- callBack?.Invoke(true);
- }
- catch
- {
- callBack?.Invoke(false);
- }
- }));
- }
- Queue<FileData> fdQueue = new Queue<FileData>();
- void putFile(string filePath, string fileName, Action<string> callBack)
- {
- FileData fd = new FileData();
- fd.filePath = filePath;
- fd.fileName = fileName;
- fd.callBack = callBack;
- fdQueue.Enqueue(fd);
- }
- string DicName = "XunJian";
- public void saveFile(byte[] bytes, int projectId, string Index, int imgIndex, Action<string> callBack)
- {
- this.projectId = projectId;
- if (!Directory.Exists(Application.persistentDataPath + "/" + DicName))
- Directory.CreateDirectory(Application.persistentDataPath + "/" + DicName);
- string fileName = DicName + "_" + projectId + "_" + Index + "_" + imgIndex + ".png";
- string filePathname = Application.persistentDataPath + "/" + DicName + "/" + fileName;
- // if (File.Exists(filePathname))
- // File.Delete(filePathname);
- File.WriteAllBytes(filePathname, bytes);
- // PlayerPrefs.SetString(fileName, filePathname);
- putFile(filePathname, fileName, callBack);
- }
- /// <summary>
- /// 运行模式下Texture转换成Texture2D
- /// </summary>
- /// <param name="texture"></param>
- /// <returns></returns>
- private Texture2D TextureToTexture2D(Texture texture)
- {
- Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
- RenderTexture currentRT = RenderTexture.active;
- RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
- Graphics.Blit(texture, renderTexture);
- RenderTexture.active = renderTexture;
- texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
- texture2D.Apply();
- RenderTexture.active = currentRT;
- RenderTexture.ReleaseTemporary(renderTexture);
- return texture2D;
- }
- /// <summary>
- /// 获取文件
- /// </summary>
- /// <param name="projectId">巡检ID</param>
- /// <param name="Index">步骤ID</param>
- /// <param name="imgIndex">步骤ID内图片ID</param>
- /// <param name="callBack"></param>
- public void getFile(int projectId, string Index, int imgIndex, Action<Texture2D> callBack)//, string path
- {
- this.projectId = projectId;
- string fileName = DicName + "_" + projectId + "_" + Index + "_" + imgIndex + ".png";
- string filePathname = PlayerPrefs.GetString(fileName);
- if (filePathname != "")
- {
- StartCoroutine(_GetTexture(filePathname, callBack));
- }
- else
- {
- GetFileData gfd = new GetFileData();
- gfd.bucket = bucket;
- gfd.objectName = DicName + "_" + projectId + "_" + Index + "_" + imgIndex + ".png";
- gfd.callBack = callBack;
- gfdQueue.Enqueue(gfd);
- }
- }
- GetFileData nowgfd;
- Queue<byte[]> backQueue = new Queue<byte[]>();
- Queue<GetFileData> gfdQueue = new Queue<GetFileData>();
- // File uploader task.
- private async Task RunFile()
- {
- Debug.LogError(bucket);
- Debug.LogError(path + "/" + nowgfd.objectName);
- try
- {
- StatObjectArgs statObjectArgs = new StatObjectArgs()
- .WithBucket(bucket)
- .WithObject(path + "/" + nowgfd.objectName);
- await minio.StatObjectAsync(statObjectArgs);
- // Get input stream to have content of 'my-objectname' from 'my-bucketname'
- GetObjectArgs getObjectArgs = new GetObjectArgs()
- .WithBucket(bucket)
- .WithObject(path + "/" + nowgfd.objectName)
- .WithCallbackStream(async (stream) =>
- {
- backQueue.Enqueue(StreamToBytes(stream));
- });
- await minio.GetObjectAsync(getObjectArgs);
- }
- catch
- {
- backQueue.Enqueue(null);
- }
- }
- static byte[] StreamToBytes(Stream stream)
- {
- byte[] bytes = new byte[stream.Length];
- stream.Read(bytes, 0, bytes.Length);
- stream.Seek(0, SeekOrigin.Begin);
- return bytes;
- }
- /// <summary>
- /// 请求图片
- /// </summary>
- /// <param name="url">图片地址,like 'http://www.my-server.com/image.png '</param>
- /// <param name="action">请求发起后处理回调结果的委托,处理请求结果的图片</param>
- /// <returns></returns>
- IEnumerator _GetTexture(string url, Action<Texture2D> actionResult)
- {
- #if UNITY_ANDROID
- using (UnityWebRequest request = new UnityWebRequest(url))
- {
- DownloadHandlerTexture downloadHandlerTexture = new DownloadHandlerTexture(true);
- request.downloadHandler = downloadHandlerTexture;
- yield return request.SendWebRequest();
- if (string.IsNullOrEmpty(request.error))
- {
- Texture2D localTexture = downloadHandlerTexture.texture;
- actionResult?.Invoke(localTexture);
- }
- else
- {
- actionResult?.Invoke(null);
- Debug.Log("_GetTexture:" + request.error);
- }
- }
- /*
- WWW www = new WWW(url);
- yield return www;
- Texture2D texture2D = new Texture2D(450, 300);
- if (www.isDone)
- {
- actionResult?.Invoke(texture2D);
- //texture2D.LoadImage(www.bytes);
- }
- */
- #else
- UnityWebRequest uwr = new UnityWebRequest(url);
- DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
- uwr.downloadHandler = downloadTexture;
- yield return uwr.SendWebRequest();
- Texture2D t = null;
- if (!(uwr.isNetworkError || uwr.isHttpError))
- {
- t = downloadTexture.texture;
- }
- else
- {
- Debug.Log("下载失败,请检查网络,或者下载地址是否正确: " + uwr.error);
- }
- if (actionResult != null)
- {
- actionResult(t);
- }
- #endif
- }
- // File uploader task.
- private async Task Run()
- {
- try
- {
- var getListBucketsTask = await minio.ListBucketsAsync().ConfigureAwait(false);
- var bucketName = bucket;
- var objectName = path + "/" + Nowfd.fileName;
- var filePath = Nowfd.filePath;
- var contentType = "application/octet-stream";
- // Make a bucket on the server, if not already present.
- // Upload a file to bucket.
- var putObjectArgs = new PutObjectArgs()
- .WithBucket(bucketName)
- .WithObject(objectName)
- .WithFileName(filePath)
- .WithContentType(contentType);
- await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
- Debug.Log("33333333333333333");
- PlayerPrefs.SetString(Nowfd.fileName, Nowfd.filePath);
- isUpdate = false;
- Nowfd.callBack?.Invoke(Nowfd.fileName);
- Nowfd = null;
- }
- catch
- {
- Nowfd.callBack?.Invoke(null);
- isUpdate = false;
- Nowfd = null;
- }
- }
- bool isDownLoad;
- FileData Nowfd;
- private void Update()
- {
- if (fdQueue.Count > 0 && Nowfd == null)
- {
- Nowfd = fdQueue.Dequeue();
- }
- if (minio == null)
- {
- if (projectId != 0 && !isminioUpdate)
- {
- isminioUpdate = true;
- initMinIo((bool b) =>
- {
- isminioUpdate = false;
- });
- }
- }
- if (backQueue.Count > 0)
- {
- byte[] bytes = backQueue.Dequeue();
- if (bytes != null)
- {
- Debug.Log("444444444444");
- Texture2D t2d = new Texture2D(10, 10);
- t2d.LoadImage(bytes);
- t2d.Apply();
- nowgfd.callBack?.Invoke(t2d);
- }
- else
- {
- Debug.Log("55555555555555");
- nowgfd.callBack?.Invoke(null);
- }
- isDownLoad = false;
- nowgfd = null;
- }
- }
- public class FileData
- {
- public string filePath;
- public string fileName;
- public Action<string> callBack;
- }
- public class GetFileData
- {
- public string bucket;
- public string objectName;
- public Action<Texture2D> callBack;
- }
- }
|