This repository has been archived on 2026-09-13. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
BPOS-MVP/apps/api/src/app/file-doc.ts
T
xcsandClaude Opus 5 0aa2fc11b0 资料文件真产出来了;E04/I01 补到验收线,顺手填了几个「墙拦住却不给路」的坑
**资料文件全链**(用户:审核资料这一步要真产出 word/excel/md/html,前端能渲、点资料名能开)
接手时这一步一个文件都产不出来 —— S3 剧本白纸黑字写着「file_ref 本 MVP 阶段可留空」。
文件是台账那一行的投影,不另存一份:
· 声明 `file:{titleColumn,bodyColumn,formatColumn}`,它同时是授权面(没声明就点不开)
· 现造真字节 —— docx/xlsx 是自己打的 OOXML 包(zip-write:store+CRC32,零依赖)
· 读回来渲 —— 解的就是造出来那份字节,渲得出来才证明文件是真的
· 件上 `data-file-of` → 工作区二级界面(Word 纸面/Excel 网格/渲好的 md/沙箱 html)+ 下载原件
放行落地前把每一份冻进 `sys.task_asset_file`(迁移 0029):归档件点开的必须是签字那一刻
那一份,而资料落在主数据上、试跑单不落正式表 —— 不存这一份,收工后一个都点不开。
工作成果里「产出的资料」自成一摞。Markdown 富文本渲染顺带销账(仍零依赖)。

**Harness**
· 画面按「这一拍办完之后」在办的那一步挑 —— 从前拿拍首的局面决定拍尾的事:
  收掉 S4、发出 S5 的卡,而屏上还摆着 S4 的只读件,人无处可填(墙要配路第四次现形)
· 验收「重做」真能退回重做,且打回后台账没动就不许再提交(软硬两段,不锁死单子)
· 立监听的授权面是清单声明;发卡的授权面是调人预算,不是计划项
· 读工具同参重调有兜底;空转写身后还有行动就不废串;finish 的 doneWhen 按写完后的草稿算

**界面**
· 翻件与翻页各归各位(从前二选一,多卡多页时页翻不动);没得翻写「—」不写 0/0
· 工作成果详情能翻份;表宽超出能横滑(E04/I01 抄版式时把 overflow-x 抄丢了)
· 查看面灰底滚过一屏变白 —— 两条独立的路都治了(沙箱内不同色 / 弹性子项不长个)
· 测试台「取不到」不再当成「还活着」

**活动**
E04 关系覆盖、I01 品牌内容各自补到验收线:判据全读基座数、判据指向的东西摆上卡面、
样例满载且名字 ≤14 字;提交前要裁的判据搬进说明书(模型会压拍,压过之后只有说明书还在)。

验收:e2e i01 4/4、e04 3/3 全部 released,资料在办中渲得出、放行后存了档点得开;
门禁 typecheck/lint/depcruise/1628 测全绿。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EEJX8Z7CHejvwmDQyPQuBX
2026-08-24 10:26:45 +08:00

122 lines
5.1 KiB
TypeScript

/**
* 造好的资料文件**读回来给前端渲染** —— 进字节,出 `FileDoc`。
*
* 为什么绕这一圈,而不是直接把 Markdown 原文丢给前端按格式摆:
* **那样界面渲的就不是那份文件了**。人下载到的是 .docx,屏上看到的是另一条路
* 拼出来的样子,两边哪天不一致谁也发现不了。这里读的就是 `composeFile` 出的那一份
* 字节:docx 解出段落、xlsx 解出行列 —— 渲得出来,才证明那份文件是真的。
*
* 纯计算:进字节、出结构,不碰库不碰网。
*/
import type { FileDoc } from '@contracts';
import { markdownHtmlDoc, type FileFormat } from './file-compose.ts';
import { readZip, sharedStrings, sheetToRows, unescapeXml } from './upload-extract.ts';
/** docx 的一段:`<w:p>` 里所有 `<w:t>` 连起来就是这一段的文字 */
function docxParagraphs(xml: string): Array<{ style: string; text: string }> {
const out: Array<{ style: string; text: string }> = [];
for (const p of xml.matchAll(/<w:p\b[^>]*>([\s\S]*?)<\/w:p>/g)) {
const inner = p[1] ?? '';
const style = /<w:pStyle\s+w:val="([^"]+)"/.exec(inner)?.[1] ?? '';
const text = [...inner.matchAll(/<w:t\b[^>]*>([\s\S]*?)<\/w:t>/g)].map((t) => unescapeXml(t[1] ?? '')).join('');
out.push({ style, text });
}
return out;
}
/** 查看面认得的块 —— 与 `FileDoc` 的 word 分支同形 */
export type WordBlock = { t: 'h1' | 'h2' | 'p'; text: string } | { t: 'ul'; items: string[] };
/**
* docx → 查看面的块。表格在 `<w:tbl>` 里,而查看面(`FileViewer` 的 word 分支)
* 只认 h1/h2/p/ul —— 表格按行摊成 `p`(用「 | 」连列),**宁可朴素也不丢内容**。
*/
export function docxBlocks(xml: string): WordBlock[] {
const blocks: WordBlock[] = [];
let bullets: string[] = [];
const flush = (): void => {
if (bullets.length === 0) return;
blocks.push({ t: 'ul', items: bullets });
bullets = [];
};
// 表格与段落按它们在文档里的先后走:一次扫过,谁先出现算谁
for (const node of xml.matchAll(/<w:tbl\b[\s\S]*?<\/w:tbl>|<w:p\b[^>]*>[\s\S]*?<\/w:p>/g)) {
const chunk = node[0];
if (chunk.startsWith('<w:tbl')) {
flush();
for (const tr of chunk.matchAll(/<w:tr\b[^>]*>([\s\S]*?)<\/w:tr>/g)) {
const cells = [...(tr[1] ?? '').matchAll(/<w:tc\b[^>]*>([\s\S]*?)<\/w:tc>/g)].map((tc) =>
[...(tc[1] ?? '').matchAll(/<w:t\b[^>]*>([\s\S]*?)<\/w:t>/g)].map((t) => unescapeXml(t[1] ?? '')).join(''),
);
if (cells.some((c) => c !== '')) blocks.push({ t: 'p', text: cells.join(' | ') });
}
continue;
}
const [para] = docxParagraphs(chunk);
if (para === undefined) continue;
const { style, text } = para;
if (text === '') continue;
if (style === 'ListParagraph') {
bullets.push(text.replace(/^[•\d]+[.、]?\s*/, ''));
continue;
}
flush();
if (style === 'Title' || style === 'Heading1') blocks.push({ t: 'h1', text });
else if (style === 'Heading2') blocks.push({ t: 'h2', text });
else blocks.push({ t: 'p', text });
}
flush();
return blocks;
}
/** xlsx → 行列(第一张表)。读的是**真包**:解不开就如实说,不编一张表出来 */
export function xlsxRows(bytes: Uint8Array): { rows: string[][]; sheet: string } | undefined {
const zip = readZip(bytes);
if (zip.size === 0) return undefined;
const dec = new TextDecoder('utf-8');
const sharedRaw = zip.get('xl/sharedStrings.xml');
const shared = sharedRaw === undefined ? [] : sharedStrings(dec.decode(sharedRaw));
const first = [...zip.keys()].filter((k) => /^xl\/worksheets\/sheet\d+\.xml$/.test(k)).sort()[0];
if (first === undefined) return undefined;
const raw = zip.get(first);
if (raw === undefined) return undefined;
const wbRaw = zip.get('xl/workbook.xml');
const sheet =
wbRaw === undefined ? 'Sheet1' : unescapeXml(/<sheet\s+name="([^"]*)"/.exec(dec.decode(wbRaw))?.[1] ?? 'Sheet1');
return { rows: sheetToRows(dec.decode(raw), shared), sheet };
}
/**
* 字节 → `FileDoc`。**读不出来就返回 undefined** —— 上层据此如实说
* 「这份文件打不开」,不摆一个空壳假装渲染过(界面不撒谎)。
*/
export function toFileDoc(input: {
readonly name: string;
readonly title: string;
readonly format: FileFormat;
readonly bytes: Uint8Array;
}): FileDoc | undefined {
const { name, title, format, bytes } = input;
const dec = new TextDecoder('utf-8');
if (format === 'html') return { kind: 'html', name, html: dec.decode(bytes) };
if (format === 'md') {
const md = dec.decode(bytes);
// 原文与渲好的一起交:查看面摆渲染版,而「下载」拿到的仍是这段原文
return { kind: 'markdown', name, md, html: markdownHtmlDoc(title, md) };
}
if (format === 'xlsx') {
const sheet = xlsxRows(bytes);
if (sheet === undefined) return undefined;
return { kind: 'excel', name, sheet: sheet.sheet, rows: sheet.rows };
}
const zip = readZip(bytes);
const doc = zip.get('word/document.xml');
if (doc === undefined) return undefined;
const blocks = docxBlocks(dec.decode(doc));
if (blocks.length === 0) return undefined;
return { kind: 'word', name, blocks };
}