fix: 截图目标文件路径兼容文件夹写法

- 目标文件路径语义为完整文件路径(含文件名), 提示文案不变
- 容错: 填的是已存在文件夹或以分隔符结尾时, 自动往里存默认文件名(screenshot_时间戳.png 或配置的文件名称), 不再报 EISDIR
This commit is contained in:
yu 2026-11-02 15:09:23 +08:00
parent 29f3443110
commit 1bb28e3368

View File

@ -75,6 +75,18 @@ export function cropPng(source: PNG, region: { x: number; y: number; width: numb
return cropped;
}
/** 目标路径容错:填的是文件夹(已存在或以分隔符结尾)时,自动往里存默认文件名 */
async function resolveOutputPath(outputPath: string, defaultName: string): Promise<string> {
if (/[\\/]$/.test(outputPath)) return path.join(outputPath, defaultName);
try {
const stat = await fs.stat(outputPath);
if (stat.isDirectory()) return path.join(outputPath, defaultName);
} catch {
// 路径不存在说明是要新建的文件,原样返回
}
return outputPath;
}
register("Image_Screenshot", async (ctx, node, args) => {
const folder = text(args, ["outImagePath", "folder"]);
const name = text(args, ["outImageName", "name"], `screenshot_${Date.now()}.png`);
@ -95,9 +107,12 @@ register("Image_Screenshot", async (ctx, node, args) => {
image = PNG.sync.read(await fs.readFile(sourceFile));
output = folder ? path.resolve(path.join(folder, name)) : sourceFile;
} else {
// 当前屏幕来源:image_path 即保存路径,缺省用 输出目录+文件名;再缺省落流程证据目录(路径可预期)
// 当前屏幕来源:image_path 即保存路径(文件夹则自动追加默认文件名),缺省用 输出目录+文件名;再缺省落流程证据目录(路径可预期)
image = await capturePrimaryScreenPng();
output = path.resolve(imageArg || (folder ? path.join(folder, name) : path.join(ctx.evidenceDir, name)));
output = await resolveOutputPath(
path.resolve(imageArg || (folder ? path.join(folder, name) : path.join(ctx.evidenceDir, name))),
name
);
}
if (range === "2") {