Ваша проблема возникает из-за смешивания API Promises и API обратного вызова.
Синтаксис async
/ await
работает только тогда, когда объединяются обещания.
Например, эта строка await tmp.dir
не ждет, пока _tempDirCreated
оценит и вернет ожидаемые обещания.
await tmp.dir(async function _tempDirCreated(err: any, path: any) {
/* ... */
});
// this line gets called immediately
Короче говоря, вам нужно будет либо принять основанные на обещаниях API, такие как tmp-promise
вместо node-tmp
,или оберните всю свою функцию в Обещание.
const tmp = require('tmp-promise');
export const makeRoundPicture = functions.https.onCall(async (data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.');
}
console.log('Called by: ', context.auth.uid);
const bucket = admin.storage().bucket();
const { path: tmpDirPath } = await tmp.dir();
const initialurl = "https://firebasestorage.googleapis.com/v0/b/myproject.appspot.com/o/"+context.auth.uid+".jpg?alt=media";
const options = {
url: initialurl,
dest: tmpDirPath
}
console.log('Temp Dir: ', tmpDirPath);
let { filename: originalFilename } = await download.image(options);
console.log('Saved: ', originalFilename);
let output = tmpDirPath + "photo.png";
let size = 230;
await gmMakeRoundPicture(originalFilename, output, size);
await bucket.upload(output, {
destination: "myfolder/"+context.auth.uid+".png",
});
});
function gmMakeRoundPicture(inFilePath, outFilePath, size) {
return new Promise((resolve, reject) => {
gm(inFilePath)
.resize(size, size)
.write(outFilePath, async (err) => {
if (err) return reject(err);
gm(size, size, 'none')
.fill(outFilePath)
.drawCircle(size/2.05, size/2.05, size/2.05, 0)
.write(outFilePath, (err) => err ? reject(err) : resolve());
})
});
}