Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch fix for Rest calls #41

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions Runtime/Utilities/WebRequestRest/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace RealityCollective.Utilities.WebRequestRest
public static class Rest
{
#region Global Properties
public const string FileUriPrefix = "file://";
public static string DownloadLocation = Application.temporaryCachePath;
public static Dictionary<string, string> Headers = new Dictionary<string, string>();
public static IProgress<float> Progress = null;
Expand Down Expand Up @@ -325,11 +326,8 @@ public static bool TryGetDownloadCacheItem(string uri, out string filePath, stri
{
return !TryDeleteCacheItem(uri, downloadDirectory);
}
#if UNITY_STANDALONE || UNITY_WSA || UNITY_EDITOR_WIN
filePath = $"{Path.GetFullPath(filePath)}";
#else
filePath = $"file://{Path.GetFullPath(filePath)}";
#endif

filePath = $"{Rest.FileUriPrefix}{Path.GetFullPath(filePath)}";
}

return exists;
Expand Down Expand Up @@ -384,7 +382,7 @@ private static bool TryGetFileNameFromUrl(string url, out string fileName)
return Path.HasExtension(fileName);
}

#endregion Download Cache
#endregion Download Cache

/// <summary>
/// Download a <see cref="Texture2D"/> from the provided <see cref="url"/>.
Expand Down Expand Up @@ -416,6 +414,11 @@ public static async Task<Texture2D> DownloadTextureAsync(string url, string file
isCached = TryGetDownloadCacheItem(fileName, out cachePath, downloadLocation, ForceDownload || downloadTextureArgs.ForceDownload);
}

if (isCached)
{
url = cachePath;
}

using var webRequest = UnityWebRequestTexture.GetTexture(url);
var response = await ProcessRequestAsync(webRequest, downloadTextureArgs);

Expand Down Expand Up @@ -661,13 +664,16 @@ public static async Task<byte[]> DownloadFileBytesAsync(string url, string fileN
filePath = await DownloadFileAsync(url, fileName, downloadFileArgs);
}

// Strip the file:// prefix for local storage access
filePath = filePath.Replace(Rest.FileUriPrefix, "");

if (File.Exists(filePath))
{
try
{
bytes = File.ReadAllBytes(filePath);
}
catch (Exception ex)
catch (Exception ex)
{
Debug.LogError(GenerateErrorMessage("File as Bytes", url, ex));
}
Expand Down Expand Up @@ -716,7 +722,7 @@ public static async Task<string> DownloadFileStringAsync(string url, string file
return fileContents;
}

#endregion Get Multimedia Content
#endregion Get Multimedia Content

#region Private Functions

Expand Down