gpt4 book ai didi

javascript - 错误 : Cloud Functions for Firebase spawn EACCES (ghostscript)

转载 作者:行者123 更新时间:2023-12-03 11:15:22 27 4
gpt4 key购买 nike

我尝试使用 Firebase Cloud Functions 创建 PDF 文件的缩略图。
调用 gs 后,出现以下错误:

2018-06-12T11:29:08.685Z E makeThumbnail: Error: spawn EACCES

at exports._errnoException (util.js:1020:11)

at ChildProcess.spawn (internal/child_process.js:328:11)

at exports.spawn (child_process.js:370:9)

at Object.exec (/user_code/node_modules/gs/index.js:86:28)

at Promise (/user_code/index.js:95:12)

at mkdirp.then.then (/user_code/index.js:86:12)

2018-06-12T11:29:08.698166767Z D makeThumbnail: Function execution took 780 ms, finished with status: 'error'


使用Spark以外的计划是否需要使用ghostscript之类的组件?
另外,我的代码。也许我只是在代码中没有看到我的问题
const functions = require('firebase-functions');
const mkdirp = require('mkdirp-promise');
const gcs = require('@google-cloud/storage')();
const admin = require('firebase-admin');

const spawn = require('child-process-promise').spawn;
const path = require('path');
const os = require('os');
const fs = require('fs');
const gs = require('gs');
const THUMB_MAX_HEIGHT = 200;
const THUMB_MAX_WIDTH = 200;
const THUMB_PREFIX = 'thumb_';
const gs_exec_path = path.join(__dirname, './lambda-ghostscript/bin/gs');

try{admin.initializeApp(functions.config().firebase); } catch(e) {}

exports.makeThumbnail = functions.storage.object().onFinalize((object) => {

const filePath = object.name;
const contentType = object.contentType;
const fileDir = path.dirname(filePath);
const fileName = path.basename(filePath);
const thumbFilePath = path.normalize(path.join(fileDir, `${THUMB_PREFIX} ${fileName}`));
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);

const tmp_dir = os.tmpdir();


if (fileName.startsWith(THUMB_PREFIX)) {
console.log('Is thumbnail');
return null;
}


const bucket = gcs.bucket(object.bucket);
const file = bucket.file(filePath);
const thumbFile = bucket.file(thumbFilePath);
const metadata = {
contentType: contentType,
};

return mkdirp(tmp_dir).then(() => {
console.log("Dir Created");

console.log(tempLocalFile);
return file.download({destination: tempLocalFile});
}).then(() => {
console.log("File downloaded");
if(!contentType.startsWith("image/")){

return new Promise((resolve, reject) => {
const pg= 1;
gs().batch().nopause()
.option(`-dFirstPage=${pg}`)
.option(`-dLastPage=${pg}`)
.executablePath(gs_exec_path)
.device('png16m')
.output(tempLocalThumbFile+".png")
.input(tempLocalFile)
.exec(err => err ? reject(err) : resolve());
});
}
else
{
var args = [ tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile ];
return spawn('convert', args, {capture: ['stdout', 'stderr']});
}
}).then(() => {

return bucket.upload(tempLocalThumbFile, { destination: thumbFilePath });
}).then(() => {

fs.unlinkSync(tempLocalFile);
fs.unlinkSync(tempLocalThumbFile);

return result[0];
});

});

最佳答案

经过几个小时的挠头并毫无意义地一遍又一遍地运行相同的代码,我终于找到了问题所在!

您定义的可执行路径不正确。应该是 'gs' .

这是一个完整的 gs() 调用示例:

gs()
.batch()
.option('-dFirstPage=2')
.option('-dLastPage=2')
.nopause()
.res(90)
.executablePath('gs')
.device('jpeg')
.output(tempNewPath2)
.input(tempFilePath)
.exec((err, stdout, stderr) => {
if (!err) {
console.log('gs executed w/o error');
console.log('stdout', stdout);
console.log('stderr', stderr);
resolve();
} else {
console.log('gs error:', err);
reject(err);
}
});

如需更多帮助,您可以查看我为此问题创建的示例存储库
https://github.com/krharsh17/ghostscript-firebase-sample

关于javascript - 错误 : Cloud Functions for Firebase spawn EACCES (ghostscript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50816826/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com