我想在上传时分析图像,如果内容被认为是不合适的,则对其进行模糊处理。
日志this image is inappropriate
工作正常,所以检测工作正常。但是我没有看到之后的任何日志,所以我猜blurImage
函数不能正常工作。
我很难调试这个函数中的错误,以及为什么我看不到任何进一步的日志。
exports.controlImage = functions.storage.object().onFinalize(
async (object) => {
const bucket = storage.bucket(object.bucket);
const file = bucket.file(object.name);
const fileName = object.name;
const destBucket = admin.storage().bucket(bucket);
const file = destBucket.file(filePath); */
// Check the image content using the Cloud Vision API.
return client.safeSearchDetection(`gs://${bucket.name}/${file.name}`)
.then((data) => {
const safeSearch = data[0];
functions.logger.log('SafeSearch results on image', safeSearch);
if (safeSearch.safeSearchAnnotation.violence === 'VERY_LIKELY' ||
safeSearch.safeSearchAnnotation.violence === 'POSSIBLE' ||
safeSearch.safeSearchAnnotation.adult === 'VERY_LIKELY' ||
safeSearch.safeSearchAnnotation.adult === 'POSSIBLE') {
functions.logger.log("this image is innapropriate")
blurImage(object.name, object.bucket, object.metadata);
return null
}
else functions.logger.log("this image is ok")
return null;
});
}
)
/**
* Blurs the given image located in the given bucket using ImageMagick.
*/
function blurImage(filePath, bucketName, metadata) {
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
const bucket = storage.bucket(bucketName);
// Create the temp directory where the storage file will be downloaded.
return mkdirp(tempLocalDir).then(() => {
functions.logger.log('Temporary directory has been created', tempLocalDir);
// Download file from bucket.
return bucket.file(filePath).download({destination: tempLocalFile});
}).then(() => {
functions.logger.log('The file has been downloaded to', tempLocalFile);
// Blur the image using ImageMagick.
return spawn('convert', [tempLocalFile, '-channel', 'RGBA', '-blur', '0x8', tempLocalFile]);
}).then(() => {
functions.logger.log('Blurred image created at', tempLocalFile);
// Uploading the Blurred image.
return bucket.upload(tempLocalFile, {
destination: filePath,
metadata: {metadata: metadata}, // Keeping custom metadata.
});
}).then(() => {
functions.logger.log('Blurred image uploaded to Storage at', filePath);
fs.unlinkSync(tempLocalFile);
return functions.logger.log('Deleted local file', filePath);
});
}
1条答案
按热度按时间yacmzcpb1#
可能是因为
实际上并不需要等到asynchronous
blurImage()
函数完成后,再向云函数平台提示可以清理云函数(通过return null;
)。换句话说,在返回
null
之前,您需要等待此blurImage()
函数完成。由于您使用的是
async
,因此这里是您的CF的改编版本(未将then()
与async/await
一起使用)(未经测试):