mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
* feat(terminal): 粘贴时自动上传剪贴板图片(远程会话,设置项控制) - 新增设置项 autoUploadClipboardImageOnPaste(默认关闭):剪贴板含图片时,远程 SSH 会话中粘贴自动通过 SFTP 上传图片到远端 .netcatty-paste-images/ 并输入远端路径,无图则回退普通文本粘贴 - terminalClipboardPaste.ts:图片检查优先于本地文件/文本粘贴;上传失败上报错误提示,不上传时静默回退 - 统一四路粘贴入口:右键/中键(onPaste)、DOM paste 事件(useTerminalFilePaste)、绑定快捷键(createXTermRuntime case "paste") - TerminalBehaviorSettings.tsx 新增设置开关,i18n 补齐 en/ru/zh-CN/zh-TW - syncPayload.ts 设置同步白名单加入新字段 - 测试:terminalClipboardPaste.test.ts 新增 4 个用例(上传/回退/失败上报/本地不受影响) Author: yangheng Model: 42865985-ead4-417f-88a9-12012a3495fe/deepseek-v4-flash * fix(terminal): 剪贴板图片上传异常时上报错误而非静默回退 - clipboardImagePaste.ts:readClipboardImage 抛错视为 no-image(读图失败与空剪贴板无法区分,可安全回退普通粘贴) - terminalClipboardPaste.ts:确认剪贴板有图后上传流程抛异常(如 SFTP 无法打开)→ 上报 upload-failed 错误并停止,与右键菜单路径一致,不再静默粘贴无关剪贴板内容 - 测试:新增「上传抛异常 → 上报 upload-failed 且不粘贴」「读图失败 → no-image」2 个用例,18/18 通过 Author: yangheng Model: 42865985-ead4-417f-88a9-12012a3495fe/deepseek-v4-flash --------- Co-authored-by: yangheng <492238647@qq.com>
142 lines
4.8 KiB
TypeScript
142 lines
4.8 KiB
TypeScript
const REMOTE_CLIPBOARD_IMAGE_DIR = ".netcatty-paste-images";
|
|
|
|
type ClipboardImageFile = {
|
|
path: string;
|
|
name: string;
|
|
mediaType: string;
|
|
size?: number;
|
|
};
|
|
|
|
export type RemoteClipboardImageBridge = Pick<
|
|
NetcattyBridge,
|
|
"readClipboardImage" | "openSftpForSession" | "startStreamTransfer"
|
|
> & Pick<Partial<NetcattyBridge>, "closeSftp" | "deleteTempFile">;
|
|
|
|
type TerminalLike = {
|
|
focus?: () => void;
|
|
};
|
|
|
|
type HandleRemoteClipboardImagePasteOptions = {
|
|
bridge?: RemoteClipboardImageBridge;
|
|
createTransferId?: () => string;
|
|
getRemoteCwd: () => Promise<string | null | undefined>;
|
|
isSensitiveInput?: () => boolean;
|
|
scrollToBottomAfterProgrammaticInput?: (data: string) => void;
|
|
sessionId: string | null | undefined;
|
|
terminalBackend: {
|
|
writeToSession: (sessionId: string, data: string, options?: { automated?: boolean; sensitive?: boolean }) => void;
|
|
};
|
|
term?: TerminalLike | null;
|
|
};
|
|
|
|
export type RemoteClipboardImageUploadResult =
|
|
| { ok: true; remotePath: string; pastedPath: string }
|
|
| { ok: false; reason: "unsupported" | "no-session" | "no-image" | "no-cwd" | "upload-failed" };
|
|
|
|
export function getRemoteClipboardImageUploadErrorMessageKey(
|
|
result: RemoteClipboardImageUploadResult,
|
|
): "terminal.clipboardImageUpload.noImage" | "terminal.clipboardImageUpload.failed" | null {
|
|
if (result.ok === true) return null;
|
|
return result.reason === "no-image"
|
|
? "terminal.clipboardImageUpload.noImage"
|
|
: "terminal.clipboardImageUpload.failed";
|
|
}
|
|
|
|
const shellSafePathPattern = /^[A-Za-z0-9_./~:@%+=,-]+$/;
|
|
|
|
export function sanitizeRemoteClipboardImageName(name: string): string {
|
|
const fallback = "netcatty-paste.png";
|
|
const trimmed = name.trim() || fallback;
|
|
const sanitized = trimmed
|
|
.replace(/[\0/\\]/g, "_")
|
|
.replace(/[^A-Za-z0-9._-]+/g, "_")
|
|
.replace(/_+/g, "_")
|
|
.replace(/^_+|_+$/g, "");
|
|
|
|
return sanitized || fallback;
|
|
}
|
|
|
|
export function buildRemoteClipboardImagePath(cwd: string | null | undefined, fileName: string): string {
|
|
const safeFileName = sanitizeRemoteClipboardImageName(fileName);
|
|
const normalizedCwd = typeof cwd === "string" ? cwd.trim() : "";
|
|
if (!normalizedCwd) return "";
|
|
const base = normalizedCwd.replace(/\/+$/g, "") || "/";
|
|
|
|
if (base === "/") {
|
|
return `/${REMOTE_CLIPBOARD_IMAGE_DIR}/${safeFileName}`;
|
|
}
|
|
|
|
return `${base}/${REMOTE_CLIPBOARD_IMAGE_DIR}/${safeFileName}`;
|
|
}
|
|
|
|
export function quoteRemotePathForShell(remotePath: string): string {
|
|
if (shellSafePathPattern.test(remotePath)) return remotePath;
|
|
return `'${remotePath.replace(/'/g, "'\\''")}'`;
|
|
}
|
|
|
|
function defaultTransferId(): string {
|
|
const uuid = globalThis.crypto?.randomUUID?.();
|
|
return uuid ? `clipboard-image-${uuid}` : `clipboard-image-${Date.now()}`;
|
|
}
|
|
|
|
export async function handleRemoteClipboardImageUpload({
|
|
bridge,
|
|
createTransferId = defaultTransferId,
|
|
getRemoteCwd,
|
|
isSensitiveInput,
|
|
scrollToBottomAfterProgrammaticInput,
|
|
sessionId,
|
|
terminalBackend,
|
|
term,
|
|
}: HandleRemoteClipboardImagePasteOptions): Promise<RemoteClipboardImageUploadResult> {
|
|
if (!sessionId) return { ok: false, reason: "no-session" };
|
|
if (!bridge?.readClipboardImage || !bridge.openSftpForSession || !bridge.startStreamTransfer) {
|
|
return { ok: false, reason: "unsupported" };
|
|
}
|
|
|
|
let image: ClipboardImageFile | null;
|
|
try {
|
|
image = await bridge.readClipboardImage();
|
|
} catch {
|
|
// A clipboard read failure is indistinguishable from an empty clipboard —
|
|
// treat it as "no image" so callers can fall back to a normal paste.
|
|
return { ok: false, reason: "no-image" };
|
|
}
|
|
if (!image?.path || !image.name) return { ok: false, reason: "no-image" };
|
|
|
|
let sftpId: string | undefined;
|
|
try {
|
|
const remoteCwd = await getRemoteCwd();
|
|
const targetPath = buildRemoteClipboardImagePath(remoteCwd, image.name);
|
|
if (!targetPath) return { ok: false, reason: "no-cwd" };
|
|
const transferId = createTransferId();
|
|
|
|
sftpId = await bridge.openSftpForSession(sessionId);
|
|
const transferResult = await bridge.startStreamTransfer({
|
|
transferId,
|
|
sourcePath: image.path,
|
|
targetPath,
|
|
sourceType: "local",
|
|
targetType: "sftp",
|
|
targetSftpId: sftpId,
|
|
totalBytes: image.size,
|
|
});
|
|
if (!transferResult || transferResult.error) return { ok: false, reason: "upload-failed" };
|
|
|
|
const pastedPath = quoteRemotePathForShell(targetPath);
|
|
terminalBackend.writeToSession(sessionId, pastedPath, {
|
|
sensitive: isSensitiveInput?.() === true,
|
|
});
|
|
scrollToBottomAfterProgrammaticInput?.(pastedPath);
|
|
term?.focus?.();
|
|
return { ok: true, remotePath: targetPath, pastedPath };
|
|
} finally {
|
|
if (sftpId && bridge.closeSftp) {
|
|
await bridge.closeSftp(sftpId).catch(() => undefined);
|
|
}
|
|
if (bridge.deleteTempFile) {
|
|
await bridge.deleteTempFile(image.path).catch(() => undefined);
|
|
}
|
|
}
|
|
}
|