From 81744f2ba5e655ac8e18850626920c41d26501a5 Mon Sep 17 00:00:00 2001 From: SimonDarksideJ Date: Sat, 6 Jan 2024 17:05:53 +0000 Subject: [PATCH 1/7] Add DownloadFileBytes method --- .../com.utilities.rest/Runtime/Rest.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index 5a73422..a61d072 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -983,6 +983,35 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName) return filePath; } + public static async Task DownloadFileBytesAsync( + string url, + string fileName = null, + RestParameters parameters = null, + bool debug = false, + CancellationToken cancellationToken = default) + { + await Awaiters.UnityMainThread; + byte[] bytes = null; + + var filePath = await DownloadFileAsync(url, fileName, parameters, false, cancellationToken); + var absolutefilePath = filePath.Replace("file://", ""); + if (File.Exists(absolutefilePath)) + { + try + { + bytes = File.ReadAllBytes(absolutefilePath); + } + catch (Exception ex) + { + Debug.LogError(ex); + throw; + } + } + + return bytes; + } + + #endregion Get Multimedia Content /// From 452e4b8680397898e56ccafcc989026673df9aa7 Mon Sep 17 00:00:00 2001 From: SimonDarksideJ Date: Sat, 6 Jan 2024 17:08:07 +0000 Subject: [PATCH 2/7] Add comments to method --- .../Packages/com.utilities.rest/Runtime/Rest.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index a61d072..35b1d6d 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -983,6 +983,15 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName) return filePath; } + /// + /// Download a file from the provided and return the array of its contents. + /// + /// The url to download the file from. + /// Optional, file name to download (including extension). + /// Optional, . + /// Optional, debug http request. + /// Optional, . + /// The of the downloaded file. public static async Task DownloadFileBytesAsync( string url, string fileName = null, @@ -1010,8 +1019,6 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName) return bytes; } - - #endregion Get Multimedia Content /// From 346fade76509fd382ef1870b96654762eb3a5cd9 Mon Sep 17 00:00:00 2001 From: SimonDarksideJ Date: Sat, 6 Jan 2024 17:30:35 +0000 Subject: [PATCH 3/7] Added DownloadBytes method --- .../com.utilities.rest/Runtime/Rest.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index 35b1d6d..5115690 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -1019,6 +1019,31 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName) return bytes; } + + /// + /// Download a from the provided . + /// + /// The url to download from. + /// Optional, . + /// Optional, debug http request. + /// Optional, . + /// The downloaded from the server. + /// This call cannot be cached, if you want the cached version of a , then use the function. + public static async Task DownloadBytesAsync( + string url, + RestParameters parameters = null, + bool debug = false, + CancellationToken cancellationToken = default) + { + await Awaiters.UnityMainThread; + + using var webRequest = UnityWebRequest.Get(url); + using var downloadHandlerBuffer = new DownloadHandlerBuffer(); + webRequest.downloadHandler = downloadHandlerBuffer; + var response = await webRequest.SendAsync(parameters, cancellationToken); + response.Validate(debug); + return response.Data; + } #endregion Get Multimedia Content /// From 2df83ffd73cd74824c7b8016fb6a43042e83be8c Mon Sep 17 00:00:00 2001 From: "Simon (Darkside) Jackson" Date: Sat, 6 Jan 2024 17:54:31 +0000 Subject: [PATCH 4/7] Update Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs Co-authored-by: Stephen Hodgson --- Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index 5115690..8165754 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -1003,7 +1003,7 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName) byte[] bytes = null; var filePath = await DownloadFileAsync(url, fileName, parameters, false, cancellationToken); - var absolutefilePath = filePath.Replace("file://", ""); + var absolutefilePath = filePath.Replace("file://", string.Empty); if (File.Exists(absolutefilePath)) { try From f86797d2dd0304926b5ac8696db781c793f17f7a Mon Sep 17 00:00:00 2001 From: "Simon (Darkside) Jackson" Date: Sat, 6 Jan 2024 17:54:44 +0000 Subject: [PATCH 5/7] Update Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs Co-authored-by: Stephen Hodgson --- Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index 8165754..c115a47 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -1004,6 +1004,7 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName) var filePath = await DownloadFileAsync(url, fileName, parameters, false, cancellationToken); var absolutefilePath = filePath.Replace("file://", string.Empty); + if (File.Exists(absolutefilePath)) { try From 0387286f726c7e2f717dcf2cb4a79cb20a5adc25 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Sun, 14 Jan 2024 19:59:20 -0500 Subject: [PATCH 6/7] a bit of cleanup --- .../com.utilities.rest/Runtime/Rest.cs | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index 3836d94..2550ee6 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -913,32 +913,30 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName) } /// - /// Download a file from the provided and return the array of its contents. + /// Download a file from the provided and return the contents as bytes. /// /// The url to download the file from. /// Optional, file name to download (including extension). /// Optional, . - /// Optional, debug http request. /// Optional, . - /// The of the downloaded file. + /// The bytes of the downloaded file. public static async Task DownloadFileBytesAsync( - string url, - string fileName = null, - RestParameters parameters = null, - bool debug = false, - CancellationToken cancellationToken = default) + string url, + string fileName = null, + RestParameters parameters = null, + CancellationToken cancellationToken = default) { await Awaiters.UnityMainThread; byte[] bytes = null; - var filePath = await DownloadFileAsync(url, fileName, parameters, false, cancellationToken); + var filePath = await DownloadFileAsync(url, fileName, parameters, cancellationToken); var absolutefilePath = filePath.Replace("file://", string.Empty); if (File.Exists(absolutefilePath)) { try { - bytes = File.ReadAllBytes(absolutefilePath); + bytes = await File.ReadAllBytesAsync(absolutefilePath, cancellationToken); } catch (Exception ex) { @@ -951,19 +949,17 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName) } /// - /// Download a from the provided . + /// Download raw file contents from the provided . /// /// The url to download from. /// Optional, . - /// Optional, debug http request. /// Optional, . - /// The downloaded from the server. - /// This call cannot be cached, if you want the cached version of a , then use the function. + /// The bytes downloaded from the server. + /// This call cannot be cached, if you want the cached version of the file, then use the function. public static async Task DownloadBytesAsync( - string url, - RestParameters parameters = null, - bool debug = false, - CancellationToken cancellationToken = default) + string url, + RestParameters parameters = null, + CancellationToken cancellationToken = default) { await Awaiters.UnityMainThread; @@ -971,9 +967,10 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName) using var downloadHandlerBuffer = new DownloadHandlerBuffer(); webRequest.downloadHandler = downloadHandlerBuffer; var response = await webRequest.SendAsync(parameters, cancellationToken); - response.Validate(debug); + response.Validate(parameters?.Debug ?? false); return response.Data; } + #endregion Get Multimedia Content /// From f3be2baa6d30d3c81434d504246506aa89f71a01 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Sun, 14 Jan 2024 20:40:02 -0500 Subject: [PATCH 7/7] change requests --- .../com.utilities.rest/Runtime/Rest.cs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index 2550ee6..0596ced 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -928,21 +928,12 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName) { await Awaiters.UnityMainThread; byte[] bytes = null; - var filePath = await DownloadFileAsync(url, fileName, parameters, cancellationToken); - var absolutefilePath = filePath.Replace("file://", string.Empty); + var localPath = filePath.Replace("file://", string.Empty); - if (File.Exists(absolutefilePath)) + if (File.Exists(localPath)) { - try - { - bytes = await File.ReadAllBytesAsync(absolutefilePath, cancellationToken); - } - catch (Exception ex) - { - Debug.LogError(ex); - throw; - } + bytes = await File.ReadAllBytesAsync(localPath, cancellationToken).ConfigureAwait(true); } return bytes; @@ -955,14 +946,13 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName) /// Optional, . /// Optional, . /// The bytes downloaded from the server. - /// This call cannot be cached, if you want the cached version of the file, then use the function. + /// This request does not cache results. public static async Task DownloadBytesAsync( string url, RestParameters parameters = null, CancellationToken cancellationToken = default) { await Awaiters.UnityMainThread; - using var webRequest = UnityWebRequest.Get(url); using var downloadHandlerBuffer = new DownloadHandlerBuffer(); webRequest.downloadHandler = downloadHandlerBuffer;