在上载文件或在网络上写入文件之前查找流的内容长度

问题描述 投票:0回答:1

我正在读取文件,压缩和加密它,然后在网络上上传/写入。但我需要知道结束流的内容长度(通过读取,zip,加密后返回的流)来发布请求。

let zlib = zlib.createGzip(),
   encrypt = crypto.cipherIV(....),
    input = fs.createReadStream('file.jpg');
function zipAndEncrypt(){
   let stream = readStream.pipe( zlib).pipe( encrypt );
   let options = {
     "stream_length":0,
     headers: { "content-type": 'image/jpeg',
                "content-length": '123456', // need to get this length 
          .....
     }
    }

// post the stream
needle( 'post', url, stream, options )
   .then( resp => { console.log( "file length", resp.body.length);})
   .catch( err => {})
}

如果我在标题中输入正确的内容长度(在这种情况下我知道长度),上面的代码可以工作。所以我需要找到流的长度。

到目前为止我通过以下方式实现了:

  let chunk = [], conLength;
  stream.on( 'data', ( data ) => {
            chunk.push( data );
        } )
        .on( 'end', () => {
         conLength = Buffer.concat( chunk ).length; 
        } );

但是post请求失败,SOCKET挂起错误。

看起来流被耗尽或消耗,因为在使用上面的代码找到长度后它不会发出'data'事件。

尝试了stream.resume()。但没有任何作用。你能否建议如何在不消耗流的情况下找到流的长度。

javascript node.js stream pipeline needle.js
1个回答
0
投票

如果您需要发送内容长度,唯一的方法是知道它,是在文件被压缩和加密后。

因此,您的解决方案可行,但仅当您发送缓冲区而不是流时,因为您已经消耗了流中的所有数据。既然你已经拥有了内存中的所有块,你也可以发送它。

let chunk = [];

stream.on('data', data => chunk.push(data))
.on('end', () => {
    const buffer = Buffer.concat(chunk);
    const conLength = buffer.length;
    // Execute the request here, sending the whole buffer, not the stream
    needle(/*...*/)
});

但是如果您的文件太大,则需要对其进行流式传输,否则您将达不到内存,一个简单的解决方法,只需要一点开销,就是将其传输到临时文件,然后发送该文件。这样,您可以在执行请求,访问stream.bytesWritten属性或使用fs.lstat之前知道文件大小。

function zipAndEncrypt(input) {
    const gzip = zlib.createGzip();
    const encrypt = crypto.createCipheriv(algo, key, iv),

    const stream = input.pipe(gzip).pipe(encrypt);


    const fileName = tmpFileName();
    const file = fs.createWriteStream(fileName)
    stream
        .pipe(file)
        .on('finish', () => {
            let options = {
                "stream_length": 0,
                headers: {
                    "content-type": 'image/jpeg',
                    "content-length": file.bytesWritten
                }
            }

            const readStream = fs.createReadStream(fileName);

            // post the stream
            needle('post', url, readStream, options)
                .then(resp => {
                    console.log("file length", resp.body.length);
                })
                .catch(err => {})
                .finally(() => {
                    // Remove the file from disk
                });
        })

}
© www.soinside.com 2019 - 2024. All rights reserved.