aheckmann / gm

GraphicsMagick for node
http://aheckmann.github.com/gm/
6.95k stars 615 forks source link

Randomly occurring buffer output when streaming a file. #301

Open pruhstal opened 10 years ago

pruhstal commented 10 years ago

My files are getting uploaded, they are being read properly from the appropriate path, and even getting properly resized and pushed to S3.

However, sometimes (non-deterministically), they don't. When they are being read with stream() the buffer is quite literally empty. Has anyone else run into this problem?

Is it an issue with the previous stream still being open and not closed? Here's my code:

resizeAndUpload: function(srcPath, newPath, width, height) {
    var s3bucket = new AWS.S3({params: {Bucket: 'beernuts'}}),
        deferred = Q.defer();

    gm(srcPath)
      .resize(width, height, '^')
      .gravity('Center')
      .extent(width, height)
      .quality(100)
      .stream(function (err, stdout, stderr) {
        var chunks = [];

        stdout.on('data', function(chunk) {
          chunks.push(chunk);
        });

        stdout.on('end', function() {
          var image = Buffer.concat(chunks);
          var params = {
            Key: newPath,
            Body: image,
            ContentLength: image.length,
            ACL: 'public-read'
          };

          s3bucket.putObject(params, function (err, res) {
            if (err) deferred.reject(err);
            else console.log('uploaded successfully');
            deferred.resolve(res);
          });
        });

        stdout.on('err', function (err) {
          console.log(err);
          deferred.reject(err);
        });
      });

    return deferred.promise;
  }

Also happens when using toBuffer(), FWIW.

ekirkland commented 10 years ago

I'm seeing very similar behaviour to this. Any suggestions for how to debug? The same file will work in one case, but not in the other.

pruhstal commented 10 years ago

@evster I ended up switching to spawning a child process and resizing with Python. I couldn't handle the instability of this package.

thomasmery commented 8 years ago

Hi there,

chiming in because I'm running into something similar where, using toBuffer, I would get the following error : toBuffer Stream yields empty buffer

I could not yet determine if this was random or not

as some operations with a given set of images seem to always succeed when others won't

hoping someone will have a direction to a solution as I'm heavily relying on this package right now

thanks