2017-04-21 2 views
0

다른 입력 이름의 이미지 파일이 여러 개있는 제출 된 양식의 파일을 업로드하려고합니다.skippper (sails.js)와 다른 알 수없는 입력 필드 이름이있는 미들웨어 파일 처리

텍스트 입력 및 파일 입력과 다중 양식을 asumming 예를 들어

, 및 그룹 같은 이름을 가진 모든 파일 입력에의 실용적이지, 예를 들어 images[] 내가 표현을 만들

<form ...> 
some text inputs... 
<input type='file' name='logo'/> 
<input type='file' name='image-1'/> 
<input type='file' name='image-2'/> 
</form> 

/돛 미들웨어 업로드를 처리하기 위해 컨트롤러에 직접 액세스 할 수 있습니다.

난 (req.file에 그 업로드 방법 (가정) promisified한다)이 오류가 발생합니다

async function(req, res, next){ 
     const maxTimeToBuffer = 9500 
     const dirname = '/some-upload-path' 
     const uploads = [ 
     await req.file('logo').upload(dirname, maxTimeToBuffer) 
     await req.file('image-1').upload(dirname, maxTimeToBuffer) 
     await req.file('image-2').upload(dirname, maxTimeToBuffer) 
     ] 
     // attaching the uploads to the request body ... 
     next() 
    } 

이 작업을 수행 할 경우 EMAXBUFFER: An Upstream ( 이미지-1 ) timed out before it was plugged into a receiver. It was still unused after waiting 9500ms. You can configure this timeout by changing the maxTimeToBuffer option.

그것은 '아무튼를 Skipper 모듈 자체에서도 maxTimeToBuffer을 30000으로 늘리면 문제가 없습니다.

순차적 방식으로 각 파일을 처리하려고하면 입력 필드 이름을 알고있는 한 아무 문제없이 병렬 처리로 오류를 던집니다.

async function(req, res, next){ 
     const maxTimeToBuffer = 9500 
     const dirname = '/some-upload-path' 
     const uploads = await [ 
     req.file('logo').upload(dirname, maxTimeToBuffer) 
     req.file('image-1').upload(dirname, maxTimeToBuffer) 
     req.file('image-2').upload(dirname, maxTimeToBuffer) 
     ] 
     // attaching the uploads to the request body ... 
     next() 
    } 

이제 입력란을 미리 알지 못해도 마지막 스 니펫에서 어떻게 동일한 결과를 얻을 수 있습니까? 이

async function(req, res, next){ 
    const maxTimeToBuffer = 9500 
    const dirname = '/some-upload-path' 
    const uploads = await req._fileparser.upstreams 
    // attaching the uploads to the request body ... 
    next() 
} 

것은 같은 뭔가 req._fileparser.upstreams이 새로운 스트림을 수신하고 첫 번째 이미지가 완전히 수신 된 후 그 만 일어날 것 (연속)하지만 난이 업스트림을 기다리는 경우 배열이 완료 될 때마다 업데이트되는 것입니다 시간 초과 오류도 발생합니다.

해결 방법이 있습니까?

나는 더 많은 설명이 필요한 경우 알려주는 것이 많다는 것을 안다. 어떤 생각이라도 반갑습니다.

답변

0

좋아, 벽에 머리를 두드리는 소리가 많이 난 후에 나는 이것을 생각해 냈습니다. 우리는 스트림을 기다리지 않고 실행하기 시작합니다. 새 스트림을 추가 할 때마다 upload를 호출하여 실행합니다. 파서/폼이 skipper에 의해 닫힌 것으로 설정된 경우 간격을 100ms마다 확인합니다. 모든 스트림을 안전하게 기다렸다가 마지막으로 약속을 해결하거나 타임 아웃이 발생하면이를 거부합니다. 내 코드가 단순화 되었기 때문에 다른 구문 분석을 많이하고 있기 때문에 다른 사람에게 도움이되기를 바랍니다.

async function(req, res, next){ 
    const maxTimeToBuffer = 9500 
    const dirname = '/some-upload-path' 
    const uploads = await new Promise((resolve, reject)=> { 
     const TIMEOUT = 20000 
     const {upstreams} = req._fileparser 
     const tasks = _.map(upstreams, (stream)=> stream.upload(dirname, maxTimeToBuffer)) 
     // We proxy the original push method, to push to our task list every time a new stream is added 
     upstreams.push = (stream)=> { 
     tasks.push(stream.upload(dirname, maxTimeToBuffer)) 
     Array.prototype.push.call(upstreams, stream) 
     } 
     const startTime = (new Date()).getTime() 
     const intervalId = setInterval(async function(){ 
      if(req._fileparser.closed){ 
       clearInterval(intervalId) 
       fileObjects = await tasks 
       resolve(fileObjects) 
      } 
      else if((new Date()).getTime() - startTime > TIMEOUT){ 
       clearInterval(intervalId) 
       reject(new Error('Timeout triggered before form closed')) 
       , 100)  
      } 
    }) 
    // attaching the uploads to the request body ... 
    next() 
} 

나는 선장이처럼 행동하고, 해킹의 종류 그래서 아마 나는 경우, 알려주세요, 뭔가가 미안 강요하지한다고 생각합니다.