123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using LitJson;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- public class DownLoadXRManager
- {
- public static Dictionary<string, byte[]> downLoadCache = new Dictionary<string, byte[]>();
- public static bool DownLoadForBytes(string msg, Action<byte[]> bytes, Action<float> presson)
- {
-
- JsonData data = JsonMapper.ToObject(msg);
- if(data["drive"].ToString()== "minio")
- {
- //string bucket = data["data"]["bucket"].ToString();
- //string objectName = data["data"]["bucket"].ToString();
- string Url = data["url"].ToString();
- Debug.Log("DGJ ===> DownLoadForBytes " + msg);
- RoomFileMinio.Instance.getFile(RoomFileMinio.Instance.bucket, Url, (RoomFileMinio.RoomFileData roomFile) =>
- {
- bytes?.Invoke(roomFile.bytes);
- });
- }
- else if (data["drive"].ToString() == "COS")
- {
- return false;
- }
- else
- {
- DownLoadUrlConfig config = new DownLoadUrlConfig();
- config.url = data["url"].ToString();
- config.bytes = bytes;
- config.presson = presson;
- GameObject go = GameObject.Instantiate(Resources.Load<GameObject>("DownLoadURLXRingItem"));
- DownLoadURLXRItem dlitem = go.GetComponent<DownLoadURLXRItem>();
- dlitem.startDownload(config);
- }
- return true;
- }
- public static bool DownLoadForFilePath(string msg, Action<string> bytes, Action<float> presson)
- {
- string Url = "";
- JsonData data = JsonMapper.ToObject(msg);
- if (data["drive"].ToString() == "minio")
- {
- Url = data["url"].ToString();
- }
- else if (data["drive"].ToString() == "COS")
- {
- return false;
- }
- else
- {
- Url = data["url"].ToString();
- }
- if (PlayerPrefs.HasKey(Url))
- {
- bytes?.Invoke(PlayerPrefs.GetString("DownLoadXR_" + Url));
- return true;
- }
-
- return DownLoadForBytes(msg, (byte[] fileBytes) => {
- if(fileBytes!=null)
- {
- if (PlayerPrefs.HasKey(Url))
- {
- bytes?.Invoke(PlayerPrefs.GetString("DownLoadXR_" + Url));
- }else
- {
- //文件流信息
- //StreamWriter sw;
- Stream sw;
- string path = Application.persistentDataPath + "/DownLoadXR/" + Url.Split("/")[Url.Split("/").Length - 1];
- 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(fileBytes, 0, fileBytes.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());
- */
- }
- bytes?.Invoke(path);
- PlayerPrefs.SetString("DownLoadXR_" + Url, path);
- }
- }
- else
- {
- bytes?.Invoke(null);
- }
-
-
- }, presson);
- }
- public static bool DownLoadForTexture(string msg, Action<Texture2D> bytes, Action<float> presson)
- {
- string Url = "";
- JsonData data = JsonMapper.ToObject(msg);
- if (data["drive"].ToString() == "minio")
- {
- }
- else if (data["drive"].ToString() == "COS")
- {
- return false;
- }
- else
- {
- Url = data["url"].ToString();
- }
- 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);
- }
- public static string getTestData(string url)
- {
- JsonData data = new JsonData();
- data["drive"] = "URL";
- data["url"] = url;
- return data.ToJson();
- }
- public static string getTestData(string url,string drive)
- {
- JsonData data = new JsonData();
- data["drive"] = drive;
- data["url"] = url;
- return data.ToJson();
- }
- public class DownLoadUrlConfig
- {
- public string url;
- public Action<byte[]> bytes;
- public Action<float> presson;
- }
- public class DownLoadMinIoConfig
- {
- public string url;
- public Action<byte[]> bytes;
- public Action<float> presson;
- }
- public class DownLoadCosConfig
- {
- public string url;
- public Action<byte[]> bytes;
- public Action<float> presson;
- }
- }
|