From 170731501f4539f1f607e6ccff0f9ab788fdee90 Mon Sep 17 00:00:00 2001 From: Anton Franzluebbers Date: Sun, 11 Sep 2022 17:01:40 -0400 Subject: [PATCH] uploading/downloading files --- unity_package/Runtime/VELConnectManager.cs | 36 ++++++++++++++++++++-- unity_package/package.json | 4 +-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/unity_package/Runtime/VELConnectManager.cs b/unity_package/Runtime/VELConnectManager.cs index c20a991..3446c3d 100644 --- a/unity_package/Runtime/VELConnectManager.cs +++ b/unity_package/Runtime/VELConnectManager.cs @@ -150,7 +150,8 @@ namespace VELConnect SetDeviceField(new Dictionary { { "current_app", Application.productName }, - { "pairing_code", PairingCode } + { "pairing_code", PairingCode }, + { "friendly_name", SystemInfo.deviceName }, }); UpdateUserCount(); @@ -533,14 +534,43 @@ namespace VELConnect Task.Run(async () => { - HttpResponseMessage r = await new HttpClient().PostAsync(_instance.velConnectUrl + "/api/upload_file", requestContent); + HttpResponseMessage r = + await new HttpClient().PostAsync(_instance.velConnectUrl + "/api/upload_file", requestContent); string resp = await r.Content.ReadAsStringAsync(); - Dictionary dict = JsonConvert.DeserializeObject>(resp); + Dictionary dict = JsonConvert.DeserializeObject>(resp); successCallback?.Invoke(dict["key"]); }); } + public static void DownloadFile(string key, Action successCallback = null) + { + _instance.StartCoroutine(_instance.DownloadFileCo(key, successCallback)); + } + + private IEnumerator DownloadFileCo(string key, Action successCallback = null) + { + UnityWebRequest www = new UnityWebRequest(velConnectUrl + "/api/download_file/" + key); + www.downloadHandler = new DownloadHandlerBuffer(); + yield return www.SendWebRequest(); + + if (www.result != UnityWebRequest.Result.Success) + { + Debug.Log(www.error); + } + else + { + // Show results as text + Debug.Log(www.downloadHandler.text); + + // Or retrieve results as binary data + byte[] results = www.downloadHandler.data; + + successCallback?.Invoke(results); + } + } + + public static void GetRequestCallback(string url, Action successCallback = null, Action failureCallback = null) { diff --git a/unity_package/package.json b/unity_package/package.json index 2ac6467..a83c833 100644 --- a/unity_package/package.json +++ b/unity_package/package.json @@ -1,7 +1,7 @@ { "name": "edu.uga.engr.vel.vel-connect", "displayName": "VEL-Connect", - "version": "1.0.0", + "version": "1.0.1", "unity": "2019.1", "description": "Web-based configuration for VR applications", "keywords": [], @@ -14,4 +14,4 @@ "dependencies": { "com.unity.nuget.newtonsoft-json": "3.0.0" } -} \ No newline at end of file +}