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

Add Support for a specific DownloadHandler #28

Closed
rengaw83 opened this issue Aug 23, 2018 · 1 comment
Closed

Add Support for a specific DownloadHandler #28

rengaw83 opened this issue Aug 23, 2018 · 1 comment

Comments

@rengaw83
Copy link

Currently for each UnityWebRequest the DownloadHandlerBuffer are used.
See the extension class

In my unity project i has to download large files (>100mb) to a file on the pc or device.

With the byte buffer download handler the whole content of the file was loaded into the ram and when the download was finished the content can be written to the local file.

For these large files it would be nice to set a download handler manualy to the RequestHelper like this:

string srcUri = "http://myapi/ressource/load/bigfile";
string destPah = Path.Combine(Application.persistentDataPath, "bigfile");

DownloadHandlerFile dh = new DownloadHandlerFile(destPah);
dh.removeFileOnAbort = true;

Proyecto26.RequestHelper request = new Proyecto26.RequestHelper()
    {
        Uri = srcUri,
        DownloadHelper = dh,
    }
;

Proyecto26.RestClient.Get(request)
    .Then(
        () => {
            Debug.Log("Downloaded to " + destPah);
        }
    )
    .Catch(
        err => {
            Debug.Log("Error during download:" + err);
        }
    )
;

In the extension class the options can be checked like this:

if (options.DownloadHelper is DownloadHandler)
    request.downloadHandler = options.DownloadHelper;
else
    request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();

To add the download helper to the request helper the request helper needs to become the download helper property like this:

public class RequestHelper {
        public DownloadHandler DownloadHandler { get; set; }
}

There are a lot of useful download helpers out there. Like AssetBundle or Texture, ....

It wold be nice when you can add the support of a configurable download helper.

My currently workaround is to perform the web request by myself without your RestClient (without all the features and much more code).

public static RSG.IPromise LoadRessourceToFile(
    System.Uri url,
    string dest
)
{
    RSG.Promise promise = new RSG.Promise();

    UnityWebRequest request = new UnityWebRequest(url.AbsoluteUri, UnityWebRequest.kHttpVerbGET);

    DownloadHandlerFile dh = new DownloadHandlerFile(dest);
    dh.removeFileOnAbort = true;
    request.downloadHandler = dh;

    StartCoroutine(
        SendRequest(request, promise)
    );

    return promise;
}

private static System.Collections.IEnumerator SendRequest(
    UnityWebRequest request,
    RSG.Promise promise
)
{
    yield return request.SendWebRequest();

    if (request.isNetworkError || request.isHttpError)
        promise.Reject(new Exception(request.error));
    else
        promise.Resolve();

    yield break;
}
@jdnichollsc
Copy link
Member

Done, thanks! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants