File size: 3,041 Bytes
06671aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { spawn } from "child_process";
import { readFile, writeFile, mkdir, rm } from "fs/promises";
import { join } from "path";
import { randomUUID } from "crypto";
import { uploadFile, downloadFile } from "./storage";

const BOOKMARK_INJECTOR_PROJECT =
  process.env.BOOKMARK_INJECTOR_PROJECT ||
  join(__dirname, "../../tools/docx-bookmark-injector/docx-bookmark-injector.csproj");

export interface BookmarkSidecar {
  bookmarks: Record<
    string,
    {
      paragraph_id: string;
      native_para_id: string | null;
      container: "body" | "table_cell";
      table_index?: number;
      row_index?: number;
      cell_index?: number;
      text_preview: string;
    }
  >;
}

export async function injectBookmarks(
  inputDocxPath: string,
  workDir: string,
): Promise<{ outputDocxPath: string; sidecar: BookmarkSidecar }> {
  const outputDocxPath = join(workDir, `bookmarked_${randomUUID().replace(/-/g, "")}.docx`);
  const sidecarPath = join(workDir, `bookmark_sidecar_${randomUUID().replace(/-/g, "")}.json`);

  const command = [
    "dotnet",
    "run",
    "--project",
    BOOKMARK_INJECTOR_PROJECT,
    "--",
    "--input",
    inputDocxPath,
    "--output",
    outputDocxPath,
    "--sidecar",
    sidecarPath,
  ];

  const result = await runCommand(command);

  if (result.exitCode !== 0) {
    throw new Error(`Bookmark injector failed: ${result.stderr || result.stdout}`);
  }

  const sidecarRaw = await readFile(sidecarPath, "utf-8");
  const sidecar: BookmarkSidecar = JSON.parse(sidecarRaw);

  return { outputDocxPath, sidecar };
}

export async function storeBookmarkSidecar(
  userId: string,
  documentId: string,
  sidecar: BookmarkSidecar,
): Promise<string> {
  const storagePath = `documents/${userId}/${documentId}/bookmark_sidecar.json`;
  const bytes = Buffer.from(JSON.stringify(sidecar), "utf-8");
  await uploadFile(
    storagePath,
    bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer,
    "application/json",
  );
  return storagePath;
}

export async function loadBookmarkSidecar(
  userId: string,
  documentId: string,
): Promise<BookmarkSidecar | null> {
  const storagePath = `documents/${userId}/${documentId}/bookmark_sidecar.json`;
  const bytes = await downloadFile(storagePath);
  if (!bytes) return null;
  return JSON.parse(Buffer.from(bytes).toString("utf-8")) as BookmarkSidecar;
}

function runCommand(
  command: string[],
): Promise<{ exitCode: number; stdout: string; stderr: string }> {
  return new Promise((resolve) => {
    const proc = spawn(command[0], command.slice(1));
    let stdout = "";
    let stderr = "";
    proc.stdout.on("data", (d: Buffer) => (stdout += d.toString()));
    proc.stderr.on("data", (d: Buffer) => (stderr += d.toString()));
    proc.on("close", (code) =>
      resolve({ exitCode: code ?? 1, stdout, stderr }),
    );
    setTimeout(() => {
      proc.kill("SIGKILL");
      resolve({ exitCode: 1, stdout, stderr: stderr + "\n[TIMEOUT] Process killed after 120 seconds" });
    }, 120_000);
  });
}