Резкая ошибка: [Ошибка: отсутствует входной файл] - PullRequest
0 голосов
/ 10 марта 2020

Я загружаю изображение через request, а затем обрабатываю изображение через sharp. Но есть ошибка, что входной файл отсутствует, на самом деле переменная body имеет значение.

import { IADLandingPageABTest } from '@byted/ec-types';
import request from 'request';
import sharp from 'sharp';

const images: Array<keyof IADLandingPageABTest> = ['topPosterUrl', 'bottomPosterUrl'];

export default function handleImage (config: IADLandingPageABTest) {
    images.forEach(key => {
        const url = config[key];
        if (url && typeof url === 'string' ) {
           request(url, (err, response, body) => {
               //console.log('body', body);
               //body has a value
               if (!err && response.statusCode === 200) {
                sharp(body)
                .resize(100)
                .toBuffer()
                .then((data) => {
                    console.log(data.toString('base64'));
                })
                .catch( err => { console.log('error', err) });
               }
           })
        }
    });
}

1 Ответ

2 голосов
/ 10 марта 2020

Я обнаружил проблему в репозитории sharp, в которой изложено решение:

модуль request ожидает, что для encoding будет установлен body для Buffer.

- request(url, function(error, response, body) {
+ request({ url, encoding: null }, function(error, response, body) {

Источник: https://github.com/lovell/sharp/issues/930#issuecomment -326833522

...