fix: file sizes from PowerShell, quoted mentions, dedupe and BOM
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
1 parent
761a80004a
commit
1ed04fe9be
5 files changed
+339
-94
No files matched your search
+131
-25
@@ -1,5 +1,5 @@
|
||||
// clipboard.js - what the composer asks the system for: the clipboard, the
|
||||
// file picker, and the old files of the paste folder.
|
||||
// file picker, the sizes of files, and the old files of the paste folder.
|
||||
//
|
||||
// Pure: no gpui-kit, no gpui-base, no herdr. The scripts are PowerShell text
|
||||
// (run by main.js with `exec("powershell", ["-NoProfile", "-STA", "-Command",
|
||||
@@ -7,10 +7,16 @@
|
||||
// machine), and the parsers read what the scripts print, so both halves are
|
||||
// tested in plain Node (tests/clipboard.test.mjs).
|
||||
//
|
||||
// The mods have no `stat` (fs/promises: no size, no mtime), so the SIZE of
|
||||
// every file comes from PowerShell, in the same call that finds the file.
|
||||
//
|
||||
// The scripts print one fact per line, the fields separated by a tab:
|
||||
// FILES<TAB><path> (one line per file) - files copied in the Explorer
|
||||
// IMAGE<TAB><path> - an image, saved as a PNG
|
||||
// NONE - nothing to paste
|
||||
// FILES<TAB><path><TAB><bytes> (one line per file) - files from the Explorer
|
||||
// IMAGE<TAB><path><TAB><bytes> - an image, saved as a PNG
|
||||
// DIR<TAB><path> - a folder (not carried)
|
||||
// NONE - nothing to paste
|
||||
// An empty or missing <bytes> means the size could not be read: the caller
|
||||
// does not let such a file through (fail closed).
|
||||
|
||||
/** How long a pasted image stays in the paste folder. */
|
||||
export const PASTE_MAX_AGE_MS = 7 * 864e5;
|
||||
@@ -19,8 +25,8 @@ export const PASTE_MAX_AGE_MS = 7 * 864e5;
|
||||
const IMAGE_APPS = new Set(["claude-code", "codex", "gemini", "opencode"]);
|
||||
|
||||
/**
|
||||
* The PowerShell that reads the clipboard: files first, then an image (saved
|
||||
* as `<outDir>\paste-<stamp>.png`), otherwise `NONE`.
|
||||
* The PowerShell that reads the clipboard: files first (folders apart), then
|
||||
* an image (saved as `<outDir>\paste-<stamp>.png`), otherwise `NONE`.
|
||||
*
|
||||
* @param {string} outDir - where a pasted image is saved
|
||||
* @param {string} stamp - what makes the file name its own (a moment)
|
||||
@@ -37,7 +43,15 @@ export function clipboardScript(outDir, stamp) {
|
||||
"$tab = [char]9",
|
||||
"$files = [System.Windows.Forms.Clipboard]::GetFileDropList()",
|
||||
"if ($files -ne $null -and $files.Count -gt 0) {",
|
||||
' foreach ($f in $files) { Write-Output ("FILES" + $tab + $f) }',
|
||||
" foreach ($f in $files) {",
|
||||
" if (Test-Path -LiteralPath $f -PathType Container) {",
|
||||
' Write-Output ("DIR" + $tab + $f)',
|
||||
" } else {",
|
||||
" $len = ''",
|
||||
" try { $len = (Get-Item -LiteralPath $f -ErrorAction Stop).Length } catch { }",
|
||||
' Write-Output ("FILES" + $tab + $f + $tab + $len)',
|
||||
" }",
|
||||
" }",
|
||||
" exit 0",
|
||||
"}",
|
||||
"$img = [System.Windows.Forms.Clipboard]::GetImage()",
|
||||
@@ -46,7 +60,9 @@ export function clipboardScript(outDir, stamp) {
|
||||
" New-Item -ItemType Directory -Force -Path $dir | Out-Null",
|
||||
` $path = Join-Path $dir '${name}'`,
|
||||
" $img.Save($path, [System.Drawing.Imaging.ImageFormat]::Png)",
|
||||
' Write-Output ("IMAGE" + $tab + $path)',
|
||||
" $len = ''",
|
||||
" try { $len = (Get-Item -LiteralPath $path -ErrorAction Stop).Length } catch { }",
|
||||
' Write-Output ("IMAGE" + $tab + $path + $tab + $len)',
|
||||
" exit 0",
|
||||
"}",
|
||||
"Write-Output 'NONE'",
|
||||
@@ -56,8 +72,12 @@ export function clipboardScript(outDir, stamp) {
|
||||
/**
|
||||
* What the clipboard script printed.
|
||||
*
|
||||
* `files` are the things to attach (an image comes as one file); `dirs` are
|
||||
* folders, which are not carried. Only folders: `{kind:"dirs"}`.
|
||||
*
|
||||
* @param {{code?:number, stdout?:string, stderr?:string}|null} res
|
||||
* @returns {{kind:"image", paths:string[]} | {kind:"files", paths:string[]} | {kind:"none"} | {kind:"error", message:string}}
|
||||
* @returns {{kind:"image"|"files", files:{path:string, bytes:number|null}[], dirs:string[]}
|
||||
* | {kind:"dirs", dirs:string[]} | {kind:"none"} | {kind:"error", message:string}}
|
||||
*/
|
||||
export function parseClipboardResult(res) {
|
||||
if (!res || typeof res !== "object") return { kind: "error", message: "Brak wyniku z PowerShell" };
|
||||
@@ -68,25 +88,25 @@ export function parseClipboardResult(res) {
|
||||
}
|
||||
|
||||
const files = [];
|
||||
let image = "";
|
||||
const dirs = [];
|
||||
let image = null;
|
||||
for (const line of lines(res.stdout)) {
|
||||
const tab = line.indexOf("\t");
|
||||
if (tab < 0) continue;
|
||||
const kind = line.slice(0, tab);
|
||||
const path = line.slice(tab + 1).trim();
|
||||
if (path === "") continue;
|
||||
if (kind === "FILES") files.push(path);
|
||||
if (kind === "IMAGE" && image === "") image = path;
|
||||
const [kind, path, size] = fields(line);
|
||||
if (!path) continue;
|
||||
if (kind === "FILES") files.push({ path, bytes: toBytes(size) });
|
||||
if (kind === "IMAGE" && image === null) image = { path, bytes: toBytes(size) };
|
||||
if (kind === "DIR") dirs.push(path);
|
||||
}
|
||||
|
||||
if (files.length > 0) return { kind: "files", paths: files };
|
||||
if (image !== "") return { kind: "image", paths: [image] };
|
||||
if (files.length > 0) return { kind: "files", files, dirs };
|
||||
if (image) return { kind: "image", files: [image], dirs };
|
||||
if (dirs.length > 0) return { kind: "dirs", dirs };
|
||||
return { kind: "none" };
|
||||
}
|
||||
|
||||
/**
|
||||
* The PowerShell that opens the file dialog (several files may be picked) and
|
||||
* prints one path per line. Cancel prints nothing.
|
||||
* prints `<path><TAB><bytes>` per line. Cancel prints nothing.
|
||||
*
|
||||
* @returns {string}
|
||||
*/
|
||||
@@ -94,24 +114,85 @@ export function pickerScript() {
|
||||
return [
|
||||
"Add-Type -AssemblyName System.Windows.Forms",
|
||||
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8",
|
||||
"$tab = [char]9",
|
||||
"$dialog = New-Object System.Windows.Forms.OpenFileDialog",
|
||||
"$dialog.Multiselect = $true",
|
||||
"$dialog.Title = 'Wybierz pliki'",
|
||||
"if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {",
|
||||
" foreach ($f in $dialog.FileNames) { Write-Output $f }",
|
||||
" foreach ($f in $dialog.FileNames) {",
|
||||
" $len = ''",
|
||||
" try { $len = (Get-Item -LiteralPath $f -ErrorAction Stop).Length } catch { }",
|
||||
' Write-Output ($f + $tab + $len)',
|
||||
" }",
|
||||
"}",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* The paths the dialog printed; cancel, or a failed run, is no path at all.
|
||||
* The files the dialog printed; cancel, or a failed run, is no file at all.
|
||||
*
|
||||
* @param {{code?:number, stdout?:string}|null} res
|
||||
* @returns {string[]}
|
||||
* @returns {{path:string, bytes:number|null}[]}
|
||||
*/
|
||||
export function parsePickerResult(res) {
|
||||
if (!res || typeof res !== "object" || res.code !== 0) return [];
|
||||
return lines(res.stdout);
|
||||
return lines(res.stdout).map((line) => {
|
||||
const [path, size] = fieldsLast(line);
|
||||
return { path, bytes: toBytes(size) };
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The PowerShell that measures a batch of paths in one call: `SIZE` for a
|
||||
* file, `DIR` for a folder, `MISSING` for what is not there.
|
||||
*
|
||||
* @param {string[]} paths
|
||||
* @returns {string}
|
||||
*/
|
||||
export function sizeScript(paths) {
|
||||
const list = (Array.isArray(paths) ? paths : []).map(quote).join(", ");
|
||||
return [
|
||||
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8",
|
||||
"$tab = [char]9",
|
||||
`$paths = @(${list})`,
|
||||
"foreach ($p in $paths) {",
|
||||
" if (Test-Path -LiteralPath $p -PathType Container) {",
|
||||
' Write-Output ("DIR" + $tab + $p)',
|
||||
" } elseif (Test-Path -LiteralPath $p -PathType Leaf) {",
|
||||
" $len = ''",
|
||||
" try { $len = (Get-Item -LiteralPath $p -ErrorAction Stop).Length } catch { }",
|
||||
' Write-Output ("SIZE" + $tab + $p + $tab + $len)',
|
||||
" } else {",
|
||||
' Write-Output ("MISSING" + $tab + $p)',
|
||||
" }",
|
||||
"}",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* What `sizeScript` printed. A run that failed has no sizes at all: `error`
|
||||
* says why, and the caller does not attach anything on a guess.
|
||||
*
|
||||
* @param {{code?:number, stdout?:string, stderr?:string}|null} res
|
||||
* @returns {{files:{path:string, bytes:number|null}[], dirs:string[], missing:string[], error:string|null}}
|
||||
*/
|
||||
export function parseSizeResult(res) {
|
||||
const empty = { files: [], dirs: [], missing: [], error: null };
|
||||
if (!res || typeof res !== "object") return { ...empty, error: "Brak wyniku z PowerShell" };
|
||||
if (res.code !== 0) {
|
||||
const stderr = String(res.stderr ?? "").trim();
|
||||
return { ...empty, error: stderr || `PowerShell zakończył się kodem ${res.code}` };
|
||||
}
|
||||
|
||||
const out = { files: [], dirs: [], missing: [], error: null };
|
||||
for (const line of lines(res.stdout)) {
|
||||
const [kind, path, size] = fields(line);
|
||||
if (!path) continue;
|
||||
if (kind === "SIZE") out.files.push({ path, bytes: toBytes(size) });
|
||||
if (kind === "DIR") out.dirs.push(path);
|
||||
if (kind === "MISSING") out.missing.push(path);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,14 +220,39 @@ export function supportsImages(app) {
|
||||
return typeof app === "string" && IMAGE_APPS.has(app.trim());
|
||||
}
|
||||
|
||||
/** The non-empty lines of an output. */
|
||||
/** The non-empty lines of an output; a BOM in front of it is not part of it. */
|
||||
function lines(text) {
|
||||
return String(text ?? "")
|
||||
.replace(/^/, "")
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.replace(/\r$/, ""))
|
||||
.filter((line) => line.trim() !== "");
|
||||
}
|
||||
|
||||
/** `KIND<TAB>path<TAB>size` -> [kind, path, size]. */
|
||||
function fields(line) {
|
||||
const first = line.indexOf("\t");
|
||||
if (first < 0) return [line, "", ""];
|
||||
const rest = line.slice(first + 1);
|
||||
const second = rest.indexOf("\t");
|
||||
return second < 0
|
||||
? [line.slice(0, first), rest.trim(), ""]
|
||||
: [line.slice(0, first), rest.slice(0, second).trim(), rest.slice(second + 1).trim()];
|
||||
}
|
||||
|
||||
/** `path<TAB>size` -> [path, size] (the size is behind the last tab). */
|
||||
function fieldsLast(line) {
|
||||
const at = line.lastIndexOf("\t");
|
||||
return at < 0 ? [line.trim(), ""] : [line.slice(0, at).trim(), line.slice(at + 1).trim()];
|
||||
}
|
||||
|
||||
/** A size in bytes, or null when it is not a size. */
|
||||
function toBytes(text) {
|
||||
if (typeof text !== "string" || text.trim() === "") return null;
|
||||
const value = Number(text);
|
||||
return Number.isFinite(value) && value >= 0 ? value : null;
|
||||
}
|
||||
|
||||
/** A PowerShell single-quoted string. */
|
||||
function quote(text) {
|
||||
return `'${String(text).replace(/'/g, "''")}'`;
|
||||
|
||||
Reference in new issue
Block a user