Public: anyone can read this; the crew can change it.
A .dwg, .blend or .fbx file shows what its header says: format, version and size, not raw bytes #14696
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14671-a-dwg-blend-or-fbx-file-shows-what-its-h → main 10 commits fork 4 threads · 4 unresolved
12 files
changed +291
−4
docs/features/binary-facts.md added +13
| @@ -0,0 +1,13 @@ | ||
| 1 | --- | |
| 2 | topic: Files in the browser | |
| 3 | row: A .dwg, .blend or .fbx file: format, version and size | |
| 4 | --- | |
| 5 | # A .dwg, .blend or .fbx file | |
| 6 | ||
| 7 | The forge cannot draw these, but it says what they are: kind, version, size and a Download link. Code: `src/web/render/binary-facts.ts` (`binaryFacts`, pure), `src/web/render/binary-view.ts` (the page). In the terminal, open the file in the workspace (`src/tui/screens/workspace_cad.ts`, `cadView`): the same lines in place of a hex dump. | |
| 8 | ||
| 9 | - **dwg:** `AC10xx` in the first six bytes names the AutoCAD release (`AC1032` is 2018). | |
| 10 | - **blend:** `BLENDER` and the version in the header (`BLENDER-v404` is 4.4; the Blender 5 header is read too). A gzip- or zstd-compressed `.blend` is not read: it stays a binary. | |
| 11 | - **fbx:** `Kaydara FBX Binary`, then the version number (7400 is 7.4). ASCII FBX stays text. | |
| 12 | ||
| 13 | Only the first 64 bytes are looked at, at any file size. The magic decides, never the name alone: a `.dwg` that does not open on `AC10xx` is an ordinary binary. A header cut before its version shows the kind and says so. |
docs/features/file-kinds.md +1
| @@ -26,6 +26,7 @@ The forge draws a file on its page when its kind is below and it is within the s | ||
| 26 | 26 | - **EPUB book**, drawn up to 20 MB: `.epub` |
| 27 | 27 | - **Font**, drawn up to 20 MB: `.ttf`, `.otf`, `.woff`, `.woff2` |
| 28 | 28 | - **Photoshop**, drawn up to 4 GB: `.psd` |
| 29 | - **CAD or 3D source (header facts)**, drawn up to 4 GB: `.dwg`, `.blend`, `.fbx` | |
| 29 | 30 | - **Config**, drawn up to 1 MB: `.json`, `.jsonc`, `.yaml`, `.yml`, `.toml`, `.conf` |
| 30 | 31 | - **Markdown**, drawn up to 1 MB: `.md`, `.markdown`, `.mdx` |
| 31 | 32 | - **Notebook**, drawn up to 1 MB: `.ipynb` |
scripts/kinds-guide.ts +1 −1
| @@ -11,7 +11,7 @@ export const END = "<!-- file-kinds:end -->"; | ||
| 11 | 11 | export const NAMES: Readonly<Record<FileKind, string>> = { |
| 12 | 12 | image: "Image", svg: "SVG", pdf: "PDF", html: "HTML page", audio: "Audio", video: "Video", csv: "Table (CSV, TSV, JSON lines)", |
| 13 | 13 | sqlite: "SQLite database", parquet: "Parquet", model3d: "3D model", archive: "Archive", sheet: "Spreadsheet", document: "Word document", |
| 14 | slides: "Slides", layers: "Layered image", font: "Font", psd: "Photoshop", config: "Config", markdown: "Markdown", | |
| 14 | slides: "Slides", layers: "Layered image", font: "Font", psd: "Photoshop", cad: "CAD or 3D source (header facts)", config: "Config", markdown: "Markdown", | |
| 15 | 15 | notebook: "Notebook", patch: "Patch", log: "Log", subtitles: "Subtitles", gpx: "GPS track", har: "HTTP archive", |
| 16 | 16 | geojson: "GeoJSON", kml: "KML", calendar: "Calendar", otio: "OpenTimelineIO", edl: "Edit list", fcpxml: "Final Cut XML", kdenlive: "Kdenlive project", |
| 17 | 17 | contacts: "Contacts", mail: "Email", gcode: "G-code", dxf: "DXF drawing", gerber: "Gerber layer", mermaid: "Mermaid diagram", drawio: "draw.io diagram", |
src/tui/screens/workspace.ts +2 −1
| @@ -96,6 +96,7 @@ import { sqliteStep, sqliteView, type SqliteAt } from "./workspace_sqlite.ts"; | ||
| 96 | 96 | import { officeView } from "./workspace_office.ts"; |
| 97 | 97 | import { parquetView } from "./workspace_parquet.ts"; |
| 98 | 98 | import { bibView } from "./workspace_bib.ts"; |
| 99 | import { cadView } from "./workspace_cad.ts"; | |
| 99 | 100 | import { layoutTable, type Grid } from "../ui/grid.ts"; |
| 100 | 101 | import type { Ctx, Screen } from "../app/app.ts"; |
| 101 | 102 | import { stripAnsi } from "../term/ansi.ts"; |
| @@ -1553,7 +1554,7 @@ export function workspaceScreen( | ||
| 1553 | 1554 | // bytes as text is how a terminal ends up beeping at you. |
| 1554 | 1555 | const base = looksLikeImage(t.path) ? null : sqliteView(t.path, read.value.body, { table: 0, page: 1 }); |
| 1555 | 1556 | if (base !== null && typeof base !== "string") db = read.value.body; |
| 1556 | const book = looksLikeImage(t.path) ? null : (base ?? sheetView(t.path, read.value.body) ?? officeView(t.path, read.value.body) ?? parquetView(t.path, read.value.body) ?? bibView(t.path, read.value.body)); | |
| 1557 | const book = looksLikeImage(t.path) ? null : (base ?? sheetView(t.path, read.value.body) ?? officeView(t.path, read.value.body) ?? parquetView(t.path, read.value.body) ?? bibView(t.path, read.value.body) ?? cadView(t.path, read.value.body, read.value.size, formatSize)); | |
| 1557 | 1558 | if (looksLikeImage(t.path)) picture = read.value.body; |
| 1558 | 1559 | else if (book !== null && typeof book !== "string") sheet = book; |
| 1559 | 1560 | else if (typeof book === "string") note = book; |
src/tui/screens/workspace_cad.ts added +24
| @@ -0,0 +1,24 @@ | ||
| 1 | /** | |
| 2 | * A .dwg, .blend or .fbx file in the terminal workspace, read the way the page reads it: the same | |
| 3 | * `binaryFacts`, said as a few lines (kind, version, size) in place of a hex dump. The same gate | |
| 4 | * too: the name and the first bytes must say so (`fileKind`). | |
| 5 | * | |
| 6 | * `cadView(path, body, size)` answers the lines (the workspace shows them as its note), or `null` | |
| 7 | * for a file that is not one of these. | |
| 8 | */ | |
| 9 | ||
| 10 | import { binaryFacts, FACTS_HEAD } from "../../web/render/binary-facts.ts"; | |
| 11 | import { fileKind, HEAD_BYTES } from "../../web/render/kinds.ts"; | |
| 12 | ||
| 13 | export function cadView(path: string, body: Uint8Array, size: number, formatSize: (n: number) => string): string | null { | |
| 14 | if (fileKind(path, body.subarray(0, HEAD_BYTES)) !== "cad") return null; | |
| 15 | const ext = path.slice(path.lastIndexOf(".") + 1).toLowerCase(); | |
| 16 | const facts = binaryFacts(ext, body); | |
| 17 | if (facts === undefined) return null; | |
| 18 | return [ | |
| 19 | `Kind: ${facts.format}`, | |
| 20 | `Version: ${facts.version ?? "unknown"}`, | |
| 21 | `Size: ${formatSize(size)}`, | |
| 22 | facts.note ?? `The forge cannot draw this format; it reads only the first ${FACTS_HEAD} bytes.`, | |
| 23 | ].join("\n"); | |
| 24 | } |
src/web/render/binary-facts.ts added +88
| @@ -0,0 +1,88 @@ | ||
| 1 | /** | |
| 2 | * What a CAD or 3D source says of itself in its first bytes: `.dwg` (AutoCAD), `.blend` (Blender) | |
| 3 | * and `.fbx` (Autodesk binary FBX). The forge cannot draw them, but it can name them: format, | |
| 4 | * version and size, not raw bytes. Plain data in, plain data out; nothing past `FACTS_HEAD` bytes | |
| 5 | * is looked at, and a name never decides alone (the magic must be there). | |
| 6 | */ | |
| 7 | ||
| 8 | /** The most of a file this module reads. */ | |
| 9 | export const FACTS_HEAD = 64; | |
| 10 | ||
| 11 | export interface BinaryFacts { | |
| 12 | /** The format as a person says it: `AutoCAD drawing`. */ | |
| 13 | readonly format: string; | |
| 14 | /** `AutoCAD 2018`, `4.4`, `7.4`; undefined when the header is cut before it. */ | |
| 15 | readonly version?: string; | |
| 16 | /** Said when something the header should hold is missing or unknown. */ | |
| 17 | readonly note?: string; | |
| 18 | } | |
| 19 | ||
| 20 | const text = (head: Uint8Array, at: number, n: number): string => { | |
| 21 | let s = ""; | |
| 22 | for (let i = at; i < Math.min(at + n, head.length, FACTS_HEAD); i++) s += String.fromCharCode(head[i]!); | |
| 23 | return s; | |
| 24 | }; | |
| 25 | ||
| 26 | /** DWG release codes, `AC` then four digits, as AutoCAD names them. */ | |
| 27 | const DWG: Readonly<Record<string, string>> = { | |
| 28 | AC1006: "R10", | |
| 29 | AC1009: "R11/R12", | |
| 30 | AC1012: "R13", | |
| 31 | AC1014: "R14", | |
| 32 | AC1015: "2000", | |
| 33 | AC1018: "2004", | |
| 34 | AC1021: "2007", | |
| 35 | AC1024: "2010", | |
| 36 | AC1027: "2013", | |
| 37 | AC1032: "2018", | |
| 38 | }; | |
| 39 | ||
| 40 | function dwg(head: Uint8Array): BinaryFacts | undefined { | |
| 41 | const code = text(head, 0, 6); | |
| 42 | if (!/^AC10\d\d$/.test(code)) return undefined; | |
| 43 | const release = DWG[code]; | |
| 44 | return release === undefined | |
| 45 | ? { format: "AutoCAD drawing", version: code, note: "A release this forge has no name for." } | |
| 46 | : { format: "AutoCAD drawing", version: `AutoCAD ${release} (${code})` }; | |
| 47 | } | |
| 48 | ||
| 49 | /** `BLENDER` then, in the old header, `_`/`-` (4/8-byte pointers), `v`/`V` (byte order), `404`. */ | |
| 50 | function blend(head: Uint8Array): BinaryFacts | undefined { | |
| 51 | if (text(head, 0, 7) !== "BLENDER") return undefined; | |
| 52 | const format = "Blender file"; | |
| 53 | // Blender 5 and later: BLENDER, a header size ("17"), "-", a format ("01"), "v", then "0500". | |
| 54 | const modern = /^(\d\d)-(\d\d)v(\d{4})$/.exec(text(head, 7, 10)); | |
| 55 | if (modern !== null) { | |
| 56 | const n = Number(modern[3]); | |
| 57 | return { format, version: `${Math.floor(n / 100)}.${n % 100}` }; | |
| 58 | } | |
| 59 | const old = /^[_-][vV](\d)(\d\d)$/.exec(text(head, 7, 5)); | |
| 60 | if (old !== null) return { format, version: `${old[1]}.${Number(old[2])}` }; | |
| 61 | return head.length < 12 | |
| 62 | ? { format, note: "The header is cut short: no version." } | |
| 63 | : { format, note: "The version in the header is not one this forge reads." }; | |
| 64 | } | |
| 65 | ||
| 66 | const FBX_MAGIC = "Kaydara FBX Binary \0"; | |
| 67 | ||
| 68 | /** `Kaydara FBX Binary \0`, two bytes (`1A 00`), then the version as a little-endian number. */ | |
| 69 | function fbx(head: Uint8Array): BinaryFacts | undefined { | |
| 70 | if (text(head, 0, FBX_MAGIC.length) !== FBX_MAGIC) return undefined; | |
| 71 | const format = "FBX binary"; | |
| 72 | if (head.length < 27) return { format, note: "The header is cut short: no version." }; | |
| 73 | const n = (head[23]! | (head[24]! << 8) | (head[25]! << 16) | (head[26]! << 24)) >>> 0; | |
| 74 | return { format, version: `${Math.floor(n / 1000)}.${Math.floor((n % 1000) / 100)}` }; | |
| 75 | } | |
| 76 | ||
| 77 | const BY_EXTENSION: Readonly<Record<string, (head: Uint8Array) => BinaryFacts | undefined>> = { dwg, blend, fbx }; | |
| 78 | ||
| 79 | /** The extensions this module reads, for the registry. */ | |
| 80 | export const FACTS_EXTENSIONS: readonly string[] = Object.keys(BY_EXTENSION); | |
| 81 | ||
| 82 | /** | |
| 83 | * The facts a file's header gives, when the extension and the magic agree; undefined when the | |
| 84 | * bytes are not that format (a `.dwg` of something else is only binary). | |
| 85 | */ | |
| 86 | export function binaryFacts(ext: string, head: Uint8Array): BinaryFacts | undefined { | |
| 87 | return BY_EXTENSION[ext.toLowerCase()]?.(head.subarray(0, FACTS_HEAD)); | |
| 88 | } |
src/web/render/binary-view.ts added +30
| @@ -0,0 +1,30 @@ | ||
| 1 | /** | |
| 2 | * A CAD or 3D source on the blob page: what its header says (format, version) and its size, with | |
| 3 | * a download link. The forge does not draw these. Facts are in `binary-facts.ts`. | |
| 4 | */ | |
| 5 | ||
| 6 | import { html } from "../html.ts"; | |
| 7 | import { fileSize } from "../ui.ts"; | |
| 8 | import { binaryFacts, FACTS_HEAD } from "./binary-facts.ts"; | |
| 9 | import type { Rendered, RenderInput } from "./renderers.ts"; | |
| 10 | ||
| 11 | const row = (k: string, v: string) => html`<div class="status-row"><dt>${k}</dt><dd>${v}</dd></div>`; | |
| 12 | ||
| 13 | export function binaryView(input: RenderInput): Rendered { | |
| 14 | const name = input.path.slice(input.path.lastIndexOf("/") + 1); | |
| 15 | const ext = name.slice(name.lastIndexOf(".") + 1); | |
| 16 | const facts = binaryFacts(ext, input.body); | |
| 17 | if (facts === undefined) return { content: html``, lines: 0, fallback: `not a readable ${ext} file` }; | |
| 18 | return { | |
| 19 | lines: 0, | |
| 20 | summary: `${fileSize(input.size)} · ${facts.format}${facts.version === undefined ? "" : ` ${facts.version}`}`, | |
| 21 | content: html` | |
| 22 | <dl class="status-rows" aria-label="${`${name} facts`}"> | |
Design: the binary-facts class is a new component class with no entry in docs/design/areas/<area>.md. Document it there or drop the wrapper class. |
||
| 23 | ${row("Kind", facts.format)} | |
| 24 | ${row("Version", facts.version ?? "unknown")} | |
| 25 | ${row("Size", fileSize(input.size))} | |
| 26 | </dl> | |
| 27 | <p class="muted small">${facts.note ?? `The forge cannot draw this format; it reads only the first ${String(FACTS_HEAD)} bytes.`}</p> | |
| 28 | <p class="small"><a class="btn" href="${input.download}">Download</a></p>`, | |
| 29 | }; | |
outdated
Principle 1 and clean: this second muted sentence repeats what the facts already say. Keep one, and use FACTS_HEAD in place of String(64) so the number cannot drift. |
||
| 30 | } |
src/web/render/kinds.ts +9
| @@ -13,6 +13,8 @@ | ||
| 13 | 13 | * source view it always had. |
| 14 | 14 | */ |
| 15 | 15 | |
| 16 | import { binaryFacts, FACTS_EXTENSIONS } from "./binary-facts.ts"; | |
| 17 | ||
| 16 | 18 | export type FileKind = |
| 17 | 19 | | "image" |
| 18 | 20 | | "svg" |
| @@ -32,6 +34,7 @@ export type FileKind = | ||
| 32 | 34 | | "epub" |
| 33 | 35 | | "font" |
| 34 | 36 | | "psd" |
| 37 | | "cad" | |
| 35 | 38 | | "config" |
| 36 | 39 | | "markdown" |
| 37 | 40 | | "notebook" |
| @@ -117,6 +120,8 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = { | ||
| 117 | 120 | font: 20 * MB, |
| 118 | 121 | // Read whole up to 5 MB; above that the header alone gives the facts. |
| 119 | 122 | psd: 4_000 * MB, |
| 123 | // Only the first 64 bytes are read, whatever the size. | |
| 124 | cad: 4_000 * MB, | |
| 120 | 125 | config: 1 * MB, |
| 121 | 126 | markdown: 1 * MB, |
| 122 | 127 | notebook: 1 * MB, |
| @@ -492,6 +497,8 @@ export function fileKind(path: string, head: Uint8Array): FileKind { | ||
| 492 | 497 | const office = isOffice(path, head); |
| 493 | 498 | if (office !== undefined) return office; |
| 494 | 499 | if (isArchive(path, head)) return "archive"; |
| 500 | // A CAD or 3D source is named by its magic as well as its name, and has NULs in its head. | |
| 501 | if (binaryFacts(extension(path), head) !== undefined) return "cad"; | |
| 495 | 502 | if (looksBinary(head)) return "binary"; |
| 496 | 503 | if (isLcovName(path)) return "lcov"; |
| 497 | 504 | if (isEnvExample(path)) return "env"; |
| @@ -539,6 +546,7 @@ export function kindByName(path: string): FileKind { | ||
| 539 | 546 | if (isComposeName(path)) return "compose"; |
| 540 | 547 | if (ext === "xlsx") return "sheet"; |
| 541 | 548 | if (OFFICE[ext] !== undefined) return OFFICE[ext]!; |
| 549 | if (FACTS_EXTENSIONS.includes(ext)) return "cad"; | |
| 542 | 550 | return MEDIA[ext]?.kind ?? TEXTUAL[ext] ?? "text"; |
| 543 | 551 | } |
| 544 | 552 | |
| @@ -639,6 +647,7 @@ const BY_CONTENT: Readonly<Partial<Record<FileKind, string>>> = { | ||
| 639 | 647 | const SNIFFED_EXTENSIONS: Readonly<Partial<Record<FileKind, readonly string[]>>> = { |
| 640 | 648 | sheet: ["xlsx"], |
| 641 | 649 | archive: ["zip", "tar", "tgz"], |
| 650 | cad: [...FACTS_EXTENSIONS], | |
| 642 | 651 | }; |
| 643 | 652 | |
| 644 | 653 | /** What names a kind, for the guide: extensions and file names, each once, to show as code. */ |
src/web/render/renderers.ts +4 −2
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | import type { Html } from "../html.ts"; |
| 8 | 8 | import type { Blob } from "../../view/source.ts"; |
| 9 | 9 | import { archive } from "./archive-view.ts"; |
| 10 | import { binaryView } from "./binary-view.ts"; | |
| 10 | 11 | import { audio } from "./audio.ts"; |
| 11 | 12 | import { config } from "./config.ts"; |
| 12 | 13 | import { isFormConfig } from "./config-format.ts"; |
| @@ -188,6 +189,7 @@ export const RENDERERS: Partial<Readonly<Record<FileKind, Renderer>>> = { | ||
| 188 | 189 | epub, |
| 189 | 190 | font, |
| 190 | 191 | psd, |
| 192 | cad: binaryView, | |
| 191 | 193 | }; |
| 192 | 194 | |
| 193 | 195 | /** |
| @@ -232,7 +234,7 @@ export function rendererFor( | ||
| 232 | 234 | const outlined = kind === "config" && blob.kind === "text" && blob.partial === true && isJsonPath(path); |
| 233 | 235 | if (blob.size > RENDER_CAP[kind] && !prefixed && !outlined) return undefined; |
| 234 | 236 | if (MEDIA_KINDS.has(kind) && !where.media) return undefined; |
| 235 | if (blob.kind === "too_large" && !MEDIA_KINDS.has(kind) && kind !== "psd") return undefined; | |
| 237 | if (blob.kind === "too_large" && !MEDIA_KINDS.has(kind) && kind !== "psd" && kind !== "cad") return undefined; | |
| 236 | 238 | // A kind may draw only some of its files: `config` draws JSON, YAML and |
| 237 | 239 | // TOML as a form, and leaves the rest (INI, .env …) their source. |
| 238 | 240 | // A log with no escape and no marked line keeps the source view, exactly as plain text. |
| @@ -245,7 +247,7 @@ export function rendererFor( | ||
| 245 | 247 | if (render === undefined) return undefined; |
| 246 | 248 | // A PSD over the read ceiling is drawn from its head: the header holds the facts. So is a |
| 247 | 249 | // photo's: its EXIF is in the first 64 KiB. |
| 248 | const fromHead = kind === "psd" || (kind === "image" && holdsExif(path)); | |
| 250 | const fromHead = kind === "psd" || kind === "cad" || (kind === "image" && holdsExif(path)); | |
| 249 | 251 | const body = blob.kind === "too_large" ? (fromHead ? (blob.head ?? NO_BYTES) : NO_BYTES) : blob.body; |
| 250 | 252 | return { render, body }; |
| 251 | 253 | } |
test/binary-facts.test.ts added +96
| @@ -0,0 +1,96 @@ | ||
| 1 | /** A .dwg, .blend or .fbx says what its header says: format, version, size; never raw bytes. */ | |
| 2 | ||
| 3 | import { describe, expect, test } from "bun:test"; | |
| 4 | import type { Blob } from "../src/view/source.ts"; | |
| 5 | import { binaryFacts, FACTS_HEAD } from "../src/web/render/binary-facts.ts"; | |
| 6 | import { fileKind, kindByName } from "../src/web/render/kinds.ts"; | |
| 7 | import { rendererFor } from "../src/web/render/renderers.ts"; | |
| 8 | ||
| 9 | const enc = (t: string): number[] => [...t].map((c) => c.charCodeAt(0)); | |
| 10 | const pad = (b: number[], n = 200): Uint8Array => new Uint8Array([...b, ...new Array(Math.max(0, n - b.length)).fill(0)]); | |
| 11 | ||
| 12 | const DWG = pad(enc("AC1032")); | |
| 13 | const BLEND = pad(enc("BLENDER-v404")); | |
| 14 | const BLEND5 = pad(enc("BLENDER17-01v0500")); | |
| 15 | const FBX = pad([...enc("Kaydara FBX Binary "), 0, 0x1a, 0, 0x28, 0x1c, 0, 0]); // 7208 | |
| 16 | const FBX74 = pad([...enc("Kaydara FBX Binary "), 0, 0x1a, 0, 0xe8, 0x1c, 0, 0]); // 7400 | |
| 17 | ||
| 18 | const input = (path: string, body: Uint8Array, size = body.length) => ({ | |
| 19 | path, | |
| 20 | size, | |
| 21 | body, | |
| 22 | media: "/m", | |
| 23 | download: "/d?x=1", | |
| 24 | }); | |
| 25 | const draw = (path: string, body: Uint8Array, size?: number) => { | |
| 26 | const r = rendererFor(path, { kind: "binary", body, size: size ?? body.length } satisfies Blob)!; | |
| 27 | return r.render(input(path, r.body, size ?? body.length)); | |
| 28 | }; | |
| 29 | ||
| 30 | describe("header facts", () => { | |
| 31 | test("dwg: AC10xx names the AutoCAD release", () => { | |
| 32 | expect(binaryFacts("dwg", DWG)).toEqual({ format: "AutoCAD drawing", version: "AutoCAD 2018 (AC1032)" }); | |
| 33 | expect(binaryFacts("dwg", pad(enc("AC1015")))?.version).toBe("AutoCAD 2000 (AC1015)"); | |
| 34 | expect(binaryFacts("dwg", pad(enc("AC1099")))?.note).toBeDefined(); | |
| 35 | }); | |
| 36 | test("blend: BLENDER and its version, old header and new", () => { | |
| 37 | expect(binaryFacts("blend", BLEND)).toEqual({ format: "Blender file", version: "4.4" }); | |
| 38 | expect(binaryFacts("blend", pad(enc("BLENDER_v279")))?.version).toBe("2.79"); | |
| 39 | expect(binaryFacts("blend", BLEND5)?.version).toBe("5.0"); | |
| 40 | }); | |
| 41 | test("fbx: Kaydara binary and its version", () => { | |
| 42 | expect(binaryFacts("fbx", FBX)).toEqual({ format: "FBX binary", version: "7.2" }); | |
| 43 | expect(binaryFacts("fbx", FBX74)?.version).toBe("7.4"); | |
| 44 | }); | |
| 45 | test("a truncated header is named, with no version, and never throws", () => { | |
| 46 | expect(binaryFacts("blend", new Uint8Array(enc("BLENDER-v")))).toEqual({ | |
| 47 | format: "Blender file", | |
| 48 | note: "The header is cut short: no version.", | |
| 49 | }); | |
| 50 | expect(binaryFacts("fbx", FBX.subarray(0, 24))?.version).toBeUndefined(); | |
| 51 | expect(binaryFacts("fbx", FBX.subarray(0, 24))?.note).toContain("cut short"); | |
| 52 | expect(binaryFacts("dwg", new Uint8Array(enc("AC10")))).toBeUndefined(); | |
| 53 | expect(binaryFacts("dwg", new Uint8Array(0))).toBeUndefined(); | |
| 54 | }); | |
| 55 | test("the magic decides: a name alone, or the wrong extension, says nothing", () => { | |
| 56 | expect(binaryFacts("dwg", pad(enc("hello world")))).toBeUndefined(); | |
| 57 | expect(binaryFacts("blend", DWG)).toBeUndefined(); | |
| 58 | expect(binaryFacts("png", DWG)).toBeUndefined(); | |
| 59 | expect(fileKind("a.dwg", pad([1, 0, 2]))).toBe("binary"); | |
| 60 | expect(fileKind("a.dwg", DWG)).toBe("cad"); | |
| 61 | expect(fileKind("a.blend", BLEND)).toBe("cad"); | |
| 62 | expect(fileKind("a.fbx", FBX)).toBe("cad"); | |
| 63 | expect(fileKind("a.txt", DWG)).toBe("binary"); | |
| 64 | expect(kindByName("a.FBX")).toBe("cad"); | |
| 65 | }); | |
| 66 | test("nothing past the first 64 bytes is read", () => { | |
| 67 | const late = new Uint8Array(200); | |
| 68 | late.set(enc("AC1032"), FACTS_HEAD); | |
| 69 | expect(binaryFacts("dwg", late)).toBeUndefined(); | |
| 70 | }); | |
| 71 | }); | |
| 72 | ||
| 73 | describe("the page", () => { | |
| 74 | test("shows kind, version, size and a download link", () => { | |
| 75 | const out = draw("cad/part.dwg", DWG); | |
| 76 | const page = String(out.content); | |
| 77 | expect(page).toContain("AutoCAD drawing"); | |
| 78 | expect(page).toContain("AutoCAD 2018 (AC1032)"); | |
| 79 | expect(page).toContain('href="/d?x=1"'); | |
| 80 | expect(out.summary).toContain("AutoCAD drawing"); | |
| 81 | expect(String(draw("scene.blend", BLEND).content)).toContain("4.4"); | |
| 82 | expect(String(draw("rig.fbx", FBX).content)).toContain("7.2"); | |
| 83 | }); | |
| 84 | test("a truncated file shows its kind and says the header is cut", () => { | |
| 85 | const page = String(draw("scene.blend", new Uint8Array(enc("BLENDER-v"))).content); | |
| 86 | expect(page).toContain("Blender file"); | |
| 87 | expect(page).toContain("cut short"); | |
| 88 | }); | |
| 89 | test("a file over the read ceiling is drawn from its head alone", () => { | |
| 90 | const r = rendererFor("big.fbx", { kind: "too_large", size: 900_000_000, head: FBX } satisfies Blob)!; | |
| 91 | expect(r.body.length).toBe(FBX.length); | |
| 92 | const out = r.render(input("big.fbx", r.body, 900_000_000)); | |
| 93 | expect(String(out.content)).toContain("7.2"); | |
| 94 | expect(String(out.content)).toContain("858.3 MB"); | |
| 95 | }); | |
| 96 | }); |
test/fuzz-renderers.test.ts +2
| @@ -344,6 +344,7 @@ export const EXCLUDED: Readonly<Record<string, string>> = { | ||
| 344 | 344 | layers: "a zip of images (ora); needs a fixture to splice into", |
| 345 | 345 | font: "binary font tables; needs a fixture to splice into", |
| 346 | 346 | psd: "binary Photoshop layers; needs a fixture to splice into", |
| 347 | cad: "binary CAD or 3D header; only the format, version and size are read, never drawn as bytes", | |
| 347 | 348 | }; |
| 348 | 349 | |
| 349 | 350 | /** |
| @@ -599,6 +600,7 @@ export const RAW_PATHS: Readonly<Record<string, string>> = { | ||
| 599 | 600 | layers: "a.ora", |
| 600 | 601 | font: "a.ttf", |
| 601 | 602 | psd: "a.psd", |
| 603 | cad: "a.dwg", | |
| 602 | 604 | text: "notes.txt", |
| 603 | 605 | binary: "a.bin", |
| 604 | 606 | }; |
test/workspace-cad.test.ts added +21
| @@ -0,0 +1,21 @@ | ||
| 1 | /** The terminal workspace says what a .dwg, .blend or .fbx header says, not a hex dump. */ | |
| 2 | ||
| 3 | import { expect, test } from "bun:test"; | |
| 4 | import { cadView } from "../src/tui/screens/workspace_cad.ts"; | |
| 5 | ||
| 6 | const enc = (t: string): number[] => [...t].map((c) => c.charCodeAt(0)); | |
| 7 | const pad = (b: number[], n = 200) => new Uint8Array([...b, ...new Array(n - b.length).fill(0)]); | |
| 8 | const size = (n: number) => `${n} B`; | |
| 9 | ||
| 10 | test("a dwg and a blend answer kind, version and size", () => { | |
| 11 | const dwg = cadView("a/plan.dwg", pad(enc("AC1032")), 200, size)!; | |
| 12 | expect(dwg).toContain("Kind: AutoCAD drawing"); | |
| 13 | expect(dwg).toContain("Version: AutoCAD 2018 (AC1032)"); | |
| 14 | expect(dwg).toContain("Size: 200 B"); | |
| 15 | expect(cadView("m.blend", pad(enc("BLENDER-v404")), 200, size)).toContain("Version: 4.4"); | |
| 16 | }); | |
| 17 | ||
| 18 | test("a name alone is not enough, and other files are not its", () => { | |
| 19 | expect(cadView("a.dwg", pad(enc("hello")), 200, size)).toBeNull(); | |
| 20 | expect(cadView("a.txt", pad(enc("AC1032")), 200, size)).toBeNull(); | |
| 21 | }); |
new above
offline
Both doors: the page is documented, the terminal is not offered. Add a src/tui/ screen and say here how to reach it.