1

I'm working on functionality for my project to upload an inputted image to aws, watermark it using jimp, and then upload the watermarked image to aws. All of the functionality works, however, only the pre-watermarked image is getting uploaded twice while the watermarked image seems to be updated after the upload to aws. I believe it has to do with async and have used promises but since I'm new to js I'm not quite sure if I'm missing something obvious. I've attached my three functions below.

 const uploadFile = async (fileName) => {
  //Read content from file
  const fileContent = fs.readFileSync(fileName);

  //setting up S3 upload parameters
  const params = {
    Bucket: BUCKET_NAME,
    Key: 'original_test.jpg', //filename to save as in S3
    Body: fileContent,
  };

  //uploading image to the bucket
  s3.upload(params)
    .promise()
    .then((data) => {
      console.log('Success');
      watermarker(fileName);
    })
    .catch(function(err) {
      console.log(err);
    });
};



 const watermarker = async (uploadedImage) => {
  const promises = function jimpify() {
    return Jimp.read(uploadedImage)
      .then(function(image) {
        loadedImage = image;
        return Jimp.loadFont(Jimp.FONT_SANS_128_BLACK);
      })
      .then(function(font) {
        loadedImage.print(font, 10, 10, imageCaption).write(uploadedImage);
      })
      .catch(function(err) {
        console.error(err);
      });
  Promise.all(promises).then(() => watermarkImageUploader(uploadedImage));
};



 const watermarkImageUploader = async (watermarkedImage) => {
  //Read content from watermark image
  const watermarkFileContent = fs.readFileSync(watermarkedImage);

  //watermark image S3 upload parameter
  const watermarkParams = {
    Bucket: BUCKET_NAME,
    Key: 'watermark_test.jpg', //filename to save as in S3
    Body: watermarkFileContent,
  };

  //uploading watermark image to the bucket
  s3.upload(watermarkParams)
    .promise()
    .then(function(data) {
      console.log('Success 2');
    })
    .catch(function(err) {
      console.log(err);
    });
};

0 Answers0