2016-05-17 67 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没有返回一个承诺所以它没有等待写入就完成了。