2016-05-17 76 views
0

當文件是JPG時,它會在返回響應之前創建(以在瀏覽器中顯示調整大小的圖片)。但是,當它返回它已寫入導致的Node.js服務器的崩潰PNG,因爲它不能創造的東西ReadStream不存在之前PNG:Promise調用得太早

調整器呼叫

else { 
    resizer 
     .resizeHandler(filepath, parsedUrl, fullDestinationPath) 
     .then(function() { 
      return self.send(response, 200, {'Content-Type': mime.lookup(fullDestinationPath)}, fs.createReadStream(fullDestinationPath)); 
     }); 
} 

調整大小

Resizer.prototype.resizeThenCrop = function(filepath, parsedUrl, fullDestinationPath){ 
    return Jimp.read(filepath) 
     .then(function (picture) { 
      var cropWidth = parsedUrl.query.w, 
       cropHeight = parsedUrl.query.h; 
      calculate(picture, parsedUrl); 
       picture.resize(parseInt(parsedUrl.query.w), parseInt(parsedUrl.query.h)) 
        .crop(parseInt((parsedUrl.query.w - cropWidth)/2), parseInt((parsedUrl.query.h - cropHeight)/2), parseInt(cropWidth), parseInt(cropHeight)) 
        .quality(parseInt(parsedUrl.query.quality)) 
        .write(fullDestinationPath) 
     }) 
     .catch(function (err) { 
      console.error(err); 
     }); 
}; 

發送

Router.prototype.send = function (response, code, headers, data) { 
    response.statusCode = code; 

    if (headers) { 
     for (var index in headers) { 
      if (headers.hasOwnProperty(index)) { 
       response.setHeader(index, headers[index]); 
      } 
     } 
    } 

    if (data instanceof Stream) { 
     data.pipe(response); 
    } else { 
     response.end(data); 
    } 
}; 

但也許它不能處理PNG或它嘗試調整它的大小錯誤?我測試過,並證實了這一情況並非如此簡單的代碼更改爲此:

else { 
     resizer 
      .resizeHandler(filepath, parsedUrl, fullDestinationPath) 
      .then(function() { 
       //return self.send(response, 200, {'Content-Type': mime.lookup(fullDestinationPath)}, fs.createReadStream(fullDestinationPath)); 
      }); 
    } 

現在,它沒有返回值和我的瀏覽器將永遠等待,因爲它不給回一個響應。但它確實在文件夾中創建了文件,就像它對JPG的影響一樣,這意味着它可以工作。在實際創建調整大小的文件之前調用createReadStream時,會導致崩潰,因爲該文件不存在。該文件也不會被創建,因爲創建它的服務器已停止。錯誤:

Error: ENOENT: no such file or directory, open '/var/www/pngbla_w512_h53_q80.png' 
    at Error (native) 

我該怎麼做才能使它適合我的PNG?爲什麼它不適用於我的PNG文件,即使對於某些JPG文件,它需要20秒鐘,因爲它被調整爲大分辨率。

編輯:我已經嘗試過多種尺寸,即使調整大小几乎是即時的~5ms,響應仍然會被調用之前與PNG。

回答

0

顯然,它已經開始寫JPG和BMP的,只有當它這樣做,我解決它使用回調更改代碼:

Resizer.prototype.resizeThenCrop = function(filepath, parsedUrl, fullDestinationPath){ 
    return Jimp.read(filepath) 
     .then(function (picture) { 
      var cropWidth = parsedUrl.query.w, 
       cropHeight = parsedUrl.query.h; 
      calculate(picture, parsedUrl); 

      return new Promise(function(resolve, reject) { 
       picture.resize(parseInt(parsedUrl.query.w), parseInt(parsedUrl.query.h)) 
        .crop(parseInt((parsedUrl.query.w - cropWidth)/2), parseInt((parsedUrl.query.h - cropHeight)/2), parseInt(cropWidth), parseInt(cropHeight)) 
        .quality(parseInt(parsedUrl.query.quality)) 
        .write(fullDestinationPath, function(err) { 
         if(err) { 
          return reject(err); 
         } 

         return resolve(fullDestinationPath); 
        }); 
      }); 
     }) 
     .catch(function (err) { 
      console.error(err); 
     }); 
}; 

問題是picture.resize沒有返回一個承諾所以它沒有等待寫入就完成了。