Node JS https 检查下载的图像是否 100% 完成

Node JS https check if downloaded image is 100% complete

我的应用使用 NodeJS https 模块实现了图片下载功能。 下载没问题,但有时(无明确原因)下载的图像不完整/包含灰色区域

const fs = require('fs'), https = require('https');
// ...
        return new Promise(async (res) => {
            // ...
            // assume var url = 'https://example.com/image.jpg'
            let file = fs.createWriteStream('./image.jpg');
            await https.get(url, async (response) => {
                response.pipe(file).on("finish", () => res(true))
            }).on("error", err => res(err.stack))
        })

正如您在上面看到的(以及根据图像的描述),失败的图像没有抛出任何错误( .on('err'... failed to catch anything)

在调用 Promise 的 resolve() 之前,如何检查/验证下载的图像是否 100% 完整?

我想我找到了方法。 所以对于任何和我有同样问题的人,我想在这里分享我的答案

我不得不切换到 node-fetch,所以如果您正在寻找 http 解决方案,那么很抱歉,但我不知道如何。

const fs = require('fs'), fetch= require('https');
var w_size: number = 0 // file size from URL

await (async (url: string) => {
    return new Promise<Buffer>(resolve =>
        resolve(fetch(url)
            .then((r) => { /* get file size from header */ w_size = parseInt(r.headers.get('content-length') || '0'); return r })
            .then(res => res.arrayBuffer())
            .then(buff => Buffer.from(buff)))
    )
})(url).then(async (buffer) => {
    console.log('Writing');
    await fs.writeFileSync(downloadedFilePath, buffer);
    if (w_size) { // if w_size == 0 (means failed to get ['content-length'] from headers), jump to else
        var getPercent = fs.statSync(downloadedFilePath).size;
        if (Math.round((getPercent / w_size) * 100) < 99) { // minimum of 99 percent complete, or if you want to be strict, use <= 99 to make sure it's 100%
            // Partial download (download failed)
            console.log(`Error:\nBuffer end before whole file finished downloading (partial download)`)
            throw new Error('Partial download detected')
        }
        else {...} // Download success
    }
    else {...} // default behavior if failed to get w_size from URL (means original / meant-to-be size is unknown)
})