hgouveia / node-downloader-helper

A simple http file downloader for node.js
MIT License
247 stars 54 forks source link

Download has incomplete status if 'content-length' is not present #60

Closed Kuzzy closed 2 years ago

Kuzzy commented 3 years ago

If for some reasons target server will not return 'content-length' header for downloaded file, download info object will always have incomplete: true property, while file will be downloaded and written to filesystem. (https://github.com/hgouveia/node-downloader-helper/blob/master/src/index.js#L514)

How to reproduce:

let result
const url = 'https://www.learningcontainer.com/wp-content/uploads/2020/07/Sample-JPEG-Image-File-Download-scaled.jpg'
const dl = new DownloaderHelper(url, __dirname)

dl.on('end', (downloadInfo) => {
  result = downloadInfo
})

await dl.start()

console.log(result)

// Output: 
// {
//  fileName: 'Sample-JPEG-Image-File-Download-scaled.jpg',
//  filePath: '/my/dir/path/Sample-JPEG-Image-File-Download-scaled (2).jpg',
//  totalSize: NaN,
//  incomplete: true,
//  onDiskSize: 221472,
//  downloadedSize: 221472
// }

I think, that this behavior is wrong. 'content-length' header check should be optional because not every server will respond with this header.

hgouveia commented 3 years ago

@Kuzzy agree, i will check this , i need to think a way of how to handle this scenarios properly, on top of my head i can think of set totalSize as -1 value, when failed to fetch the content-length , and set incomplete to false, since this can't be determined

Chaphasilor commented 2 years ago

How about setting totalSize to null? The total size is "undefined" in this case, and null is the explicit version of undefined :)

hgouveia commented 2 years ago

Released on 2.0.1