danielnieto / electron-download-manager

Manage downloadItems from Electron's BrowserWindows without user interaction, allowing single file download and bulk downloading
MIT License
120 stars 50 forks source link

bulkDownload callback not fired sometimes #46

Open skyslide22 opened 4 years ago

skyslide22 commented 4 years ago

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 commented 4 years ago

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;
            }
        })
    });