Skip to content

Commit

Permalink
Feature/ Add byte[] download methods (#64)
Browse files Browse the repository at this point in the history
# Description

Add two methods for handling byte[] downloading:

* Added `DownloadFileBytesAsync` method - Downloads a file (with
caching) and returns the byte[] of the file.
* Added `DownloadBytesAsync` method - Only downloads and returns the
byte[] from a url, no caching.

---------

Co-authored-by: Stephen Hodgson <[email protected]>
Co-authored-by: Stephen Hodgson <[email protected]>
  • Loading branch information
3 people committed Jan 15, 2024
1 parent b82d7cb commit 1fa9591
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,55 @@ public static async Task<string> DownloadFileAsync(
return filePath;
}

/// <summary>
/// Download a file from the provided <see cref="url"/> and return the contents as bytes.
/// </summary>
/// <param name="url">The url to download the file from.</param>
/// <param name="fileName">Optional, file name to download (including extension).</param>
/// <param name="parameters">Optional, <see cref="RestParameters"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>The bytes of the downloaded file.</returns>
public static async Task<byte[]> DownloadFileBytesAsync(
string url,
string fileName = null,
RestParameters parameters = null,
CancellationToken cancellationToken = default)
{
await Awaiters.UnityMainThread;
byte[] bytes = null;
var filePath = await DownloadFileAsync(url, fileName, parameters, cancellationToken);
var localPath = filePath.Replace("file:https://", string.Empty);

if (File.Exists(localPath))
{
bytes = await File.ReadAllBytesAsync(localPath, cancellationToken).ConfigureAwait(true);
}

return bytes;
}

/// <summary>
/// Download raw file contents from the provided <see cref="url"/>.
/// </summary>
/// <param name="url">The url to download from.</param>
/// <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 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;
var response = await webRequest.SendAsync(parameters, cancellationToken);
response.Validate(parameters?.Debug ?? false);
return response.Data;
}

#endregion Get Multimedia Content

/// <summary>
Expand Down

0 comments on commit 1fa9591

Please sign in to comment.