koajs multipart form post,在发送回复之前提取zip

时间:2016-03-21 18:47:59

标签: arrays yield rethinkdb koa co

我使用koajs作为我的界面我有一个带有1到x附加拉链作为文件的表单

我们如何在这些文件上进行循环,以便在koa发送回复之前将其解压缩:/?

这是我的尝试(不太了解所以可能非常糟糕:p)

module.exports.add = function *() {
    var results = {};
    var doc = {};
    var form = yield formidable.parse(this);
    Iterator.iterate(doc.points).forEach(function (point, index) {
        co(function *() {
        var p = {}
        p.km = point.km;
        p.num = point.num;
        var f = {};
        var file = form.files['serie-'+p.num];
        f.oPath = file.path;
        f.name = file.name;
        f.path = storageDir+'checkpoint/'+p.num;
        f.size = file.size;
        f.mime = file.type;
        p.document = f;
          yield extractZip(f)
          console.log('after zip')
          var files = yield analyseFiles(f)
          console.log('after analyse ' + JSON.stringify(files))
          var pt = new Point(p);
          pointsList.push(p);
          console.log("result after index : "+index, p)
        });
      });
      // here i store my doc in rethinkdb... but it s posted directly it do not wait the zip extract anf file analysis :/
}

function* extractZip(file) {
    console.log('in zip')
    yield exec('mkdir -p ' + file.path+' && unzip -o '+file.oPath+' -d '+ file.path);
}

function* analyseFiles(file) {
    console.log('in analyse')
    var files = yield walk(file.path)
    return files;
});

}

目前的结果

 <-- POST /courses/add
{ fields: 
   { idCourseType: '3accf2c1-9424-438b-b970-abf328679fcd',
     nom: 'trek de paris',
     dateCourse: '2016-03-22',
     heureDepartCourse: '08:15',
     'km-serie-1': '2',
     'km-serie-2': '4',
     points: '%5B%7B%22id%22%3A%2220P38-G-5C1022-B91299%22%2C%22num%22%3A1%2C%22km%22%3A2%7D%2C%7B%22id%22%3A%2220O99-D-B99648-B91262%22%2C%22num%22%3A2%2C%22km%22%3A4%7D%5D' },
  files: 
   { 'serie-1': 
      File {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        size: 325558122,
        path: '/tmp/upload_c62a80f7822cade622ffd1bddc8032b8',
        name: 'photos.zip',
        type: 'application/zip',
        hash: null,
        lastModifiedDate: Mon Mar 21 2016 17:01:07 GMT+0100 (CET),
        _writeStream: [Object] },
     'serie-2': 
      File {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        size: 325558122,
        path: '/tmp/upload_1115a5eaf4a213c5307c3ccec9200d49',
        name: 'photos.zip',
        type: 'application/zip',
        hash: null,
        lastModifiedDate: Mon Mar 21 2016 17:01:08 GMT+0100 (CET),
        _writeStream: [Object] } } } [ { id: '20P38-G-5C1022-B91299', num: 1, km: 2 },
  { id: '20O99-D-B99648-B91262', num: 2, km: 4 } ] '0726332b-bf00-4f8d-8bd3-3429ae0e2b13'
in zip
in zip
{ uuid: '0726332b-bf00-4f8d-8bd3-3429ae0e2b13',
  points: model { list: [] },
  nom: 'trek de paris',
  date: '2016-03-22',
  depart: '08:15',
  idCourseType: '3accf2c1-9424-438b-b970-abf328679fcd',
  totalPoints: 2 }
  --> POST /courses/add 200 2,229ms 22.57kb
  <-- GET /api/courses/types/list
  --> GET /api/courses/types/list 200 7ms 209b
after zip
in analyse
after analyse {"list":[]}
result after index : 1 model {
  km: 4,
  num: 2,
  document: 
   { oPath: '/tmp/upload_1115a5eaf4a213c5307c3ccec9200d49',
     name: 'photos.zip',
     path: '/home/srv/sites/tranceandroute/2016/RO/0726332b-bf00-4f8d-8bd3-3429ae0e2b13/checkpoint/2',
     size: 325558122,
     mime: 'application/zip' } }
after zip
in analyse
after analyse {"list":[]}
result after index : 0 model {
  km: 2,
  num: 1,
  document: 
   { oPath: '/tmp/upload_c62a80f7822cade622ffd1bddc8032b8',
     name: 'photos.zip',
     path: '/home/srv/sites/tranceandroute/2016/RO/0726332b-bf00-4f8d-8bd3-3429ae0e2b13/checkpoint/1',
     size: 325558122,
     mime: 'application/zip' } }

你可以看到我有 - &gt; POST / courses /在循环结束前添加200 2,229ms 22.57kb

理想吗?

感谢

1 个答案:

答案 0 :(得分:0)

Nodejs exec需要回调作为第二个参数https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback 但更容易使用库,例如​​https://github.com/EvanOxfeld/node-unzip并使用promise https://www.promisejs.org/generators/

包装在promise示例生成器中