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

bulkDownload callback not fired sometimes #46

Open
skyslide22 opened this issue Dec 27, 2019 · 1 comment
Open

bulkDownload callback not fired sometimes #46

skyslide22 opened this issue Dec 27, 2019 · 1 comment

Comments

@skyslide22
Copy link

the bulkDownload downloads all files of my array,
but it is not triggering the callback, the function body is completly ignored sometimes

const dl = app.require("electron-download-manager")

function downloadSiteColorURLFILELinksFiles() {
  const siteColorURLFILELinksFilesFromList = $(
    "#chooseColors ul .isURLfromURLFILE"
  );
  let siteColorURLFILELinksFiles = [];

  siteColorURLFILELinksFilesFromList.each(function() {
    siteColorURLFILELinksFiles.push($(this).html());
  });

  console.log(siteColorURLFILELinksFiles);
  console.log("downloading now all files from list");

  dl.bulkDownload(
    {
      urls: siteColorURLFILELinksFiles,
      path: ``
    },
    function(error, finished, errors) {
      if (error) {
        console.log("finished: " + finished);
        console.log("error: " + error);
        console.log("errors: " + errors);
      }
      console.log("all files downloaded");

      colorFileList.append(
        "<li class='li-seperator'>" + "downloaded files are:" + "</li>"
      );

      const siteColorURLFILElinksFilesDownloaded = fs.readdirSync(
        __dirname + colorSiteDownloadDIR,
        "utf8"
      );
      siteColorURLFILElinksFilesDownloaded.forEach(file => {
        colorFileList.append(
          "<li class='isFILE'>" +
            __dirname +
            colorSiteDownloadDIR +
            file +
            "</li>"
        );
      });
    }
  );
  console.log("after dl.bulkDownload function")
  siteColorURLFILELinksFiles = []
}

the console.log("finished/error etc") is not fired, also the console.log("all files downloaded) is not fired, but all files are downloaded, i see all in the download folder.
electron-download-manager just doesn't know when all files are downloaded sometimes.

well, it actually happes randomly, doesnt matter if the files exist before or not ...
any idea?

// i wanna add all downloaded files to a ul to read them later with the fs module

@StvBB8
Copy link

StvBB8 commented Jan 30, 2020

Can confirm this bug, it seems to happen quite often here.

EDIT: For anyone interested, I've worked around the problem like this. It might contain some errors as I've only been testing it for 15min in my implementation, but it might help you out as well.

    const length = downloadUrls.length;
    let downloaded = 0;

    downloadUrls.forEach(url => {
        DownloadManager.download({ url }, (err, info) => {
            if (err) {
                console.log(err);
                return;
            }

            // When downloaded, increase the download counter and remove the url from the downloadUrls
            downloaded++;
            const index = downloadUrls.findIndex(x => x === url);
            downloadUrls.splice(index, 1);
            
            // Check if every file has been downloaded, this replaces the callback
            if (length === downloaded) {
                // Reset download counter
                downloaded = 0;
            }
        })
    });

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

No branches or pull requests

2 participants