bufferStream抛出错误400 on node js

j1dl9f46  于 2023-04-29  发布在  Node.js
关注(0)|答案(1)|浏览(102)

我有这个代码是应该转换为缓冲区流的URL,所以我可以在我的API请求中使用它,但得到了错误

router.get("/", async (req, res) => {
    let imageUrl = 'https://oaidalleapiprodscus.blob.core.windows.net/private/org-yeajcMsnWR7hOKZLzEiwGQ5o/user-jDFXxFJWLVyxyKditQpOgSYF/img-KQoVwCMTB13cU2wZeVEzAajX.png?st=2023-04-26T05%3A41%3A25Z&se=2023-04-26T07%3A41%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-04-26T06%3A21%3A17Z&ske=2023-04-27T06%3A21%3A17Z&sks=b&skv=2021-08-06&sig=VUHDc/GxPPKdbzGU3deNsAiDGaHOHcr6CixnCdpSpeE%3D';
    try {
        const response = await axios.get(imageUrl, { responseType: 'arraybuffer' });
        const bufferStream = new stream.PassThrough();
        bufferStream.end(Buffer.from(response.data, 'binary'));

        const imageVariationResponse = await openai.createImageVariation(
            bufferStream,
            1,
            "256x256"
        );
        const result = imageVariationResponse.data.data[0].url;
        console.log('result :', result);
        res.json({ result: result });
    } catch (error) {
        console.error(error);
        res.status(500).json({ error: error });
    }
});

请求失败,状态代码为400

4nkexdtk

4nkexdtk1#

对于任何人寻找一个解决方案,这里是我更新的完整代码

router.post("/", async (req, res) => {
    var imageUrl = req.body.image;

    const imageBuffer = await axios.get(imageUrl, { responseType: 'arraybuffer' })
        .then((response) => {
            const buffer = new Buffer.from(response.data)
            buffer.name = 'image.png'
            return buffer
        })
        .catch((error) => {
            console.error('An error occurred while downloading the file:', error);
        });

    let jImage = await Jimp.read(imageBuffer);

    if (!jImage.hasAlpha()) { //Check if image has opacity
        jImage = jImage.opacity(1); //Add if it doesn't 
    }

    // Optional

    const w = jImage.bitmap.width;
    const h = jImage.bitmap.height;

    if ((w / h) != 1) {
        throw new functions.https.
            HttpsError("invalid-argument",
                "Image must be a square. Current ratio = " + (w / h));
    }

    const jsize = (await jImage.getBufferAsync(Jimp.AUTO)).byteLength;

    if (jsize >= 4000000) { //Check size
        throw new functions.https.
            HttpsError("invalid-argument",
                "Image must be less than 4MG currenty image is " +
                jsize + " bytes with Alpha");
    }

    await jImage.writeAsync("./image.png"); //Make PNG and wait for the file to be written

    try {
        const response = await openai.createImageVariation(
            fs.createReadStream("./image.png"),
            1,
            "256x256"
        );
        const image_url = response.data.data[0].url;
        console.log('variation :', image_url);
        res.json({ result: image_url });
    } catch (error) {
        console.error(error);
        res.status(500).json({ error: error });
    }

});

相关问题