DGJ 1 vuosi sitten
vanhempi
sitoutus
888d4384d2

+ 84 - 4
Assets/DownLoadXR/DownLoadXRManager.cs

@@ -13,7 +13,60 @@ public class DownLoadXRManager
         JsonData data = JsonMapper.ToObject(msg);
         if(data["drive"].ToString()== "minio")
         {
-            return false;
+            string bucket = data["data"]["bucket"].ToString();
+            string objectName = data["data"]["bucket"].ToString();
+            string Url = data["url"].ToString();
+            RoomFileMinio.Instance.getFile(bucket, objectName, (RoomFileMinio.RoomFileData roomFile) =>
+            {
+                Debug.Log("RoomFile URL "+ roomFile.url);
+                Debug.Log("RoomFile Bytes Length " + roomFile.bytes.Length);
+
+
+                //文件流信息  
+                //StreamWriter sw;  
+                Stream sw;
+                string path = Application.persistentDataPath + "/DownLoadXR/" +Path.GetFileName(Url);
+                Debug.Log("准备存文件===》" + path);
+                FileInfo file = new FileInfo(path);
+                try
+                {
+
+                    if (file.Exists)
+                    {
+                        file.Delete();
+                    }
+                    if (!Directory.Exists(Application.persistentDataPath + "/DownLoadXR"))
+                    {
+                        Directory.CreateDirectory(Application.persistentDataPath + "/DownLoadXR");
+                    }
+                    //如果此文件存在则打开  
+                    //sw = file .Append();  
+                    //如果此文件不存在则创建  
+                    sw = file.Create();
+
+                    //以行的形式写入信息  
+                    //sw.WriteLine(info);  
+                    sw.Write(roomFile.bytes, 0, roomFile.bytes.Length);
+                    sw.Close();
+                    sw.Dispose();
+                }
+                catch
+                {
+                    /*
+                    JsonData data = new JsonData();
+                    data["type"] = "fileError";
+                    List<string> backTip = new List<string>();
+                    backTip.Add(data.ToJson());
+                    backTip.Add(data.ToJson());
+                    backTip.Add(data.ToJson());
+                    WindowsManager.Instance.show(WindowConfig.windowType.Error, false, WindowsManager.Instance.getErrorData("文件提示", "文件存储失败"+ path, Color.white, "icon", backTip, false, "", 5, "知道了.", "", "").ToJson());
+                    */
+                }
+                PlayerPrefs.SetString("DownLoadXR_" + Url, path);
+                bytes?.Invoke(roomFile.bytes);             
+            });
+
+            return true;
         }
         else if (data["drive"].ToString() == "COS")
         {
@@ -39,8 +92,22 @@ public class DownLoadXRManager
         JsonData data = JsonMapper.ToObject(msg);
         if (data["drive"].ToString() == "minio")
         {
-            // 下载完后将byte[] 根据URL 用PlayerPrefs 存入本地
-            return false;
+            Url = data["url"].ToString();
+            if (PlayerPrefs.HasKey(Url))
+            {
+                bytes?.Invoke(PlayerPrefs.GetString("DownLoadXR_" + Url));
+                return true;
+            }
+            else
+            {
+               return DownLoadForBytes(msg, (byte[] callBackBytes) =>
+                {
+                    bytes?.Invoke(PlayerPrefs.GetString("DownLoadXR_" + Url));
+                }, presson);
+            }
+
+
+           
         }
         else if (data["drive"].ToString() == "COS")
         { 
@@ -126,7 +193,20 @@ public class DownLoadXRManager
         JsonData data = JsonMapper.ToObject(msg);
         if (data["drive"].ToString() == "minio")
         {
-            return false;
+            return DownLoadForBytes(msg, (byte[] fileBytes) =>
+            {
+                if (fileBytes != null)
+                {
+                    Texture2D tex = new Texture2D(10, 10);
+                    tex.LoadImage(fileBytes);
+                    bytes?.Invoke(tex);
+                }
+                else
+                {
+                    bytes?.Invoke(null);
+                }
+            },presson);
+         //   return false;
         }
         else if (data["drive"].ToString() == "COS")
         {

+ 1 - 1
Assets/Scripts/HttpEdustryAction.cs

@@ -9,5 +9,5 @@ public class HttpEdustryAction
 
     public static string Token = "";
 
-
+    public static string storage_roomCredential = "";
 }

+ 238 - 0
Assets/Scripts/RoomFileMinio.cs

@@ -0,0 +1,238 @@
+using LitJson;
+using Minio;
+using Minio.DataModel.Args;
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.IO;
+using System.Threading;
+using System.Threading.Tasks;
+using UnityEngine;
+using XRTool.Util;
+
+public class RoomFileMinio : MonoSingleton<RoomFileMinio>
+{
+    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;
+    }
+    Queue<RoomFileDataBack> backQueue = new Queue<RoomFileDataBack>();
+    // File uploader task.
+    private async Task Run()
+    {
+        /*
+        StatObjectArgs statObjectArgs = new StatObjectArgs()
+                                          .WithBucket(Nowfd.bucket)
+                                          .WithObject(Nowfd.objectName);
+        await minio.StatObjectAsync(statObjectArgs);
+        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;
+        RoomFileDataBack rfdb = new RoomFileDataBack();
+        rfdb.rfd = rfd;
+        rfdb.Nowfd = Nowfd;
+        backQueue.Enqueue(rfdb);*/
+        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);
+
+
+                                              RoomFileDataBack rfdb = new RoomFileDataBack();
+                                              rfdb.rfd = rfd;
+                                              rfdb.Nowfd = Nowfd;
+                                              backQueue.Enqueue(rfdb);
+
+                                            //  rfd.url = Application.persistentDataPath + "/" + Path.GetFileName(url);
+
+
+                                          });
+
+        await minio.GetObjectAsync(getObjectArgs);
+    }
+    void initMinIo(Action<bool> callBack)
+    {
+        JsonData data = new JsonData();
+        data["roomId"] = projectId;
+        //获取上传凭证, 根据上传凭证获取的数据初始化 MInio
+        StartCoroutine(HttpTool.Instance.SendHttp(HttpEdustryAction.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();
+
+                var minioTest = 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 bucket, string objectName, Action<RoomFileData> callBack, string roomId=null)//, 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;
+    }
+    public class RoomFileDataBack
+    {
+        public RoomFileData rfd;
+        public GetFileData Nowfd;
+    }
+
+    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;
+                });
+
+            }
+        }
+        if (backQueue.Count > 0)
+        {
+            RoomFileDataBack rfdb = backQueue.Dequeue();
+            try
+            {
+                rfdb.Nowfd.callBack?.Invoke(rfdb.rfd);
+
+            }
+            catch
+            {
+
+            }
+            isUpdate = false;
+            Nowfd = null;
+        }
+
+    }
+
+    public class GetFileData
+    {
+        public string bucket;
+        public string objectName;
+        public Action<RoomFileData> callBack;
+    }
+}

+ 11 - 0
Assets/Scripts/RoomFileMinio.cs.meta

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