ibrod83 / nodejs-file-downloader

129 stars 23 forks source link

skipExistingFileName not working #25

Closed Blogshot closed 2 years ago

Blogshot commented 2 years ago

Using:

      const videoDownload = new Downloader({
        url: 'https:' + (metadata.sources?.[0].src || metadata.video),
        directory: `${dir}/Videos/${metadata.season == 0 ? 'Specials' : `Season ${metadata.season}`}/`,
        filename: `${filename}.mp4`,
        skipExistingFileName: true,
        onProgress: (percent) => {
          videoProgress.value = parseFloat(percent)
          videoProgress.text = `${percent}%`
        }
      })

Execution above code two times in a row causes the file to be downloaded a second time. I can observer how the creation date of the file always jumps to zero.

The exists() function in ./utils/fileName.jsseems to work fine and returns true after the file is downloaded once. I believe it might be something in https://github.com/ibrod83/nodejs-file-downloader/blob/834ea1ca41904683f790dba9eefad2792c62367f/Download.js#L148 that may be buggy.

I'm implementing a workaround in my code for now, just wanted to make you aware that something is not working right.

ibrod83 commented 2 years ago

I'll look into it

ibrod83 commented 2 years ago

Hey, can you explain what you mean by "Two times in a row"? Is it sequential, or does it happen in parallel?(within a foreach loop, for example). Can you show the code that that calls videoDownload.download()? Also, are you creating a new Downloader object for each download?(you should)

Blogshot commented 2 years ago

I'm downloading a queue sequentially. Each item gets it's own instance of a Downloader object.

The code looks mostly like this:

for (var i = 0; i < queue.length; i++) {
  const item = queue[i]

  var fileType = '.mp4'
  var fileName = item.name

  const videoProgress = new ProgressBar({
      indeterminate: false,
      text: '0%',
      detail: 'Working',
      maxValue: 100
  })
  const videoDownload = new Downloader({
      url: 'https://' + (item.source),
      directory: `${dir}`,
      filename: `${filename}${fileType}`,
      skipExistingFileName: true,
      onProgress: (percent) => {
        videoProgress.value = parseFloat(percent)
        videoProgress.text = `${percent}%`
      }
  })

  try {
    await videoDownload.download()
  } catch (err) {
    console.log(err)
    alert(err)
  }
}

The target directory is empty in the first run and everything downloads as expected. WHen I finish the execution and run the program a second time, the files are replaced, as can be seen by the "Date of creation" in windows explorer.

To me, this shows that the files are a. not recognized correctly and therefor overwritten b. recognized as existing but incorrectly handled at some point

I've stringified the Downloader object, this is my instance. Executing the code a second time after the first run completed results in the usual "_2"-File alongside the first one.

{
  "config": {
    "directory": "C:\\Users\\user\\Desktop\\test/Videos/Specials/",
    "fileName": "This Will End in One Year (Mark).mp4",
    "timeout": 6000,
    "maxAttempts": 1,
    "useSynchronousMode": false,
    "cloneFiles": true,
    "shouldBufferResponse": false,
    "url": "https://cdn.unusann.us/00/001/1080.mp4",
    "filename": "This Will End in One Year (Mark).mp4",
    "skipExistingFileName": true
  },
  "percentage": 0,
  "fileSize": null,
  "currentDataSize": 0
}

Directory list:

drwxrwxrwx 1 4.0K Dec  9 23:37  .
drwxrwxrwx 1 4.0K Dec  9 23:35  ..
-rwxrwxrwx 1  12M Dec  9 23:37 'This Will End in One Year (Mark).mp4'
-rwxrwxrwx 1  12M Dec  9 23:37 'This Will End in One Year (Mark)_2.mp4'
ibrod83 commented 2 years ago

So, i still didn't manage to reproduce the problem. I tried doing it within a for loop like you. Tried to play with it- worked well each time. Question: Are you using the latest version? Because there was already some problem with skipExistingFileName few months back, which was fixed. Also, what Node version and what OS are you using?

Blogshot commented 2 years ago

Aaah! I didn't do one of the most basic checks before posting: I'm using an old version.

I checked out a project on github and they statically set the version number to 4.4.2 while 4.8.1 is the latest version.

After updating, it works like expected. Sorry to have wasted your time like this. 😢