fix(docparser): anydoc 内嵌 EMF/WMF 图片的扩展名与 Rust 侧对齐 (#3938)
anydoc 把内嵌图片就地渲染成 ``,`<ext>` 来自 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 <linus-liu-web@users.noreply.github.com>main
parent
c9723b1198
commit
9de0946e53
|
|
@ -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<ext>`, 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"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 ``,
|
||||
// where `<ext>` 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", `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
|
||||
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
|
||||
<Default Extension="xml" ContentType="application/xml"/>
|
||||
<Default Extension="emf" ContentType="image/x-emf"/>
|
||||
<Default Extension="wmf" ContentType="image/x-wmf"/>
|
||||
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
|
||||
</Types>`},
|
||||
{"_rels/.rels", `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<Relationships xmlns="` + relsNS + `"><Relationship Id="rId1" Type="` + rNS + `/officeDocument" Target="word/document.xml"/></Relationships>`},
|
||||
{"word/_rels/document.xml.rels", `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<Relationships xmlns="` + relsNS + `">
|
||||
<Relationship Id="rId1" Type="` + rNS + `/image" Target="media/image1.emf"/>
|
||||
<Relationship Id="rId2" Type="` + rNS + `/image" Target="media/image2.wmf"/>
|
||||
</Relationships>`},
|
||||
{"word/document.xml", `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<w:document xmlns:w="` + wNS + `" xmlns:r="` + rNS + `" xmlns:a="` + aNS + `" xmlns:wp="` + wpNS + `">
|
||||
<w:body><w:p><w:r><w:t>vector images</w:t></w:r></w:p>` +
|
||||
drawingParagraph(picNS, aNS, wpNS, rNS, "rId1", "chart.emf", 1) +
|
||||
drawingParagraph(picNS, aNS, wpNS, rNS, "rId2", "equation.wmf", 2) +
|
||||
`</w:body></w:document>`},
|
||||
}
|
||||
|
||||
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 `<w:p><w:r><w:drawing><wp:inline>
|
||||
<wp:extent cx="914400" cy="914400"/>
|
||||
<wp:docPr id="` + number + `" name="` + name + `" descr="` + name + `"/>
|
||||
<a:graphic><a:graphicData uri="` + picNS + `">
|
||||
<pic:pic xmlns:pic="` + picNS + `">
|
||||
<pic:nvPicPr><pic:cNvPr id="` + number + `" name="` + name + `"/><pic:cNvPicPr/></pic:nvPicPr>
|
||||
<pic:blipFill><a:blip r:embed="` + relID + `"/><a:stretch><a:fillRect/></a:stretch></pic:blipFill>
|
||||
<pic:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="914400" cy="914400"/></a:xfrm>
|
||||
<a:prstGeom prst="rect"><a:avLst/></a:prstGeom></pic:spPr>
|
||||
</pic:pic></a:graphicData></a:graphic>
|
||||
</wp:inline></w:drawing></w:r></w:p>`
|
||||
}
|
||||
|
||||
// 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")...)
|
||||
}
|
||||
|
|
@ -69,6 +69,15 @@ fn rewrite_inlines(inlines: &mut [Inline], urls: &[Option<String>]) {
|
|||
}
|
||||
}
|
||||
|
||||
/// 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<ext>`) 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",
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue