{"draft":null,"from":28,"to":29,"files":[{"path":"client/src/issues.ts","added":68,"removed":6,"diff":"--- a/client/src/issues.ts\n+++ b/client/src/issues.ts\n@@ -5,8 +5,8 @@\n // In review, and publish --done closes it. Other agents' and people's words\n // are quoted as JSON, so none can print as another line (#30).\n \n-import { readFileSync, statSync } from \"node:fs\";\n-import { basename, resolve } from \"node:path\";\n+import { mkdirSync, readFileSync, statSync, writeFileSync } from \"node:fs\";\n+import { basename, join, resolve } from \"node:path\";\n import {\n \ttype Api,\n \tApiError,\n@@ -25,13 +25,15 @@\n import { type Config, readMachineServers, trustedServers } from \"./config.ts\";\n import { issuesServerFor, tokenGoesTo } from \"./core.ts\";\n import { fromEnv } from \"./env-names.ts\";\n+import { ignoredHere } from \"./fetch.ts\";\n+import { loadIgnore, relativeTo } from \"./folder.ts\";\n import { seenText, shortWhen } from \"./times.ts\";\n \n const q = (s: string): string => JSON.stringify(s);\n \n /** The folder, with its Api pointed at the Issues Worker: the token goes only where tokenGoesTo allows, as for the avcs server. */\n-function openIssues(env: Env): { config: Config; api: Api } {\n-\tconst { config, api } = open(env);\n+function openIssues(env: Env): { root: string; config: Config; api: Api } {\n+\tconst { root, config, api } = open(env);\n \tconst server = fromEnv(env.env, \"ISSUES_SERVER\") || issuesServerFor(config.server, readMachineServers(env.env));\n \tif (server === undefined)\n \t\tthrow new UsageError(\n@@ -43,7 +45,7 @@\n \t\t);\n \t}\n \tapi.issuesServer = server;\n-\treturn { config, api };\n+\treturn { root, config, api };\n }\n \n function issueNumber(word: string | undefined, what = \"an issue number\"): number {\n@@ -172,6 +174,57 @@\n \t]);\n }\n \n+/** A saved attachment's file name: its id, then its own name with anything but letters, digits, . - _ made _. */\n+export function savedName(id: string, name: string): string {\n+\tconst own = (name.split(/[\\\\/]/).pop() ?? \"\")\n+\t\t.replace(/[^A-Za-z0-9._-]/g, \"_\")\n+\t\t.replace(/^\\.+/, \"_\")\n+\t\t.slice(0, 100);\n+\treturn `${id}-${own || \"file\"}`;\n+}\n+\n+/**\n+ * abso issue N --save-attachments <folder>: every attachment on the issue and\n+ * its comments, written to that folder as <id>-<name>; an svg's own file\n+ * beside the png drawn from it. Not into the abso folder unless abso leaves\n+ * that place out, or the images would be recorded as edits.\n+ */\n+async function saveAttachments(api: Api, root: string, cwd: string, dir: string, page: IssuePage): Promise<Output> {\n+\tconst target = resolve(cwd, dir);\n+\tconst inside = target === resolve(root) ? \"\" : relativeTo(root, target);\n+\tif (inside !== undefined && (inside === \"\" || !ignoredHere(loadIgnore(root), inside, true)))\n+\t\tthrow new UsageError(\n+\t\t\t`${dir} is inside this abso folder, so abso would record the images as your edits. Save them outside the folder, or into a folder .absoignore leaves out`,\n+\t\t);\n+\tconst seen = new Set<string>();\n+\tconst all = [...(page.issue.attachments ?? []), ...page.comments.flatMap((c) => c.attachments ?? [])].filter(\n+\t\t(a) => !seen.has(a.id) && seen.add(a.id),\n+\t);\n+\tif (all.length === 0)\n+\t\treturn {\n+\t\t\ttext: `Issue ${page.issue.id} has no attachments.`,\n+\t\t\tjson: { issue: page.issue.id, folder: target, files: [] },\n+\t\t};\n+\tmkdirSync(target, { recursive: true });\n+\tconst files: { id: string; path: string; size: number }[] = [];\n+\tfor (const a of all) {\n+\t\tconst wanted = [{ name: a.name, source: false }, ...(a.source ? [{ name: a.source.name, source: true }] : [])];\n+\t\tfor (const w of wanted) {\n+\t\t\tconst bytes = await api.attachmentBytes(a.id, w.source, ATTACH_MAX);\n+\t\t\tconst path = join(target, savedName(a.id, w.name));\n+\t\t\twriteFileSync(path, bytes);\n+\t\t\tfiles.push({ id: a.id, path, size: bytes.length });\n+\t\t}\n+\t}\n+\treturn {\n+\t\ttext: [\n+\t\t\t`Saved ${plural(files.length, \"file\")} from issue ${page.issue.id} to ${target}:`,\n+\t\t\t...files.map((f) => `  ${basename(f.path)}, ${sizeText(f.size)}`),\n+\t\t].join(\"\\n\"),\n+\t\tjson: { issue: page.issue.id, folder: target, files },\n+\t};\n+}\n+\n /** An issue's page, as text. */\n export function issuePage(p: IssuePage, api?: Pick<Api, \"attachmentUrl\">): string[] {\n \tconst i = p.issue;\n@@ -416,7 +469,15 @@\n \n export async function runIssue(args: Parsed, env: Env): Promise<Output> {\n \tconst [first, ...rest] = args.positionals;\n-\tconst { api } = openIssues(env);\n+\tconst { root, api } = openIssues(env);\n+\tconst saveTo = str(args, \"save-attachments\");\n+\tif (saveTo !== undefined) {\n+\t\tconst other = [...args.flags.keys()].filter((f) => f !== \"save-attachments\" && f !== \"json\");\n+\t\tif (other.length > 0 || first === \"new\" || first === \"draft\")\n+\t\t\tthrow new UsageError(\n+\t\t\t\t`--save-attachments goes alone with an issue number: abso issue <N> --save-attachments <folder>${other.length > 0 ? `, not with --${other[0]}` : \"\"}`,\n+\t\t\t);\n+\t}\n \tif (first === \"draft\") return runDraft(api, args, rest);\n \tif (first === \"new\") {\n \t\tif (rest.length === 0)\n@@ -522,6 +583,7 @@\n \t\t};\n \t}\n \tconst page = await api.issue(id);\n+\tif (saveTo !== undefined) return saveAttachments(api, root, env.cwd, saveTo, page);\n \treturn { text: issuePage(page, api).join(\"\\n\"), json: { ...page } };\n }\n "}],"unmatched":[]}