首页 > 解决方案 > 如何使用gridfs nodeJS调整图像大小并存储不同的sizet?

问题描述

我是 mongoDB 和 gridfs 的初学者。我有一个在数据库中保存传入图像的代码。但我想在保存之前调整图像大小。它试图使用sharp,但我真的不明白它是如何工作的。

我尝试了以下代码,但它不起作用

const storage = new GridFsStorage({
  url: mongoURI,

  file: (req, file) => {
    return new Promise((resolve, reject) => {
      const filename = req.params.userID;
      const fileInfo = {
        filename: filename,
        bucketName: "ProfilePic", 
      };
      resolve(fileInfo);
    });
  }
});

const upload = multer({ storage });

router.post("/setProfilePic/:userID", upload.single("picture"), async(req, res) => {
//code not working
  await sharp(req.file.path)
    .resize(500)
    .jpeg({quality: 50})
    .toFile(
        path.resolve(req.file.destination,'resized',image)
    )
// end of non working code
  return res.status(200).json({state: "ok"})
});

标签: node.jsmongoosesharpmulter-gridfs-storage

解决方案


推荐阅读