-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors_example_test.go
More file actions
85 lines (74 loc) · 2.1 KB
/
Copy patherrors_example_test.go
File metadata and controls
85 lines (74 loc) · 2.1 KB
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
// SPDX-License-Identifier: Apache-2.0
package epub
import (
"archive/zip"
"bytes"
"errors"
"fmt"
)
func ExampleDecode_structuredErrors() {
epubData := minimalEPUBMissingAssetExample()
_, err := Decode(bytes.NewReader(epubData), int64(len(epubData)))
if err == nil {
fmt.Println("ok")
return
}
var missing *ManifestPhysicalMissingError
if errors.As(err, &missing) {
fmt.Println("missing", missing.Href)
}
// Output:
// missing item/image/p-001.jpg
}
func minimalEPUBMissingAssetExample() []byte {
container := `<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="item/standard.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>`
opf := `<?xml version="1.0" encoding="UTF-8"?>
<package version="3.0" xmlns="http://www.idpf.org/2007/opf">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>Test Book</dc:title>
</metadata>
<manifest>
<item id="xhtml-1" href="xhtml/p-001.xhtml" media-type="application/xhtml+xml"/>
<item id="p-001" href="image/p-001.jpg" media-type="image/jpeg"/>
</manifest>
<spine page-progression-direction="rtl">
<itemref idref="xhtml-1" properties="page-spread-right"/>
</spine>
</package>`
xhtml := `<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>p1</title></head><body><p>hello</p></body></html>`
var buf bytes.Buffer
zw := zip.NewWriter(&buf)
w, err := zw.CreateHeader(&zip.FileHeader{Name: "mimetype", Method: zip.Store})
if err != nil {
panic(err)
}
if _, err := w.Write([]byte(mimeTypeValue)); err != nil {
panic(err)
}
for _, file := range []struct {
name string
body string
}{
{name: "META-INF/container.xml", body: container},
{name: "item/standard.opf", body: opf},
{name: "item/xhtml/p-001.xhtml", body: xhtml},
} {
w, err := zw.Create(file.name)
if err != nil {
panic(err)
}
if _, err := w.Write([]byte(file.body)); err != nil {
panic(err)
}
}
if err := zw.Close(); err != nil {
panic(err)
}
return buf.Bytes()
}