const _ = require("lodash");
const config = require("config");
const AWS = require("aws-sdk");
const { v1: uuidv1 } = require("uuid");

const s3 = new AWS.S3({
  accessKeyId: config.get("AWSaccessKeyId"),
  secretAccessKey: config.get("AWSsecretAccessKey"),
  region: config.get("AWSregion"),
  signatureVersion: "v4",
});

module.exports.getImageUploadUrl = async (req, res) => {
  console.log("Req Body Data: ", req.body);
  const key = `istar/${uuidv1()}${req.body.fileName}`;
  console.log("Key: ", key);
  s3.getSignedUrl(
    "putObject",
    {
      Bucket: config.get("AWSBucket"),
      ContentType: req.body.type,
      Key: key,
    },
    (err, url) => {
      if (err) console.log("ERR: ", err);
      res.send({ key, url, ok: "OK" });
    }
  );
};
