显示图像时如何调整使用GridFS存储的图像的大小

时间:2020-11-09 12:15:07

标签: mongodb express gridfs gridfs-stream multer-gridfs-storage

这些是

下路线的变量
const GridFsStorage = require("multer-gridfs-storage");
const Grid = require("gridfs-stream");
var conn = mongoose.createConnection(db, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});
let gfs;
conn.once("open", () => {
  gfs = Grid(conn.db, mongoose.mongo);
  gfs.collection("uploads");
});

这是我目前正在读取文件的方式

router.get("/image/:filename", (req, res) => {
  gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
    if (!file || file.length === 0) {
      return res.status(404).json({
        err: "No file exist",
      });
    }
    //check if it's a jpeg
    if (
      file.contentType === "image/jpeg" ||
      file.contentType === "img/png" ||
      file.contentType === "image/png"
    ) {
      //read output to browser
      const readstream = gfs.createReadStream(file.filename);
      readstream.pipe(res);
    } else {
      res.status(404).json({
        err: "Not an image",
      });
    }
  });
});

如果可以的话,使用任何外部模块在上述路线中显示图像时如何调整图像大小,或者也可以接受其他解决方案

0 个答案:

没有答案