0

looking for some newb NodeJS help. There HAS to be something simple I'm overlooking, but I've tried to go about this several ways and continuously end up with odd results.

I took the majority of the following code from this question.

// 128Kb Chunks
var targetSize = 131072;

// Open the file
fs.open('/project/input.png', 'r', function(err, fd) {

    // Gather some info about the file
    fs.fstat(fd, function(err, stats) {
        var bufferSize=stats.size,
            chunkSize=targetSize,
            buffer=new Buffer(bufferSize),
            bytesRead = 0,
            isFinal = false;

        while (bytesRead < bufferSize) {

            if ((bytesRead + chunkSize) > bufferSize) {
                chunkSize = (bufferSize - bytesRead);
                isFinal = true;
            } else {
                isFinal = false;
            }

            fs.read(fd, buffer, bytesRead, chunkSize, bytesRead);

            bytesRead += chunkSize;

            if( isFinal ) {
                fs.writeFileSync("/project/output.png", buffer);
            }

        }

    });

    // Done
    fs.close(fd);

});

When I output the output.png file, which has an identical file size as my input (so something must be sorta working?), my image viewer cannot read the file, so it is corrupted in some way.

I tried to keep this question and example as simple as possible, but in case it matters or prompts further advice, here are my core goals:

  • Read a file in fixed chunk sizes (128KB)
  • Encrypt and store each chunk in mongodb (intentionally excluded here)
  • Be able to reassemble the image later on

I appreciate any help you can give.


Update: Found the answer, and it was simple as expected.

The code I copied had a small bug, namely the fs.close()

The stream was being closed before it was being read.

This was also visible when I added a callback to my fs.read, which has a 'err' first param:

[Error: EBADF, read] errno: 9, code: 'EBADF'
Community
  • 1
  • 1

1 Answers1

0

Try changing the line

while (bytesRead < bufferSize)

to

while (bytesRead < bufferSize && !isFinal)

I don't know if that will fix it, but it's a start.

Also for efficiency's sake you don't need the section:

} else {
    isFinal = false;
}

As isFinal will be false anyway (you'd never be changing it in this line).

warchinal
  • 229
  • 2
  • 14