From 9de0946e53348d063180b2af87008a51c4fd9dcc Mon Sep 17 00:00:00 2001 From: linus-liu-web <395141741@qq.com> Date: Thu, 8 Oct 2026 01:21:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(docparser):=20anydoc=20=E5=86=85=E5=B5=8C?= =?UTF-8?q?=20EMF/WMF=20=E5=9B=BE=E7=89=87=E7=9A=84=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E5=90=8D=E4=B8=8E=20Rust=20=E4=BE=A7=E5=AF=B9=E9=BD=90=20(#393?= =?UTF-8?q?8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit anydoc 把内嵌图片就地渲染成 `![alt](images/image-N)`,`` 来自 asset_links.rs::extension_for;ImageResolver 再拿这个字符串去一张 map 里查, 键是 anydoc.ImageDir + Asset.Name,而 Name 由 backend_cgo.go::extensionFor 拼出。 查表是精确匹配,两边必须逐字符一致。 EMF/WMF 上两边并不一致:anydoc 对这两类 part 硬编码产出 image/emf 与 image/wmf(shared/assets.rs::media_type_for、shared/officeart.rs),Rust 表 落到 `.bin`,而 Go 侧去查平台 MIME 注册表——在装了 media-types 的 Debian 镜像上拿到 `.emf`/`.wmf`。链接与 ref 于是永远对不上,saveReferencedImage 找不到条目,图片字节从不落盘,且没有任何日志。 - asset_links.rs::extension_for 补 image/emf 与 image/wmf - backend_cgo.go::extensionFor 补同样两项,并改成纯查表 去掉 mime.ExtensionsByType 是有意的:让扩展名取决于宿主的 /etc/mime.types 正是这个 bug 的成因,何况这张表本来就要和 Rust 的静态表逐字符对齐。未知 类型两边都落到 .bin,仍满足「不写出无扩展名 blob」的原意。 新增 TestEmbeddedVectorImageLinksResolve:在内存里构造一个含一张 EMF 与 一张 WMF 的最小 docx,转换后要求每一条 Markdown 图片引用都能在 resolver 建的 ImageRef 表里命中,并固定扩展名为 .emf/.wmf(避免两边一起退化成 .bin 也能通过)。 Co-authored-by: linus-liu-web --- .../docparser/anydoc/backend_cgo.go | 27 ++- .../docparser/anydoc_vector_image_test.go | 228 ++++++++++++++++++ third_party/anydoc-go/src/asset_links.rs | 16 ++ 3 files changed, 264 insertions(+), 7 deletions(-) create mode 100644 internal/infrastructure/docparser/anydoc_vector_image_test.go diff --git a/internal/infrastructure/docparser/anydoc/backend_cgo.go b/internal/infrastructure/docparser/anydoc/backend_cgo.go index 625c2ebd..c8e10e88 100644 --- a/internal/infrastructure/docparser/anydoc/backend_cgo.go +++ b/internal/infrastructure/docparser/anydoc/backend_cgo.go @@ -4,7 +4,6 @@ package anydoc import ( "fmt" - "mime" "strings" upstream "github.com/firecrawl/anydoc/go" @@ -183,6 +182,22 @@ func inlineText(inlines []upstream.Inline) string { return strings.TrimSpace(text.String()) } +// extensionFor returns the extension that the Markdown link for an embedded +// image carries. +// +// This table is one half of a contract with the Rust serializer. The link it +// writes (`images/image-N`, see third_party/anydoc-go/src/asset_links.rs) +// has to equal the ImageRef.OriginalRef built from this name, because +// ImageResolver looks the link up in a map keyed by that exact string. The +// media types and extensions below must therefore match `extension_for` in +// asset_links.rs exactly. +// +// It is deliberately a pure table. Asking the platform MIME registry made the +// answer depend on the host's /etc/mime.types, which is how EMF/WMF ended up +// as ".emf"/".wmf" here and ".bin" on the Rust side: the link never matched +// the ref and the image bytes were never stored. Unknown types fall back to +// ".bin" on both sides, so downstream storage never writes an extension-less +// blob. func extensionFor(mediaType string) string { switch strings.ToLower(strings.TrimSpace(mediaType)) { case "image/jpeg", "image/jpg": @@ -199,12 +214,10 @@ func extensionFor(mediaType string) string { return ".tiff" case "image/svg+xml": return ".svg" - } - // Anything else (EMF/WMF drawings, unknown object payloads) keeps - // whatever extension the media type registry knows, and .bin otherwise, - // so downstream storage never writes an extension-less blob. - if exts, err := mime.ExtensionsByType(mediaType); err == nil && len(exts) > 0 { - return exts[0] + case "image/emf": + return ".emf" + case "image/wmf": + return ".wmf" } return ".bin" } diff --git a/internal/infrastructure/docparser/anydoc_vector_image_test.go b/internal/infrastructure/docparser/anydoc_vector_image_test.go new file mode 100644 index 00000000..6eee7efd --- /dev/null +++ b/internal/infrastructure/docparser/anydoc_vector_image_test.go @@ -0,0 +1,228 @@ +//go:build anydoc && cgo + +package docparser + +import ( + "archive/zip" + "bytes" + "encoding/binary" + "path" + "sort" + "strconv" + "testing" + + "github.com/Tencent/WeKnora/internal/infrastructure/docparser/anydoc" + "github.com/Tencent/WeKnora/internal/types" +) + +// TestEmbeddedVectorImageLinksResolve guards the string contract between +// anydoc's Rust serializer and this package's image resolver. +// +// anydoc renders an embedded image in place as `![alt](images/image-N)`, +// where `` comes from `asset_links.rs::extension_for(asset.media_type)`. +// ImageResolver looks that exact string up in a map keyed by +// `anydoc.ImageDir + Asset.Name`, and `Asset.Name` is built from +// `backend_cgo.go::extensionFor(asset.MediaType)`. Nothing normalises the two, +// so any disagreement about the extension makes the lookup miss and the image +// bytes are never stored, silently. +// +// EMF and WMF are what Word, Excel and PowerPoint use for pasted charts, +// equations and Visio drawings. The two sides used to disagree about them: the +// Rust table fell through to ".bin" while the Go side asked the platform MIME +// registry and got ".emf"/".wmf". This test converts a document carrying one of +// each and requires every Markdown image reference to resolve. +func TestEmbeddedVectorImageLinksResolve(t *testing.T) { + result, err := anydoc.Convert(vectorDocx(t), anydoc.Options{Format: "docx", WithAssets: true}) + if err != nil { + t.Fatalf("Convert: %v", err) + } + if result.AssetsError != nil { + t.Fatalf("image extraction failed: %v", result.AssetsError) + } + + var mediaTypes []string + for _, asset := range result.Assets { + mediaTypes = append(mediaTypes, asset.MediaType) + } + sort.Strings(mediaTypes) + if want := []string{"image/emf", "image/wmf"}; !equalStrings(mediaTypes, want) { + t.Fatalf("extracted media types = %v, want %v", mediaTypes, want) + } + + // Exactly what ImageResolver.ResolveAndStore builds before looking up the + // Markdown reference. + refMap := map[string]types.ImageRef{} + for _, ref := range imageRefsFromAssets(result.Assets) { + refMap[ref.OriginalRef] = ref + } + + references := scanMarkdownImageTargets(result.Markdown) + if len(references) != len(result.Assets) { + t.Fatalf("markdown carries %d image references, want %d:\n%s", + len(references), len(result.Assets), result.Markdown) + } + for _, reference := range references { + raw := result.Markdown[reference.TargetStart:reference.TargetEnd] + if _, _, _, ok := splitMarkdownImageTarget(raw, refMap); !ok { + t.Errorf("markdown reference %q matches no ImageRef, so the image would never be stored (lookup keys: %v)", + raw, sortedRefKeys(refMap)) + } + } + + // Agreeing on ".bin" for everything would satisfy the loop above while + // still discarding the format, so pin the extensions too. Asset order + // follows the archive rather than the document, so compare the set. + var extensions []string + for key := range refMap { + extensions = append(extensions, path.Ext(key)) + } + sort.Strings(extensions) + if want := []string{".emf", ".wmf"}; !equalStrings(extensions, want) { + t.Errorf("image reference extensions = %v, want %v (lookup keys: %v)", + extensions, want, sortedRefKeys(refMap)) + } +} + +func equalStrings(got, want []string) bool { + if len(got) != len(want) { + return false + } + for i := range got { + if got[i] != want[i] { + return false + } + } + return true +} + +func sortedRefKeys(refMap map[string]types.ImageRef) []string { + keys := make([]string, 0, len(refMap)) + for key := range refMap { + keys = append(keys, key) + } + sort.Strings(keys) + return keys +} + +// vectorDocx builds a minimal .docx holding one EMF and one WMF drawing. The +// payloads only need a recognisable header: anydoc addresses an embedded part +// through its relationship target and extension, not by decoding the image. +func vectorDocx(t *testing.T) []byte { + t.Helper() + + const ( + relsNS = "http://schemas.openxmlformats.org/package/2006/relationships" + wNS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" + rNS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships" + aNS = "http://schemas.openxmlformats.org/drawingml/2006/main" + wpNS = "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" + picNS = "http://schemas.openxmlformats.org/drawingml/2006/picture" + ) + + files := []struct { + name string + body string + }{ + {"[Content_Types].xml", ` + + + + + + +`}, + {"_rels/.rels", ` +`}, + {"word/_rels/document.xml.rels", ` + + + +`}, + {"word/document.xml", ` + +vector images` + + drawingParagraph(picNS, aNS, wpNS, rNS, "rId1", "chart.emf", 1) + + drawingParagraph(picNS, aNS, wpNS, rNS, "rId2", "equation.wmf", 2) + + ``}, + } + + var buf bytes.Buffer + zw := zip.NewWriter(&buf) + for _, file := range files { + writer, err := zw.Create(file.name) + if err != nil { + t.Fatalf("zip create %s: %v", file.name, err) + } + if _, err := writer.Write([]byte(file.body)); err != nil { + t.Fatalf("zip write %s: %v", file.name, err) + } + } + for name, body := range map[string][]byte{ + "word/media/image1.emf": emfHeader(), + "word/media/image2.wmf": wmfPlaceableHeader(), + } { + writer, err := zw.Create(name) + if err != nil { + t.Fatalf("zip create %s: %v", name, err) + } + if _, err := writer.Write(body); err != nil { + t.Fatalf("zip write %s: %v", name, err) + } + } + if err := zw.Close(); err != nil { + t.Fatalf("zip close: %v", err) + } + return buf.Bytes() +} + +func drawingParagraph(picNS, aNS, wpNS, rNS, relID, name string, id int) string { + number := strconv.Itoa(id) + return ` + + + + + + + + + +` +} + +// emfHeader is a structurally valid EMR_HEADER record (MS-EMF 2.3.4.2). +func emfHeader() []byte { + buf := make([]byte, 0, 88) + buf = binary.LittleEndian.AppendUint32(buf, 1) // iType = EMR_HEADER + buf = binary.LittleEndian.AppendUint32(buf, 88) + for i := 0; i < 8; i++ { // rclBounds, rclFrame + buf = binary.LittleEndian.AppendUint32(buf, 914400) + } + buf = binary.LittleEndian.AppendUint32(buf, 0x464D4520) // dSignature = ' EMF' + buf = binary.LittleEndian.AppendUint32(buf, 0x00010000) // nVersion + buf = binary.LittleEndian.AppendUint32(buf, 88) // nBytes + buf = binary.LittleEndian.AppendUint32(buf, 16) // nRecords + buf = binary.LittleEndian.AppendUint16(buf, 0) // nHandles + buf = binary.LittleEndian.AppendUint16(buf, 0) // sReserved + for i := 0; i < 3; i++ { // nDescription, offDescription, nPalEntries + buf = binary.LittleEndian.AppendUint32(buf, 0) + } + for i := 0; i < 4; i++ { // szlDevice, szlMillimeters + buf = binary.LittleEndian.AppendUint32(buf, 0) + } + return append(buf, []byte("EMF-PAYLOAD")...) +} + +// wmfPlaceableHeader is a placeable WMF header (MS-WMF 2.3.2.1). +func wmfPlaceableHeader() []byte { + buf := make([]byte, 0, 22) + buf = binary.LittleEndian.AppendUint32(buf, 0x9AC6CDD7) // key + buf = binary.LittleEndian.AppendUint16(buf, 0) // hmf + for i := 0; i < 4; i++ { // bbox + buf = binary.LittleEndian.AppendUint16(buf, 100) + } + buf = binary.LittleEndian.AppendUint16(buf, 96) // inch + buf = binary.LittleEndian.AppendUint32(buf, 0) // reserved + buf = binary.LittleEndian.AppendUint16(buf, 96) // checksum + return append(buf, []byte("WMF-PAYLOAD")...) +} diff --git a/third_party/anydoc-go/src/asset_links.rs b/third_party/anydoc-go/src/asset_links.rs index 629da715..771b54d5 100644 --- a/third_party/anydoc-go/src/asset_links.rs +++ b/third_party/anydoc-go/src/asset_links.rs @@ -69,6 +69,15 @@ fn rewrite_inlines(inlines: &mut [Inline], urls: &[Option]) { } } +/// Markdown file extension for one asset media type. +/// +/// This table is one half of a contract with the Go side. The Markdown link +/// written here (`images/image-N`) is looked up by +/// `internal/infrastructure/docparser/image_resolver.go` in a map keyed by the +/// `ImageRef.OriginalRef` that `internal/infrastructure/docparser/anydoc/ +/// backend_cgo.go` builds from the same media type. The two must agree +/// character for character or the image is dropped, so `extensionFor` there +/// lists exactly the same media types with exactly the same extensions. fn extension_for(media_type: &str) -> &'static str { match media_type.to_ascii_lowercase().as_str() { "image/jpeg" | "image/jpg" => ".jpg", @@ -78,6 +87,13 @@ fn extension_for(media_type: &str) -> &'static str { "image/bmp" => ".bmp", "image/tiff" => ".tiff", "image/svg+xml" => ".svg", + // Vector metafiles. `shared::assets::media_type_for` and the OfficeArt + // blip decoder both emit these for `.emf`/`.wmf` parts. They used to + // fall through to ".bin" here while the Go side asked the platform MIME + // registry and got ".emf"/".wmf", so the link never matched the ref and + // the image never reached storage. + "image/emf" => ".emf", + "image/wmf" => ".wmf", _ => ".bin", } }