asfernandes / node-firebird-drivers

Node.js Firebird Drivers
MIT License
53 stars 17 forks source link

Fetching BLOB (seg type 0) produces incomplete files (Only first 65535 are loaded) #36

Closed mreis1 closed 4 years ago

mreis1 commented 4 years ago

Here's the sample code with annotations

` const blob = results[2][0] as Blob; // @todo Prevent segfault from occurring if blob column is not valid. // Example: const blob = results[0] as Blob; should be const blob = results[0][0] as Blob; // +when invalid, segfault is crashes the app const blobStream = await attachment.openBlob(transaction, blob); let len = await blobStream.length; const buffer = Buffer.alloc(len);

        // ↱ Outputs the real byte length
        console.log(`This file is ${len} byte length`); 

        // ↱ It will only read the 65535 bytes
        await blobStream.read(buffer);

        let file = os.tmpdir() + '/' + Date.now() + '.jpg';
        console.log('Writing file to', file);
        fs.writeFileSync(file, buffer, {});
        await blobStream.close();
        await resultsSet.close();
        await transaction.commitRetaining();

`

asfernandes commented 4 years ago

This is by design and it's documented [1]. Read does not guarantee to fully read in one call. You can continue calling read.

This works like FB API and like file systems API. Read returns the number of bytes read and you should continue calling it.

[1]

* Reads data from the blob and return the number of bytes read or -1 for end-of-stream.
* The number of bytes read may be less than the buffer' size while more data to be read exists.