Files
claude-code-haha/desktop/electron/services/workspaceBrowserPdf.ts
T
程序员阿江(Relakkes) 6f0650c5cb fix(workspace): align resource panels and complete backend state flows
Unify workspace controls and resource tabs, refine browser, file and diff
interactions, and remove superseded panels. Preserve resource lifecycles,
refresh Git comparisons after external changes, and correlate terminal
startup events across IPC.
2026-09-13 16:02:58 +08:00

35 lines
1.1 KiB
TypeScript

import { open, unlink } from 'node:fs/promises'
import path from 'node:path'
export async function saveWorkspaceBrowserPdf(
data: Uint8Array,
selectPath: () => Promise<string | null>,
): Promise<string | null> {
const savePath = await selectPath()
if (!savePath) return null
const { dir, name, ext } = path.parse(savePath)
// Exclusive creation handles both preexisting user files and two exports
// choosing the same name concurrently. Never truncate a selected path.
for (let suffix = 0; ; suffix += 1) {
const candidate = suffix === 0 ? savePath : path.join(dir, `${name} (${suffix})${ext}`)
const file = await open(candidate, 'wx').catch((error: NodeJS.ErrnoException) => {
if (error.code === 'EEXIST') return null
throw error
})
if (!file) continue
let written = false
try {
await file.writeFile(data)
written = true
return candidate
} finally {
try {
await file.close()
} finally {
// Windows cannot unlink an open file; only clean up our reservation.
if (!written) await unlink(candidate).catch(() => {})
}
}
}
}