Firebase云功能:`convert`因ChildProcessError而失败

时间:2018-06-05 12:20:08

标签: firebase google-cloud-functions imagemagick-convert

我正在使用Firebase Cloud功能自动生成上传图片的缩略图。

我按照示例代码@ https://github.com/firebase/functions-samples/blob/master/generate-thumbnail

并制作了这个文件:

"use strict";

const functions                 =   require("firebase-functions");
const mkdirp                    =   require("mkdirp-promise");
const gcs                       =   require("@google-cloud/storage")({keyFilename: functions.config().env.serviceaccount});
//const admin                     =   require("firebase-admin");
const spawn                     =   require("child-process-promise").spawn;
const path                      =   require("path");
const os                        =   require("os");
const fs                        =   require("fs");

// Max height and width of the thumb8ail in pixels.
const _800_MAX_WIDTH            =   800;
const _800_MAX_HEIGHT           =   800;
const _400_MAX_WIDTH            =   400;
const _400_MAX_HEIGHT           =   400;
const _100_MAX_WIDTH            =   100;
const _100_MAX_HEIGHT           =   100;

// Thumbnail prefix added to file names.
//const ORIGINAL_PREFIX           =   "original_";
const _800_PREFIX               =   "800_";
const _400_PREFIX               =   "400_";
const _100_PREFIX               =   "100_";

/**
 * When an image is uploaded in the Storage bucket We generate a thumbnail automatically using
 * ImageMagick.
 * After the thumbnail has been generated and uploaded to Cloud Storage,
 * we write the public URL to the Firebase Realtime Database.
 */
export const generateThumbnail  =   functions.storage.object().onChange(event => {
    const filePath              =   event.data.name;
    const fileDir               =   path.dirname(filePath);
    const fileName              =   path.basename(filePath);
    const filePath800           =   path.normalize(path.join(fileDir, `${_800_PREFIX}${fileName}`));
    const filePath400           =   path.normalize(path.join(fileDir, `${_400_PREFIX}${fileName}`));
    const filePath100           =   path.normalize(path.join(fileDir, `${_100_PREFIX}${fileName}`));
    const tempLocalFile         =   path.join(os.tmpdir(), filePath);
    //const tempLocalFile800      =   path.join(os.tmpdir(), filePath800);
    //const tempLocalFile400      =   path.join(os.tmpdir(), filePath400);
    //const tempLocalFile100      =   path.join(os.tmpdir(), filePath100);
    const tempLocalDir          =   path.dirname(tempLocalFile);
    const tempLocal800File      =   path.join(os.tmpdir(), filePath800);
    const tempLocal400File      =   path.join(os.tmpdir(), filePath400);
    const tempLocal100File      =   path.join(os.tmpdir(), filePath100);

    // Exit if this is triggered on a file that is not an image.
    if (!event.data.contentType.startsWith("image/")) {
        console.log("This is not an image.");
        return;
    }

    // Exit if the image is already a thumbnail.
    if (fileName.startsWith(_800_PREFIX) || fileName.startsWith(_400_PREFIX) || fileName.startsWith(_100_PREFIX)) {
        console.log("Already a Thumbnail.");
        return;
    }

    // Exit if this is a move or deletion event.
    if (event.data.resourceState === "not_exists") {
        console.log("This is a deletion event.");
        return;
    }

    // Cloud Storage files.
    const bucket = gcs.bucket(event.data.bucket);
    const file = bucket.file(filePath);
    //const file800 = bucket.file(filePath800);
    //const file400 = bucket.file(filePath400);
    //const file100 = bucket.file(filePath100);

    // Create the temp directory where the storage file will be downloaded.
    return mkdirp(tempLocalDir).then(() => {
        // Download file from bucket.
        return file.download({destination: tempLocalFile});
    }).then(() => {
        console.log("The file has been downloaded to", tempLocalFile);
        // Generate a thumbnail using ImageMagick.
        return Promise.all([
            spawn("convert", [tempLocalFile, "-thumbnail", `${_800_MAX_WIDTH}x${_800_MAX_HEIGHT}>`, tempLocal800File], {capture: ['stdout', 'stderr']}),
            spawn("convert", [tempLocalFile, "-thumbnail", `${_400_MAX_WIDTH}x${_400_MAX_HEIGHT}>`, tempLocal400File], {capture: ['stdout', 'stderr']}),
            spawn("convert", [tempLocalFile, "-thumbnail", `${_100_MAX_WIDTH}x${_100_MAX_HEIGHT}>`, tempLocal100File], {capture: ['stdout', 'stderr']})
        ]);
    }).then(() => {
        console.log("Thumbnail created at", tempLocal800File, tempLocal400File, tempLocal100File);
        // Uploading the Thumbnail.
        return Promise.all([
            bucket.upload(tempLocal800File, {destination: filePath800}),
            bucket.upload(tempLocal400File, {destination: filePath400}),
            bucket.upload(tempLocal100File, {destination: filePath100}),
        ]);
    }).then(() => {
        console.log("Thumbnail uploaded to Storage at", filePath800, filePath400, filePath100);
        // Once the image has been uploaded delete the local files to free up disk space.
        fs.unlinkSync(tempLocalFile);
        fs.unlinkSync(tempLocal800File);
        fs.unlinkSync(tempLocal400File);
        fs.unlinkSync(tempLocal100File);

        return true;
    });

});

这些日子的代码运作良好。但最近代码开始抛出这个错误:

ChildProcessError: `convert /tmp/gr/regionalData/in/31/3100/ngo/-Kz3c8teLyPWZGzRLdKD/-LEEop6Wvet4Aj1ybP-T.jpg -thumbnail 400x400> /tmp/gr/regionalData/in/31/3100/ngo/-Kz3c8teLyPWZGzRLdKD/400_-LEEop6Wvet4Aj1ybP-T.jpg` failed with code 1

我尝试重新部署,但没有用。有关从何处开始寻找问题原因的任何提示?

0 个答案:

没有答案