Ver Fonte

提交Room中的下载接口

胡佳骏 há 1 ano atrás
pai
commit
d1fac2f45f

+ 180 - 0
Assets/Game/Blue/Controller/LangChaoRommMinIo.cs

@@ -0,0 +1,180 @@
+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 LangChaoRommMinIo : MonoSingleton<LangChaoRommMinIo>
+{
+    IMinioClient minio;
+    string path;
+    string bucket;
+    string host;
+
+    string tmpSecretId;
+    string tmpSecretKey;
+    string minioToken;
+
+    string projectId;
+    Thread thread;
+    private void Start()
+    {
+        thread = new Thread(DoSomeWork);
+        thread.Start();
+    }
+
+    bool isminioUpdate;
+    bool isUpdate;
+    void DoSomeWork()
+    {
+        while (true)
+        {
+            if (minio == null)
+            {
+            }
+            else
+            {
+                if (Nowfd != null && !isUpdate)
+                {
+                    isUpdate = true;
+                    Run().Wait();
+                }
+            }
+            Thread.Sleep(1000);
+        }
+    }
+    public static byte[] StreamToBytes(Stream stream)
+    {
+        byte[] bytes = new byte[stream.Length];
+        stream.Read(bytes, 0, bytes.Length);
+        stream.Seek(0, SeekOrigin.Begin);
+        return bytes;
+    }
+    // File uploader task.
+    private async Task Run()
+    {
+            StatObjectArgs statObjectArgs = new StatObjectArgs()
+                                              .WithBucket(Nowfd.bucket)
+                                              .WithObject(Nowfd.objectName);
+            await minio.StatObjectAsync(statObjectArgs);
+
+            // Get input stream to have content of 'my-objectname' from 'my-bucketname'
+            GetObjectArgs getObjectArgs = new GetObjectArgs()
+                                              .WithBucket(Nowfd.bucket)
+                                              .WithObject(Nowfd.objectName)
+                                              .WithCallbackStream(async (stream) =>
+                                              {
+                                                  PresignedGetObjectArgs args = new PresignedGetObjectArgs()
+                                                                                    .WithBucket(Nowfd.bucket)
+                                                                                    .WithObject(Nowfd.objectName)
+                                                                                    .WithExpiry(60 * 60 * 24);
+                                                  string url = await minio.PresignedGetObjectAsync(args);
+
+                                                  RoomFileData rfd = new RoomFileData();
+                                                  rfd.url = url;
+                                                  rfd.bytes = StreamToBytes(stream);
+                                                  Nowfd.callBack(rfd);
+                                              });
+            await minio.GetObjectAsync(getObjectArgs);
+    }
+    void initMinIo(Action<bool> callBack)
+    {
+        JsonData data = new JsonData();
+        data["roomId"] = projectId;
+        GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.storage_roomCredential, 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<GetFileData> fdQueue = new Queue<GetFileData>();
+    public void getFile(string roomId, string bucket, string objectName, Action<RoomFileData> callBack)//, string path
+    {
+        if (this.projectId!= roomId)
+        {
+            this.projectId = roomId;
+            minio = null;
+        }
+        GetFileData getFileData = new GetFileData();
+        getFileData.bucket = bucket;
+        getFileData.objectName = objectName;
+        getFileData.callBack = callBack;
+        fdQueue.Enqueue(getFileData);
+    }
+
+   public class RoomFileData
+    {
+        public byte[] bytes;
+        public string url;
+    }
+
+    GetFileData Nowfd;
+    private void Update()
+    {
+        if (fdQueue.Count > 0 && Nowfd == null)
+        {
+            Nowfd = fdQueue.Dequeue();
+        }
+        if (minio == null)
+        {
+            if (projectId !=null && !isminioUpdate)
+            {
+                isminioUpdate = true;
+                initMinIo((bool b) =>
+                {
+                    isminioUpdate = false;
+                });
+
+            }
+        }
+    }
+
+    public class GetFileData
+    {
+        public string bucket;
+        public string objectName;
+        public Action<RoomFileData> callBack;
+    }
+}

+ 11 - 0
Assets/Game/Blue/Controller/LangChaoRommMinIo.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 9b586d4b3fcf5d54a945e18d92a99f07
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 2 - 1
Assets/HttpActionLang.cs

@@ -22,7 +22,8 @@ public class HttpActionLang : Singleton<HttpActionLang>
     public const string inspection_step = "/cmcc-endustry/v1/inspection/step"; // 机房巡检步骤上报
 
     public const string storage_inspectionCredential = "/cmcc-endustry/v1/storage/inspectionCredential"; // 
-
+    public const string storage_roomCredential = "/cmcc-endustry/v1/storage/roomCredential"; // 
+    
     /// <summary>
     /// 获取刀片机详细信息
     /// </summary>