2016-05-17 20 views
0

ファイルがJPGの場合、応答が返される前に(ブラウザにサイズ変更された画像を表示するために)作成されます。しかし、それは存在しない何かのためReadStream作成することはできませんので、それはNode.jsのサーバのクラッシュが生じPNGを書いています前に、それは返すPNGだとき:プロミスが遅すぎる

リサイズコール

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で正しく動作させるにはどうすればよいですか?それで、JPGファイルのサイズが大きくなって20秒かかっても、PNGファイルではうまくいかないのはなぜですか?

EDIT:リサイズがほぼ瞬間〜5msであっても、複数のサイズを試しましたが、応答は以前はPNGで呼び出されます。 picture.resizeは約束を返さなかったということでした

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); 
     }); 
}; 

問題:

答えて

0

は、どうやらそれはすでにJPGを書き始め、それが終了したときBMPは唯一、私がにコードを変更し、コールバックを使用してそれを解きますそれで、.writeが完了するのを待たずに続けました。