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

Feature/ Add byte[] download methods #64

Prev Previous commit
change requests
  • Loading branch information
StephenHodgson committed Jan 15, 2024
commit f3be2baa6d30d3c81434d504246506aa89f71a01
18 changes: 4 additions & 14 deletions Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:https://", string.Empty);
var localPath = filePath.Replace("file:https://", 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;
Expand All @@ -955,14 +946,13 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName)
/// <param name="parameters">Optional, <see cref="RestParameters"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>The bytes downloaded from the server.</returns>
/// <remarks>This call cannot be cached, if you want the cached version of the file, then use the <see cref="DownloadFileBytesAsync"/> function.</remarks>
/// <remarks>This request does not cache results.</remarks>
public static async Task<byte[]> 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;
Expand Down