2016-03-21 16 views
1

i i mit 1 bis x beigefügten ZIP als Dateien ein Formular für meine Schnittstelle verwenden koajs senden habenkoajs mehrteiliger Form Post, zu extrahieren zip vor Antwort

, wie können wir tun, eine Schleife auf diese Dateien, um sie vor koa extrahiert werden Sende die Antwort: /?

hier ist, was ich versucht (don t sehr gut verstehen, so könnte sehr schlecht sein: 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; 
}); 

}

als Ergebnis für den Moment i

<-- 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' } } 

haben als Sie können sehen, ich habe die -> POST/Kurse/hinzufügen 200 2,229ms 22,57kb vor Schleife ist fertig

ein i sehr geehrter?

dank

Antwort