异步方法抛出异常,必挂
/**
* @description 抖店专用 - 图片理解,语义识别(同步任务)
* @returns {*} {Promise<any>}
* @memberof
*/
async getAIGCImageListCaption(req: AigcImageListCaptionReqPayload): Promise<any> {
// 通过URL获取图片对象
const processImageList = await formatDyImage(req.imageList);
// 循环调用图片OCR 接口获取OCR信息
const promises = processImageList.map(async (image) => {
try {
const options = await this.formatImageListCaptionReq(image);
const res = <AigcResponse>await got.post(AIGC_SERVICE_API['imageCaption'], options).json();
return res.code === 200 ? res['data'] : '';
} catch (error) {
this.logger.error(`ocr接口调用失败 error: ${error.message} param:${image}`)
return '';
}
});
const result = await Promise.all(promises)
// 处理完成,删除tmp文件夹
await deleteTempDyImage();
// 返回结果
return getImageCaptionResult(result);
}
/**
* 根据 imageSharp对象生成图片file文件
* @param imageSharp
*/
export const processImage = async (imageSharp, uniqueId?) => {
try {
const filename = `${uniqueId ? uniqueId : new Date().getTime()}.jpg`;
const outputDir = path.join(__dirname, DY_IMAGE_OUTPUT_PATH);
const outputPath = path.join(outputDir, filename);
// 检查目录是否存在,如果不存在则创建目录
await ensureDirectoryExists(outputDir);
// 将图像转换为 JPG 并保存到文件
await imageSharp.toFormat('jpg').toFile(outputPath);
// 检查文件读取权限
await checkFileReadPermission(outputPath);
// 读取生成的图片文件并返回
return await fs.readFile(outputPath);
} catch (e) {
utilLogger.error(`【根据 imageSharp 对象生成图片 file 文件】错误提示:${e.message}`);
throw new CustomException({ code: 500, message: '根据 imageSharp 对象生成图片 file 文件失败' });
}
};