Download a list of files from a url?

Hi, I have two use cases where I’d like to download files to my game’s folders, which I need to work in iOS, Windows, and Mac. I assume I’ll use the Downloader class? I’m on cocos2d-x 3.16, btw.

In one use case, they’d enter the full url to a specific file, like drive.google.com/myfolder/myleague.lgs

In the other use case, I’d need them to give a url to a folder, like drive.google.com/myimages, and I’d like to download all the files found in that folder.

Any thoughts on doing these two? I guess I’d have to do the work in a separate thread, so I can show them the progress and close the dialog after it’s finished (and also to let them stop it), right?

(oh, and a dumb question, I assume I don’t need to prepend http:// or https:// if it’s not there, right?)

Hmmm, I can’t even find the API for network::downloader?

thanks!

Have you thought about curl or wget?

Also have a look at this post: How to download zip file from url and save it on writable directory with cocos2d-x v3.11 by @CAPONE

Thanks, will do!

@slackmoehrle, I’m trying the example on that page, and trying to download a file from my google drive, a folder that I shared publicly. However, I get an error:

ERROR downloading, errorCode = -3 (60): Peer certificate cannot be authenticated with given CA certificates

** By the way, I can download the cocos2d-x zip file, so it’s something to do with trying to pull from a shared google drive. Oh, when downloading the cocos2d-x zip file, “expectedTotalBytes” stayed zero the whole time.

You got this error cause https protocol. You can use http link. Or you can look my another example HttpClient/Downloader cancel/abort a request download files by curl. I set CURLOPT_SSL_VERIFYPEER to 0 so this error wont appear. Or you can edit CCDownloader-curl.cpp on cocos/network and set CURLOPT_SSL_VERIFYPEER to 0.

Also, i dunno about google drive but when i downloaded from dropbox i needed set CURLOPT_FOLLOWLOCATION to 1 for handling redirects.

1 Like

Thanks, I’ll give your alternative a look! I like the Downloader class, but even when I try http, I get the same error. Hopefully, your example will work on Windows, Mac, and iOS, I’ll check it today. And I’ll dabble with Downloader some more.

A friend has suggested I just create a file share on Amazon S3 and host the files there. It does look as a very nice alternative.

@CAPONE, I’m having a problem including curl/curl.h. Dumb question, but how did you add curl for multiple platforms? Thanks.

With cocos it is not so easy. For some reason on cocos curl has separate headers for each platform at yourproject/cocos2d/external/curl/include/platformdir. And you need add path for each platform depend what IDE you use for build. For example for iOS you need add Header Search Path on Xcode, and for Andoid you need add lines LOCAL_STATIC_LIBRARIES += cocos_curl_static and $(call import-module,curl/prebuilt/android) to Android.mk file.

Dang. Thanks, I’ll need my download functionality to work on iOS, Mac, and Windows. So, there’s no way to get the current cocos2d-x Downloader to work with https? Or is the problem with how convoluted the google drive shared files are named?

@slackmoehrle, do you have any suggestions?

Thanks again!

As i said

Or you can edit CCDownloader-curl.cpp on cocos/network and set CURLOPT_SSL_VERIFYPEER to 0.

so you can use Downloader with https.

Oh!! Thanks, I’ll try that later today. Crossing my fingers.

@CAPONE

I’m sorry to be a pest, and I really appreciate your patience and help. I’ve now set CURLOPT_SSL_VERIFYPEER to 0 and tried, and then set CURLOPT_FOLLOWLOCATION to 1 and tried again, and I’m having no luck.

Google Drive baffles me a little, with its aliases. Here is the exact file I want to download:
https://drive.google.com/open?id=1cv7V695hjc_ZAZZfBL-ALeV-d31ht7s4

another is:
https://drive.google.com/file/d/1iRPhcy0U5ZhU89rpKC4j_G_q_XDe428l/view

Here is the pertinent part of my code:
#include “cocos-ext.h”

USING_NS_CC;
USING_NS_CC_EXT;
using namespace std;

void SavedLeaguesScene::tapNewLeague(Ref *sender, Control::EventType event)
{
  string url;
  string localPath = getPsfWritablePath();
  string filePath = localPath + "test_download.zip";

  // this works
  url = "http://cdn.cocos2d-x.org/cocos2d-x-3.15.1.zip";

  // but this doesn't
  url = "https://drive.google.com/open?id=1cv7V695hjc_ZAZZfBL-ALeV-d31ht7s4";

  // here's the actual code
  network::Downloader *downloader = new (std::nothrow) network::Downloader();
  CCLOG("url is %s, instantiated Downloader", url.c_str());

  downloader->onTaskProgress = ([] (const network::DownloadTask &task, int64_t bytesReceived, int64_t totalBytesReceived, int64_t totalBytesExpected) {
      CCLOG("onTaskProgress, downloaded %d of %d", totalBytesReceived, totalBytesExpected);
      // downloading progress
    });

  downloader->onFileTaskSuccess = ([] (const network::DownloadTask &task) {
      CCLOG("File downloaded!  Now do stuff");
    });

  downloader->onTaskError = ([] (const network::DownloadTask &task, int errorCode, int errorCodeInternal, const std::string& errorStr) {
      CCLOG("ERROR downloading, errorCode = %d (%d): %s", errorCode, errorCodeInternal, errorStr.c_str());
      //file downloading error
    });

  downloader->createDownloadFileTask(url, filePath);
}

I hope you can see what I’m doing wrong, because I’m not seeing it. :frowning:

Thanks so very much.

It’s google drive problem. Try search how download files from gdrive with curl or use another host.
For example https://stackoverflow.com/a/25033499 here is discussion about same problem.

Thanks much, I’ll read that site. I wonder if it would just be better to have people host their files on other sites, like dropbox. I probably should just go with Steam Workshop, but I’ve hesitated, because I have iOS users, too.

Thanks again, maybe that stackoverflow site will help me.