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

61 changes: 61 additions & 0 deletions Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,67 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName)
return filePath;
}

/// <summary>
/// Download a file from the provided <see cref="url"/> and return the <see cref="byte[]"/> array of its contents.
/// </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="debug">Optional, debug http request.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>The <see cref="byte[]"/> of the downloaded file.</returns>
public static async Task<byte[]> DownloadFileBytesAsync(
string url,
string fileName = null,
RestParameters parameters = null,
bool debug = false,
CancellationToken cancellationToken = default)
{
await Awaiters.UnityMainThread;
byte[] bytes = null;

var filePath = await DownloadFileAsync(url, fileName, parameters, false, cancellationToken);
StephenHodgson marked this conversation as resolved.
Show resolved Hide resolved
var absolutefilePath = filePath.Replace("file:https://", "");
SimonDarksideJ marked this conversation as resolved.
Show resolved Hide resolved
if (File.Exists(absolutefilePath))
SimonDarksideJ marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
bytes = File.ReadAllBytes(absolutefilePath);
}
catch (Exception ex)
{
Debug.LogError(ex);
throw;
StephenHodgson marked this conversation as resolved.
Show resolved Hide resolved
}
}

return bytes;
}

/// <summary>
/// Download a <see cref="byte[]"/> 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="debug">Optional, debug http request.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>The <see cref="byte[]"/> downloaded from the server.</returns>
/// <remarks>This call cannot be cached, if you want the cached version of a <see cref="byte[]"/>, then use the <see cref="DownloadFileBytesAsync"/> function.</remarks>
public static async Task<byte[]> DownloadBytesAsync(
string url,
RestParameters parameters = null,
bool debug = false,
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(debug);
return response.Data;
}
#endregion Get Multimedia Content

/// <summary>
Expand Down