1

我正在尝试将文件作为表单中的输入,然后将其存储在临时文件夹中,然后对文件运行一些加密算法并将加密文件保存在临时文件夹本身中。之后,我想将该 encryptrd 文件从 temp 文件夹上传到 gridfs。加密文件存储在 tmp 文件夹中,但未上传到 mongoDB。请帮我弄清楚为什么?下面是我的代码的一部分。

var storagedisk =   multer.diskStorage({  
  destination: function (req, file, callback) {  
    callback(null, './tmp');  
  },  
  filename: function (req, file, callback) {  
    callback(null, file.originalname);  
  }  
});  
var uploaddisk = multer({ storage : storagedisk}).single('file');
let encryptpath="";
app.post('/uploaddisk',function(req,res){
  uploaddisk(req,res,function(err) {
      if(err) {
          return res.end("Error uploading file.");  
      }
      res.end("File is uploaded successfully!");  
      const path=`tmp/${res.req.file.originalname}`;
      let f = new encrypt.FileEncrypt(path);
      f.openSourceFile();
      f.encrypt('111111');
      const efile=f;
      encryptPath = f.encryptFilePath;
      fs.unlinkSync(path);
      uploadtogfs(efile);
  });
});



// Create storage engine
const storage = new GridFsStorage({
  url: dbsURI,
  file: (req, file) => {
    return new Promise((resolve, reject) => {
      crypto.randomBytes(16, (err, buf) => {
        if (err) {
          return reject(err);
        }
        const filename = file.originalname;
        const uploader = currUser.username;
        const fileInfo = {
          metadata:{
            uploader:uploader,
            ofn: file.originalname
          },
          filename: filename,
          bucketName: 'uploads'
        };
        resolve(fileInfo);
      });
    });
  }
});
const upload = multer({ storage:storage }); 

function uploadtogfs(file){
  console.log(file);
  upload.single(file);
}
4

0 回答 0