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

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 bool TryGetFileNameFromUrl(string url, out string fileName)
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