From 75153b46d7c7392ee8fb4fe9735d748ffcfb3ae9 Mon Sep 17 00:00:00 2001 From: Andreas Herbig Date: Thu, 3 Aug 2023 11:12:04 +0200 Subject: [PATCH 01/10] Extract DownloadCache from Rest class --- .../Runtime/DownloadCache.cs | 105 ++++++++++++++++++ .../Runtime/DownloadCache.cs.meta | 3 + .../Runtime/Interfaces/IDownloadCache.cs | 13 +++ .../Runtime/Interfaces/IDownloadCache.cs.meta | 3 + .../com.utilities.rest/Runtime/Rest.cs | 78 ++----------- 5 files changed, 132 insertions(+), 70 deletions(-) create mode 100644 Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs create mode 100644 Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs.meta create mode 100644 Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs create mode 100644 Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs.meta diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs new file mode 100644 index 0000000..ae0017a --- /dev/null +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs @@ -0,0 +1,105 @@ +using System; +using System.IO; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; +using Utilities.Async; +using Utilities.WebRequestRest.Interfaces; + +namespace Utilities.WebRequestRest +{ + public class DownloadCache : IDownloadCache + { + private const string fileUriPrefix = "file://"; + private const string DOWNLOAD_CACHE = "download_cache"; + + /// + /// Generates a based on the string. + /// + /// The string to generate the . + /// A new that represents the string. + private static Guid GenerateGuid(string @string) + { + using MD5 md5 = MD5.Create(); + return new Guid(md5.ComputeHash(Encoding.Default.GetBytes(@string))); + } + + /// + /// The download cache directory.
+ ///
+ private static string DownloadCacheDirectory + => Path.Combine(Application.temporaryCachePath, DOWNLOAD_CACHE); + + public void ValidateCacheDirectory() + { + if (!Directory.Exists(DownloadCacheDirectory)) + { + Directory.CreateDirectory(DownloadCacheDirectory); + } + } + + public async Task ValidateCacheDirectoryAsync() + { + await Awaiters.UnityMainThread; + ValidateCacheDirectory(); + } + + public bool TryGetDownloadCacheItem(string uri, out string filePath) + { + ValidateCacheDirectory(); + bool exists; + + if (uri.Contains(fileUriPrefix)) + { + filePath = uri; + return File.Exists(uri.Replace(fileUriPrefix, string.Empty)); + } + + if (Rest.TryGetFileNameFromUrl(uri, out var fileName)) + { + filePath = Path.Combine(DownloadCacheDirectory, fileName); + exists = File.Exists(filePath); + } + else + { + filePath = Path.Combine(DownloadCacheDirectory, GenerateGuid(uri).ToString()); + exists = File.Exists(filePath); + } + + if (exists) + { + filePath = $"{fileUriPrefix}{Path.GetFullPath(filePath)}"; + } + + return exists; + } + + public bool TryDeleteCacheItem(string uri) + { + if (!TryGetDownloadCacheItem(uri, out var filePath)) + { + return false; + } + + try + { + File.Delete(filePath); + } + catch (Exception e) + { + Debug.LogError(e); + } + + return !File.Exists(filePath); + } + + public void DeleteDownloadCache() + { + if (Directory.Exists(DownloadCacheDirectory)) + { + Directory.Delete(DownloadCacheDirectory, true); + } + } + } +} diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs.meta b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs.meta new file mode 100644 index 0000000..e32f71a --- /dev/null +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c79cf124fa5d45a1b39caf8d6c057d14 +timeCreated: 1691048782 \ No newline at end of file diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs new file mode 100644 index 0000000..cdd54d1 --- /dev/null +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; + +namespace Utilities.WebRequestRest.Interfaces +{ + public interface IDownloadCache + { + void ValidateCacheDirectory(); + Task ValidateCacheDirectoryAsync(); + bool TryGetDownloadCacheItem(string uri, out string filePath); + bool TryDeleteCacheItem(string uri); + void DeleteDownloadCache(); + } +} diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs.meta b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs.meta new file mode 100644 index 0000000..f9cbc64 --- /dev/null +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5b070bc88bbb4390921d907c8012d0a5 +timeCreated: 1691048260 \ No newline at end of file diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index d999289..5ea0c8d 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -12,6 +12,7 @@ using UnityEngine; using UnityEngine.Networking; using Utilities.Async; +using Utilities.WebRequestRest.Interfaces; using Debug = UnityEngine.Debug; namespace Utilities.WebRequestRest @@ -347,43 +348,22 @@ public static async Task DeleteAsync( #region Download Cache - private const string DOWNLOAD_CACHE = "download_cache"; - - /// - /// Generates a based on the string. - /// - /// The string to generate the . - /// A new that represents the string. - private static Guid GenerateGuid(string @string) - { - using MD5 md5 = MD5.Create(); - return new Guid(md5.ComputeHash(Encoding.Default.GetBytes(@string))); - } - - /// - /// The download cache directory.
- ///
- public static string DownloadCacheDirectory - => Path.Combine(Application.temporaryCachePath, DOWNLOAD_CACHE); + private static IDownloadCache Cache { get; } = new DownloadCache(); /// /// Creates the if it doesn't exist. /// public static void ValidateCacheDirectory() { - if (!Directory.Exists(DownloadCacheDirectory)) - { - Directory.CreateDirectory(DownloadCacheDirectory); - } + Cache.ValidateCacheDirectory(); } /// /// Creates the if it doesn't exist. /// - public static async Task ValidateCacheDirectoryAsync() + public static Task ValidateCacheDirectoryAsync() { - await Awaiters.UnityMainThread; - ValidateCacheDirectory(); + return Cache.ValidateCacheDirectoryAsync(); } /// @@ -394,32 +374,7 @@ public static async Task ValidateCacheDirectoryAsync() /// True, if the item was in cache, otherwise false. public static bool TryGetDownloadCacheItem(string uri, out string filePath) { - ValidateCacheDirectory(); - bool exists; - - if (uri.Contains(fileUriPrefix)) - { - filePath = uri; - return File.Exists(uri.Replace(fileUriPrefix, string.Empty)); - } - - if (TryGetFileNameFromUrl(uri, out var fileName)) - { - filePath = Path.Combine(DownloadCacheDirectory, fileName); - exists = File.Exists(filePath); - } - else - { - filePath = Path.Combine(DownloadCacheDirectory, GenerateGuid(uri).ToString()); - exists = File.Exists(filePath); - } - - if (exists) - { - filePath = $"{fileUriPrefix}{Path.GetFullPath(filePath)}"; - } - - return exists; + return Cache.TryGetDownloadCacheItem(uri, out filePath); } /// @@ -429,21 +384,7 @@ public static bool TryGetDownloadCacheItem(string uri, out string filePath) /// True, if the cached item was successfully deleted. public static bool TryDeleteCacheItem(string uri) { - if (!TryGetDownloadCacheItem(uri, out var filePath)) - { - return false; - } - - try - { - File.Delete(filePath); - } - catch (Exception e) - { - Debug.LogError(e); - } - - return !File.Exists(filePath); + return Cache.TryDeleteCacheItem(uri); } /// @@ -451,10 +392,7 @@ public static bool TryDeleteCacheItem(string uri) /// public static void DeleteDownloadCache() { - if (Directory.Exists(DownloadCacheDirectory)) - { - Directory.Delete(DownloadCacheDirectory, true); - } + Cache.DeleteDownloadCache(); } /// From bb2d0c059e414e2c5632b995c16dae0435a1fe4f Mon Sep 17 00:00:00 2001 From: Andreas Herbig Date: Thu, 3 Aug 2023 11:21:55 +0200 Subject: [PATCH 02/10] Extract filewriting to seperate method --- .../com.utilities.rest/Runtime/Rest.cs | 59 +++++++++---------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index 5ea0c8d..e5e8725 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -464,23 +464,9 @@ public static async Task DownloadTextureAsync( var downloadHandler = (DownloadHandlerTexture)webRequest.downloadHandler; - if (!isCached && - !File.Exists(cachePath)) + if (!isCached) { - var fileStream = File.OpenWrite(cachePath); - - try - { - await fileStream.WriteAsync(downloadHandler.data, 0, downloadHandler.data.Length, cancellationToken); - } - catch (Exception e) - { - Debug.LogError($"Failed to write texture to disk!\n{e}"); - } - finally - { - await fileStream.DisposeAsync(); - } + await CacheItemToPath(downloadHandler.data, cachePath, cancellationToken); } await Awaiters.UnityMainThread; @@ -490,6 +476,29 @@ public static async Task DownloadTextureAsync( return texture; } + public static async Task CacheItemToPath(byte[] data, string cachePath, CancellationToken cancellationToken) + { + if (File.Exists(cachePath)) + { + return; + } + + var fileStream = File.OpenWrite(cachePath); + + try + { + await fileStream.WriteAsync(data, 0, data.Length, cancellationToken); + } + catch (Exception e) + { + Debug.LogError($"Failed to write audio asset to disk! {e}"); + } + finally + { + await fileStream.DisposeAsync(); + } + } + /// /// Download a from the provided . /// @@ -543,23 +552,9 @@ public static async Task DownloadAudioClipAsync( var downloadHandler = (DownloadHandlerAudioClip)webRequest.downloadHandler; - if (!isCached && - !File.Exists(cachePath)) + if (!isCached) { - var fileStream = File.OpenWrite(cachePath); - - try - { - await fileStream.WriteAsync(downloadHandler.data, 0, downloadHandler.data.Length, cancellationToken); - } - catch (Exception e) - { - Debug.LogError($"Failed to write audio asset to disk! {e}"); - } - finally - { - await fileStream.DisposeAsync(); - } + await CacheItemToPath(downloadHandler.data, cachePath, cancellationToken); } await Awaiters.UnityMainThread; From 626f2ce478b7c9b5d666d7a9aa17d579ca58b75a Mon Sep 17 00:00:00 2001 From: Andreas Herbig Date: Thu, 3 Aug 2023 11:36:03 +0200 Subject: [PATCH 03/10] Move filewriting caching operation into download cache --- .../Runtime/DownloadCache.cs | 24 +++++++++ .../Runtime/Interfaces/IDownloadCache.cs | 4 +- .../com.utilities.rest/Runtime/Rest.cs | 51 ++----------------- 3 files changed, 31 insertions(+), 48 deletions(-) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs index ae0017a..873436b 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs @@ -2,6 +2,7 @@ using System.IO; using System.Security.Cryptography; using System.Text; +using System.Threading; using System.Threading.Tasks; using UnityEngine; using Utilities.Async; @@ -101,5 +102,28 @@ public void DeleteDownloadCache() Directory.Delete(DownloadCacheDirectory, true); } } + + public async Task CacheItemAsync(byte[] data, string cachePath, CancellationToken cancellationToken) + { + if (File.Exists(cachePath)) + { + return; + } + + var fileStream = File.OpenWrite(cachePath); + + try + { + await fileStream.WriteAsync(data, 0, data.Length, cancellationToken); + } + catch (Exception e) + { + Debug.LogError($"Failed to write audio asset to disk! {e}"); + } + finally + { + await fileStream.DisposeAsync(); + } + } } } diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs index cdd54d1..fe5e2d9 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Threading; +using System.Threading.Tasks; namespace Utilities.WebRequestRest.Interfaces { @@ -9,5 +10,6 @@ public interface IDownloadCache bool TryGetDownloadCacheItem(string uri, out string filePath); bool TryDeleteCacheItem(string uri); void DeleteDownloadCache(); + Task CacheItemAsync(byte[] data, string cachePath, CancellationToken cancellationToken); } } diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index e5e8725..d7fd8fe 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -433,18 +433,8 @@ public static async Task DownloadTextureAsync( TryGetFileNameFromUrl(url, out fileName); } - bool isCached; string cachePath; - - if (url.Contains(fileUriPrefix)) - { - isCached = true; - cachePath = url; - } - else - { - isCached = TryGetDownloadCacheItem(fileName, out cachePath); - } + bool isCached = TryGetDownloadCacheItem(fileName, out cachePath); if (isCached) { @@ -466,7 +456,7 @@ public static async Task DownloadTextureAsync( if (!isCached) { - await CacheItemToPath(downloadHandler.data, cachePath, cancellationToken); + await Cache.CacheItemAsync(downloadHandler.data, cachePath, cancellationToken); } await Awaiters.UnityMainThread; @@ -476,29 +466,6 @@ public static async Task DownloadTextureAsync( return texture; } - public static async Task CacheItemToPath(byte[] data, string cachePath, CancellationToken cancellationToken) - { - if (File.Exists(cachePath)) - { - return; - } - - var fileStream = File.OpenWrite(cachePath); - - try - { - await fileStream.WriteAsync(data, 0, data.Length, cancellationToken); - } - catch (Exception e) - { - Debug.LogError($"Failed to write audio asset to disk! {e}"); - } - finally - { - await fileStream.DisposeAsync(); - } - } - /// /// Download a from the provided . /// @@ -522,18 +489,8 @@ public static async Task DownloadAudioClipAsync( TryGetFileNameFromUrl(url, out fileName); } - bool isCached; string cachePath; - - if (url.Contains(fileUriPrefix)) - { - isCached = true; - cachePath = url; - } - else - { - isCached = TryGetDownloadCacheItem(fileName, out cachePath); - } + bool isCached = TryGetDownloadCacheItem(fileName, out cachePath); if (isCached) { @@ -554,7 +511,7 @@ public static async Task DownloadAudioClipAsync( if (!isCached) { - await CacheItemToPath(downloadHandler.data, cachePath, cancellationToken); + await Cache.CacheItemAsync(downloadHandler.data, cachePath, cancellationToken); } await Awaiters.UnityMainThread; From 5e6fe7d044938e31b1e3c628caf28d85ded22317 Mon Sep 17 00:00:00 2001 From: Andreas Herbig Date: Thu, 3 Aug 2023 11:47:09 +0200 Subject: [PATCH 04/10] Rename download cache that saves files to disk --- .../Runtime/{DownloadCache.cs => DiskDownloadCache.cs} | 4 ++-- .../{DownloadCache.cs.meta => DiskDownloadCache.cs.meta} | 0 Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename Utilities.Rest/Packages/com.utilities.rest/Runtime/{DownloadCache.cs => DiskDownloadCache.cs} (96%) rename Utilities.Rest/Packages/com.utilities.rest/Runtime/{DownloadCache.cs.meta => DiskDownloadCache.cs.meta} (100%) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs similarity index 96% rename from Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs rename to Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs index 873436b..7ea44e9 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs @@ -10,7 +10,7 @@ namespace Utilities.WebRequestRest { - public class DownloadCache : IDownloadCache + public class DiskDownloadCache : IDownloadCache { private const string fileUriPrefix = "file://"; private const string DOWNLOAD_CACHE = "download_cache"; @@ -118,7 +118,7 @@ public async Task CacheItemAsync(byte[] data, string cachePath, CancellationToke } catch (Exception e) { - Debug.LogError($"Failed to write audio asset to disk! {e}"); + Debug.LogError($"Failed to write asset to disk! {e}"); } finally { diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs.meta b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs.meta similarity index 100% rename from Utilities.Rest/Packages/com.utilities.rest/Runtime/DownloadCache.cs.meta rename to Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs.meta diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index d7fd8fe..59f5962 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -348,7 +348,7 @@ public static async Task DeleteAsync( #region Download Cache - private static IDownloadCache Cache { get; } = new DownloadCache(); + private static IDownloadCache Cache { get; } = new DiskDownloadCache(); /// /// Creates the if it doesn't exist. From 4c54431c09b4eed803742b6cdf02f6d5d4083fa5 Mon Sep 17 00:00:00 2001 From: Andreas Herbig Date: Thu, 3 Aug 2023 11:47:34 +0200 Subject: [PATCH 05/10] Add new download cache that does not cache at all --- .../Runtime/NoOpDownloadCache.cs | 40 +++++++++++++++++++ .../Runtime/NoOpDownloadCache.cs.meta | 3 ++ 2 files changed, 43 insertions(+) create mode 100644 Utilities.Rest/Packages/com.utilities.rest/Runtime/NoOpDownloadCache.cs create mode 100644 Utilities.Rest/Packages/com.utilities.rest/Runtime/NoOpDownloadCache.cs.meta diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/NoOpDownloadCache.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/NoOpDownloadCache.cs new file mode 100644 index 0000000..0bf7f95 --- /dev/null +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/NoOpDownloadCache.cs @@ -0,0 +1,40 @@ +using System.Threading; +using System.Threading.Tasks; +using Utilities.WebRequestRest.Interfaces; + +namespace Utilities.Rest +{ + public class NoOpDownloadCache : IDownloadCache + { + public void ValidateCacheDirectory() + { + + } + + public Task ValidateCacheDirectoryAsync() + { + return Task.CompletedTask; + } + + public bool TryGetDownloadCacheItem(string uri, out string filePath) + { + filePath = uri; + return false; + } + + public bool TryDeleteCacheItem(string uri) + { + return true; + } + + public void DeleteDownloadCache() + { + + } + + public Task CacheItemAsync(byte[] data, string cachePath, CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + } +} diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/NoOpDownloadCache.cs.meta b/Utilities.Rest/Packages/com.utilities.rest/Runtime/NoOpDownloadCache.cs.meta new file mode 100644 index 0000000..d881bd3 --- /dev/null +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/NoOpDownloadCache.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d3d49896318c45d6853ad1e32ce8b550 +timeCreated: 1691055806 \ No newline at end of file From 562d0539c9063f38c51024fab34467dd5086b721 Mon Sep 17 00:00:00 2001 From: Andreas Herbig Date: Thu, 3 Aug 2023 11:51:15 +0200 Subject: [PATCH 06/10] Use NoOpDownloadCache when on webGL --- Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index 59f5962..c542c82 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -12,6 +12,7 @@ using UnityEngine; using UnityEngine.Networking; using Utilities.Async; +using Utilities.Rest; using Utilities.WebRequestRest.Interfaces; using Debug = UnityEngine.Debug; @@ -348,7 +349,11 @@ public static async Task DeleteAsync( #region Download Cache +#if UNITY_WEBGL + private static IDownloadCache Cache { get; } = new NoOpDownloadCache(); +#else private static IDownloadCache Cache { get; } = new DiskDownloadCache(); +#endif /// /// Creates the if it doesn't exist. From 0fef20d5df42e3f1017d0eaa3a151ae60f4a3f1b Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Fri, 4 Aug 2023 19:34:31 -0400 Subject: [PATCH 07/10] some change requests for PR --- .../Runtime/DiskDownloadCache.cs | 27 ++++---- .../Runtime/Interfaces/IDownloadCache.cs | 13 +++- .../Runtime/NoOpDownloadCache.cs | 31 +++------ .../com.utilities.rest/Runtime/Rest.cs | 65 +++++++++++-------- 4 files changed, 67 insertions(+), 69 deletions(-) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs index 7ea44e9..801ab87 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs @@ -1,4 +1,6 @@ -using System; +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using System; using System.IO; using System.Security.Cryptography; using System.Text; @@ -10,10 +12,9 @@ namespace Utilities.WebRequestRest { - public class DiskDownloadCache : IDownloadCache + internal class DiskDownloadCache : IDownloadCache { private const string fileUriPrefix = "file://"; - private const string DOWNLOAD_CACHE = "download_cache"; /// /// Generates a based on the string. @@ -26,17 +27,11 @@ private static Guid GenerateGuid(string @string) return new Guid(md5.ComputeHash(Encoding.Default.GetBytes(@string))); } - /// - /// The download cache directory.
- ///
- private static string DownloadCacheDirectory - => Path.Combine(Application.temporaryCachePath, DOWNLOAD_CACHE); - public void ValidateCacheDirectory() { - if (!Directory.Exists(DownloadCacheDirectory)) + if (!Directory.Exists(Rest.DownloadCacheDirectory)) { - Directory.CreateDirectory(DownloadCacheDirectory); + Directory.CreateDirectory(Rest.DownloadCacheDirectory); } } @@ -59,12 +54,12 @@ public bool TryGetDownloadCacheItem(string uri, out string filePath) if (Rest.TryGetFileNameFromUrl(uri, out var fileName)) { - filePath = Path.Combine(DownloadCacheDirectory, fileName); + filePath = Path.Combine(Rest.DownloadCacheDirectory, fileName); exists = File.Exists(filePath); } else { - filePath = Path.Combine(DownloadCacheDirectory, GenerateGuid(uri).ToString()); + filePath = Path.Combine(Rest.DownloadCacheDirectory, GenerateGuid(uri).ToString()); exists = File.Exists(filePath); } @@ -97,13 +92,13 @@ public bool TryDeleteCacheItem(string uri) public void DeleteDownloadCache() { - if (Directory.Exists(DownloadCacheDirectory)) + if (Directory.Exists(Rest.DownloadCacheDirectory)) { - Directory.Delete(DownloadCacheDirectory, true); + Directory.Delete(Rest.DownloadCacheDirectory, true); } } - public async Task CacheItemAsync(byte[] data, string cachePath, CancellationToken cancellationToken) + public async Task WriteCacheItemAsync(byte[] data, string cachePath, CancellationToken cancellationToken) { if (File.Exists(cachePath)) { diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs index fe5e2d9..f7226b0 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Interfaces/IDownloadCache.cs @@ -1,15 +1,22 @@ -using System.Threading; +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using System.Threading; using System.Threading.Tasks; namespace Utilities.WebRequestRest.Interfaces { - public interface IDownloadCache + internal interface IDownloadCache { void ValidateCacheDirectory(); + Task ValidateCacheDirectoryAsync(); + bool TryGetDownloadCacheItem(string uri, out string filePath); + bool TryDeleteCacheItem(string uri); + void DeleteDownloadCache(); - Task CacheItemAsync(byte[] data, string cachePath, CancellationToken cancellationToken); + + Task WriteCacheItemAsync(byte[] data, string cachePath, CancellationToken cancellationToken); } } diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/NoOpDownloadCache.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/NoOpDownloadCache.cs index 0bf7f95..a0ed74e 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/NoOpDownloadCache.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/NoOpDownloadCache.cs @@ -1,20 +1,16 @@ -using System.Threading; +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using System.Threading; using System.Threading.Tasks; using Utilities.WebRequestRest.Interfaces; namespace Utilities.Rest { - public class NoOpDownloadCache : IDownloadCache + internal class NoOpDownloadCache : IDownloadCache { - public void ValidateCacheDirectory() - { + public void ValidateCacheDirectory() { } - } - - public Task ValidateCacheDirectoryAsync() - { - return Task.CompletedTask; - } + public Task ValidateCacheDirectoryAsync() => Task.CompletedTask; public bool TryGetDownloadCacheItem(string uri, out string filePath) { @@ -22,19 +18,10 @@ public bool TryGetDownloadCacheItem(string uri, out string filePath) return false; } - public bool TryDeleteCacheItem(string uri) - { - return true; - } - - public void DeleteDownloadCache() - { + public bool TryDeleteCacheItem(string uri) => true; - } + public void DeleteDownloadCache() { } - public Task CacheItemAsync(byte[] data, string cachePath, CancellationToken cancellationToken) - { - return Task.CompletedTask; - } + public Task WriteCacheItemAsync(byte[] data, string cachePath, CancellationToken cancellationToken) => Task.CompletedTask; } } diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index c542c82..a8d6aeb 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -5,7 +5,6 @@ using System.IO; using System.Linq; using System.Runtime.CompilerServices; -using System.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -349,27 +348,46 @@ public static async Task DeleteAsync( #region Download Cache -#if UNITY_WEBGL - private static IDownloadCache Cache { get; } = new NoOpDownloadCache(); -#else - private static IDownloadCache Cache { get; } = new DiskDownloadCache(); -#endif + private const string DOWNLOAD_CACHE = "download_cache"; /// - /// Creates the if it doesn't exist. + /// The download cache directory.
///
- public static void ValidateCacheDirectory() + public static string DownloadCacheDirectory + => Path.Combine(Application.temporaryCachePath, DOWNLOAD_CACHE); + + private static IDownloadCache cache; + + private static IDownloadCache Cache { - Cache.ValidateCacheDirectory(); + get + { + if (cache != null) + { + return cache; + } + + cache = Application.platform switch + { + RuntimePlatform.WebGLPlayer => new NoOpDownloadCache(), + _ => new DiskDownloadCache() + }; + + return cache; + } } + /// + /// Creates the if it doesn't exist. + /// + public static void ValidateCacheDirectory() + => Cache.ValidateCacheDirectory(); + /// /// Creates the if it doesn't exist. /// public static Task ValidateCacheDirectoryAsync() - { - return Cache.ValidateCacheDirectoryAsync(); - } + => Cache.ValidateCacheDirectoryAsync(); /// /// Try to get a file out of the download cache by uri reference. @@ -378,9 +396,7 @@ public static Task ValidateCacheDirectoryAsync() /// The file path to the cached item. /// True, if the item was in cache, otherwise false. public static bool TryGetDownloadCacheItem(string uri, out string filePath) - { - return Cache.TryGetDownloadCacheItem(uri, out filePath); - } + => Cache.TryGetDownloadCacheItem(uri, out filePath); /// /// Try to delete the cached item at the uri. @@ -388,17 +404,13 @@ public static bool TryGetDownloadCacheItem(string uri, out string filePath) /// The uri key of the item. /// True, if the cached item was successfully deleted. public static bool TryDeleteCacheItem(string uri) - { - return Cache.TryDeleteCacheItem(uri); - } + => Cache.TryDeleteCacheItem(uri); /// /// Deletes all the files in the download cache. /// public static void DeleteDownloadCache() - { - Cache.DeleteDownloadCache(); - } + => Cache.DeleteDownloadCache(); /// /// We will try go guess the name based on the url. @@ -438,8 +450,7 @@ public static async Task DownloadTextureAsync( TryGetFileNameFromUrl(url, out fileName); } - string cachePath; - bool isCached = TryGetDownloadCacheItem(fileName, out cachePath); + var isCached = TryGetDownloadCacheItem(fileName, out var cachePath); if (isCached) { @@ -461,7 +472,7 @@ public static async Task DownloadTextureAsync( if (!isCached) { - await Cache.CacheItemAsync(downloadHandler.data, cachePath, cancellationToken); + await Cache.WriteCacheItemAsync(downloadHandler.data, cachePath, cancellationToken); } await Awaiters.UnityMainThread; @@ -494,8 +505,7 @@ public static async Task DownloadAudioClipAsync( TryGetFileNameFromUrl(url, out fileName); } - string cachePath; - bool isCached = TryGetDownloadCacheItem(fileName, out cachePath); + var isCached = TryGetDownloadCacheItem(fileName, out var cachePath); if (isCached) { @@ -516,7 +526,7 @@ public static async Task DownloadAudioClipAsync( if (!isCached) { - await Cache.CacheItemAsync(downloadHandler.data, cachePath, cancellationToken); + await Cache.WriteCacheItemAsync(downloadHandler.data, cachePath, cancellationToken); } await Awaiters.UnityMainThread; @@ -793,7 +803,6 @@ public static async Task DownloadFileAsync( if (!response.Successful) { Debug.LogError($"Failed to download file from \"{url}\"!\n[{response.Code}] {response.Body}"); - return null; } From 363d46f3c17f5fa344ef8b29594f96dd45bd5f9b Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Fri, 4 Aug 2023 19:41:01 -0400 Subject: [PATCH 08/10] a bit more refactoring --- .../com.utilities.rest/Runtime/DiskDownloadCache.cs | 8 +++----- .../Packages/com.utilities.rest/Runtime/Rest.cs | 6 +++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs index 801ab87..9dbab90 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/DiskDownloadCache.cs @@ -14,8 +14,6 @@ namespace Utilities.WebRequestRest { internal class DiskDownloadCache : IDownloadCache { - private const string fileUriPrefix = "file://"; - /// /// Generates a based on the string. /// @@ -46,10 +44,10 @@ public bool TryGetDownloadCacheItem(string uri, out string filePath) ValidateCacheDirectory(); bool exists; - if (uri.Contains(fileUriPrefix)) + if (uri.Contains(Rest.FileUriPrefix)) { filePath = uri; - return File.Exists(uri.Replace(fileUriPrefix, string.Empty)); + return File.Exists(uri.Replace(Rest.FileUriPrefix, string.Empty)); } if (Rest.TryGetFileNameFromUrl(uri, out var fileName)) @@ -65,7 +63,7 @@ public bool TryGetDownloadCacheItem(string uri, out string filePath) if (exists) { - filePath = $"{fileUriPrefix}{Path.GetFullPath(filePath)}"; + filePath = $"{Rest.FileUriPrefix}{Path.GetFullPath(filePath)}"; } return exists; diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index a8d6aeb..9cf4aab 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -22,8 +22,8 @@ namespace Utilities.WebRequestRest /// public static class Rest { + internal const string FileUriPrefix = "file://"; private const string kHttpVerbPATCH = "PATCH"; - private const string fileUriPrefix = "file://"; private const string eventDelimiter = "data: "; private const string stopEventDelimiter = "[DONE]"; @@ -421,7 +421,7 @@ public static void DeleteDownloadCache() public static bool TryGetFileNameFromUrl(string url, out string fileName) { var baseUrl = UnityWebRequest.UnEscapeURL(url); - var rootUrl = baseUrl.Split("?")[0]; + var rootUrl = baseUrl.Split('?')[0]; var index = rootUrl.LastIndexOf('/') + 1; fileName = rootUrl.Substring(index, rootUrl.Length - index); return Path.HasExtension(fileName); @@ -569,7 +569,7 @@ public static async Task StreamAudioAsync( TryGetFileNameFromUrl(url, out fileName); } - if (url.Contains(fileUriPrefix)) + if (url.Contains(FileUriPrefix)) { // override the httpMethod httpMethod = UnityWebRequest.kHttpVerbGET; From 086fee8f403b257cebb6b12e64abf00ea64c2cc3 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Fri, 4 Aug 2023 19:42:37 -0400 Subject: [PATCH 09/10] reorganized --- .../Packages/com.utilities.rest/Runtime/Rest.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs index 9cf4aab..a44b858 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -350,12 +350,6 @@ public static async Task DeleteAsync( private const string DOWNLOAD_CACHE = "download_cache"; - /// - /// The download cache directory.
- ///
- public static string DownloadCacheDirectory - => Path.Combine(Application.temporaryCachePath, DOWNLOAD_CACHE); - private static IDownloadCache cache; private static IDownloadCache Cache @@ -377,6 +371,12 @@ private static IDownloadCache Cache } } + /// + /// The download cache directory.
+ ///
+ public static string DownloadCacheDirectory + => Path.Combine(Application.temporaryCachePath, DOWNLOAD_CACHE); + /// /// Creates the if it doesn't exist. /// From 5fa6946b9ee66ae285a4ebf24fc1980867dd93a4 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Fri, 4 Aug 2023 19:46:11 -0400 Subject: [PATCH 10/10] revert some cache checks --- .../com.utilities.rest/Runtime/Rest.cs | 26 +++++++++++++++++-- 1 file changed, 24 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 a44b858..b188989 100644 --- a/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs +++ b/Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs @@ -450,7 +450,18 @@ public static async Task DownloadTextureAsync( TryGetFileNameFromUrl(url, out fileName); } - var isCached = TryGetDownloadCacheItem(fileName, out var cachePath); + bool isCached; + string cachePath; + + if (url.Contains(FileUriPrefix)) + { + isCached = true; + cachePath = url; + } + else + { + isCached = TryGetDownloadCacheItem(fileName, out cachePath); + } if (isCached) { @@ -505,7 +516,18 @@ public static async Task DownloadAudioClipAsync( TryGetFileNameFromUrl(url, out fileName); } - var isCached = TryGetDownloadCacheItem(fileName, out var cachePath); + bool isCached; + string cachePath; + + if (url.Contains(FileUriPrefix)) + { + isCached = true; + cachePath = url; + } + else + { + isCached = TryGetDownloadCacheItem(fileName, out cachePath); + } if (isCached) {