Google クラウド ストレージからストリーミングした後、画像のサイズを変更しようとしています。ストリーミングは正常に機能しますが、サイズ変更は機能していないようです。画像オブジェクトをsharpjsのサイズ変更関数に渡す必要があります。これにより、サイズが変更され、API応答で画像が返されます。
私のコードは以下の通りです:
const express = require('express')
const {Storage} = require('@google-cloud/storage');
let server = express()
const storage = new Storage();
const bucketName = '<some-bucket-name>'
server.get('/*', async (req, res, next) => {
// Extract the query-parameter
const widthString = req.query.width
const heightString = req.query.height
const format = req.query.format
const fileName = req.path.substring(1);
console.log("url: ", req.path)
// Parse to integer if possible
let width, height
if (widthString) {
width = parseInt(widthString)
}
if (heightString) {
height = parseInt(heightString)
}
// Set the content-type of the response
res.type(`image/${format || 'png'}`)
// Get the resized image
downloadFileStream(fileName).then((imageObj) =>{
resize(imageObj, format, width, height).pipe(res)
})
.catch ((e) =>
console.log(e)
);
})
async function downloadFileStream(fileName){
return await storage.bucket(bucketName).file(fileName).createReadStream().pipe()
}
ファイルのダウンロード後にサイズ変更を呼び出す必要がある方法で何かが欠けています。次のエラーが表示されます。
TypeError: Cannot read property 'on' of undefined
at PassThrough.Readable.pipe (internal/streams/readable.js:657:8)
at streamFile (/home/adityam/imageoptimizer/server.js:40:82)
at /home/adityam/imageoptimizer/server.js:30:7