0

I have a problem but I don't know how to solve it. I reading a file from s3, then I save it to a folder(downloads). After that, I read the file that I saved in the folder (that I saved in downloads) and re-size it + save it in a different folder with a different name (resized). Then, I want to read again this file in order to upload it again in its the new size but when I try to do that it throws an error that there is no such file in this directory but when I check it in my folder it dose. My guess it's trying to read before the image is written in the new folder (resized) How can I fix it?

update

so accroding to commits this is what I did but it disen't print out anything in fs.readFileAsync. Why?

var image = new ImageResize(__dirname +'/../downloads/'+ hash + '.png');
  image.smartResizeDown({
    width: 200,
    height: 200
  }).then(function () {
    image.stream(function (err, stdout, stderr) {
        var writeStream = fs.createWriteStream(__dirname +'/../resized/'+ hash + 'resize.png');
        stdout.pipe(writeStream);

}).then(function(){
  fs.readFileAsync('C:/Users/user/Desktop/cloud/new/resized/'+hash+'resize.png', function(error, buf){

    if(error)
      console.log(error);
    else
      console.log('yay');
});
});

});
user3488862
  • 1,329
  • 2
  • 12
  • 16

1 Answers1

0

var image = new ImageResize(__dirname +'/../downloads/'+ hash + '.png');

image.smartResizeDown({
    width: 200,
    height: 200
})
.then(function () {
    return new Promise(function(resolve, reject) {
        image.stream(function (err, stdout, stderr) {
            var writeStream = fs.createWriteStream(__dirname +'/../resized/'+ hash + 'resize.png');
            stdout.pipe(writeStream);

            resolve();
        })
    });
})
.then(function(){
    fs.readFileAsync('C:/Users/user/Desktop/cloud/new/resized/'+hash+'resize.png', function(error, buf) {
        if(error)
          console.log(error);
        else
          console.log('yay');
    });
    return null;
});

Assuming bluebird object is in Promise named variable.

Jitendra Khatri
  • 774
  • 3
  • 16