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
Next Next commit
a bit of cleanup
  • Loading branch information
StephenHodgson committed Jan 15, 2024
commit 0387286f726c7e2f717dcf2cb4a79cb20a5adc25
35 changes: 16 additions & 19 deletions Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -913,32 +913,30 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName)
}

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

var filePath = await DownloadFileAsync(url, fileName, parameters, false, cancellationToken);
var filePath = await DownloadFileAsync(url, fileName, parameters, cancellationToken);
var absolutefilePath = filePath.Replace("file:https://", string.Empty);

if (File.Exists(absolutefilePath))
SimonDarksideJ marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
bytes = File.ReadAllBytes(absolutefilePath);
bytes = await File.ReadAllBytesAsync(absolutefilePath, cancellationToken);
}
catch (Exception ex)
{
Expand All @@ -951,29 +949,28 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName)
}

/// <summary>
/// Download a <see cref="byte[]"/> from the provided <see cref="url"/>.
/// 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="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>
/// <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>
public static async Task<byte[]> DownloadBytesAsync(
string url,
RestParameters parameters = null,
bool debug = false,
CancellationToken cancellationToken = default)
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(debug);
response.Validate(parameters?.Debug ?? false);
return response.Data;
}

#endregion Get Multimedia Content

/// <summary>
Expand Down