检查busboy完成事件是否已经发生

时间:2014-07-08 12:57:40

标签: javascript node.js express

我有一个表单,我需要做一些文件处理需要一些时间,所以我希望只有在处理完成后才会执行完成事件,现在 节点正在处理文件,当它正在处理文件并执行命令节点时如果找到完成事件则触发它。那么,我如何确保只在处理完所有文件后才触发完成事件。

busboy.on('file', function(fieldname, file, filename,transferEncoding,mimeType) {
        var fName = uuid.v4();
        var fileext = filename.substr(filename.lastIndexOf('.') + 1);
        var filepath = path.normalize(__dirname + '/../../');
        var fstream = fs.createWriteStream(filepath+'/server/uploads/'+fName+'.'+fileext);
        var uploadFileCompletion = file.pipe(fstream);
        uploadFileCompletion.on('finish',function(){
            console.log('uploaded now');
            var cmd = 'libreoffice --headless --convert-to pdf --outdir '+ filepath + 'server/uploads ' + filepath + 'server/uploads/' + fName + '.' + fileext;
            exec(cmd, function(error,stdout,stderr){
                sys.puts(stdout);
                var encryptCmd = 'java -jar server/uploads/pdfbox-app-1.8.6.jar Encrypt -canAssemble false -canExtractContent false -canExtractForAccessibility false ' +
                    '-canModify false -canModifyAnnotations false -canPrint false -canPrintDegraded false server/uploads/' + fName + '.' + 'pdf'
                    + ' ' + 'server/uploads/' +fName + '.' + 'pdf';
                exec(encryptCmd, function(error,stdout,stderr){
                    fs.unlink(filepath+'server/uploads/'+fName + '.' + fileext, function(){
                        console.log("removed " +filepath+'server/uploads/'+fName + '.' + fileext);
                        actualFileName.push(filename);
                        storedFileName.push(fName+'.'+'pdf');
                    });
                });

            });
        });
    });
    busboy.on('field', function(fieldname, val, valTruncated,keyTruncated) {
        noteData = JSON.parse(val);
    });
    busboy.on('finish',function(){
        noteData.uploader = req.user.username;
        noteData.actualFileName = actualFileName;
        noteData.storedFileName = storedFileName;
        noteData.noteId = uuid.v4();
        Campusnotes.create(noteData,function(err,note){
            if(err){
                res.status(400);
                return res.send({reason:err.toString()});
            }
            console.log('finish');
            res.status(200);
            res.end();
        })
    });

现在控制台日志如下 -

finish
uploaded now
convert /home/unknown/public_html/campustop/server/uploads/8465f9a9-d6b7-4d53-8cb5-a8dbf3aed6a5.odt -> /home/unknown/public_html/campustop/server/uploads/8465f9a9-d6b7-4d53-8cb5-a8dbf3aed6a5.pdf using writer_pdf_Export

removed /home/unknown/public_html/campustop/server/uploads/8465f9a9-d6b7-4d53-8cb5-a8dbf3aed6a5.odt

表示结束事件一次又一次被触发

1 个答案:

答案 0 :(得分:2)

您可以尝试以下方式:

var files = 0;
busboy.on('file', function(fieldname, file, filename,transferEncoding,mimeType) {
  ++files;
  var fName = uuid.v4();
  var fileext = filename.substr(filename.lastIndexOf('.') + 1);
  var filepath = path.normalize(__dirname + '/../../');
  var fstream = fs.createWriteStream(filepath+'/server/uploads/'+fName+'.'+fileext);
  file.pipe(fstream).on('finish',function() {
    console.log('uploaded now');
    var cmd = 'libreoffice --headless --convert-to pdf --outdir '+ filepath + 'server/uploads ' + filepath + 'server/uploads/' + fName + '.' + fileext;
    exec(cmd, function(error,stdout,stderr) {
      console.log(stdout);
      var encryptCmd = 'java -jar server/uploads/pdfbox-app-1.8.6.jar Encrypt -canAssemble false -canExtractContent false -canExtractForAccessibility false ' +
          '-canModify false -canModifyAnnotations false -canPrint false -canPrintDegraded false server/uploads/' + fName + '.' + 'pdf'
          + ' ' + 'server/uploads/' +fName + '.' + 'pdf';
      exec(encryptCmd, function(error,stdout,stderr) {
        fs.unlink(filepath+'server/uploads/'+fName + '.' + fileext, function() {
          console.log("removed " +filepath+'server/uploads/'+fName + '.' + fileext);
          actualFileName.push(filename);
          storedFileName.push(fName+'.'+'pdf');
        });
      });
      --files;
      onFinish();
    });
  });
});
busboy.on('field', function(fieldname, val, valTruncated,keyTruncated) {
  noteData = JSON.parse(val);
});
busboy.on('finish', onFinish);

function onFinish() {
  if (!busboy.writable && files === 0) {
    noteData.uploader = req.user.username;
    noteData.actualFileName = actualFileName;
    noteData.storedFileName = storedFileName;
    noteData.noteId = uuid.v4();
    Campusnotes.create(noteData,function(err,note){
      if (err){
        res.status(400);
        return res.send({reason:err.toString()});
      }
      console.log('finish');
      res.status(200);
      res.end();
    });
  }
}

在一个不相关的说明中,你应该对文件名进行一些清理/检查,有人可能是恶意的并使用类似'../../../../../../../../../etc/passwd'的东西(我不确定createWriteStream()是否解析/规范化给定的路径是不是。)