1

我将 koajs 用于我的界面 我有一个表格,其中包含 1 到 x 附加的 zip 作为文件

我们如何在 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' } }

如您所见,在循环完成之前我有 -> POST /courses/add 200 2,229ms 22.57kb

一个想法?

谢谢

4

1 回答 1

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 包装在 promise 示例生成器中https://www.promisejs.org/generators/

于 2017-07-10T14:36:26.593 回答