feat(backend): cover extraction for cbz and epub (opf cover-image/meta)
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package bookfile
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCBZCoverIsFirstPage(t *testing.T) {
|
||||
buf := &bytes.Buffer{}
|
||||
zw := zip.NewWriter(buf)
|
||||
w, _ := zw.Create("02.jpg")
|
||||
w.Write([]byte("second"))
|
||||
w, _ = zw.Create("01.jpg")
|
||||
w.Write([]byte("first"))
|
||||
zw.Close()
|
||||
r := bytes.NewReader(buf.Bytes())
|
||||
img, ext, err := CBZCover(r, int64(r.Len()))
|
||||
if err != nil || string(img) != "first" || ext != ".jpg" {
|
||||
t.Fatalf("%q %q %v", img, ext, err)
|
||||
}
|
||||
}
|
||||
|
||||
func zipEntries(t *testing.T, kv map[string]string) *bytes.Reader {
|
||||
t.Helper()
|
||||
buf := &bytes.Buffer{}
|
||||
zw := zip.NewWriter(buf)
|
||||
for name, data := range kv {
|
||||
w, _ := zw.Create(name)
|
||||
w.Write([]byte(data))
|
||||
}
|
||||
zw.Close()
|
||||
return bytes.NewReader(buf.Bytes())
|
||||
}
|
||||
|
||||
const containerXML = `<?xml version="1.0"?><container><rootfiles><rootfile full-path="OEBPS/content.opf"/></rootfiles></container>`
|
||||
|
||||
func opf(manifest, spine string) string {
|
||||
return `<?xml version="1.0"?><package xmlns="http://www.idpf.org/2007/opf"><metadata>` +
|
||||
spine + `</metadata><manifest>` + manifest + `</manifest></package>`
|
||||
}
|
||||
|
||||
func TestEPUBCoverProperties(t *testing.T) {
|
||||
r := zipEntries(t, map[string]string{
|
||||
"META-INF/container.xml": containerXML,
|
||||
"OEBPS/content.opf": opf(`<item id="c" href="img/cover%20one.png" properties="cover-image" media-type="image/png"/><item id="t" href="t.xhtml" media-type="application/xhtml+xml"/>`, ""),
|
||||
"OEBPS/img/cover one.png": "PNGDATA",
|
||||
})
|
||||
img, ext, err := EPUBCover(r, int64(r.Len()))
|
||||
if err != nil || string(img) != "PNGDATA" || ext != ".png" {
|
||||
t.Fatalf("%q %q %v", img, ext, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEPUBCoverMetaID(t *testing.T) {
|
||||
r := zipEntries(t, map[string]string{
|
||||
"META-INF/container.xml": containerXML,
|
||||
"OEBPS/content.opf": opf(`<item id="cv" href="art.jpg" media-type="image/jpeg"/><item id="t" href="t.xhtml" media-type="application/xhtml+xml"/>`, `<meta name="cover" content="cv"/>`),
|
||||
"OEBPS/art.jpg": "JPGDATA",
|
||||
})
|
||||
img, _, err := EPUBCover(r, int64(r.Len()))
|
||||
if err != nil || string(img) != "JPGDATA" {
|
||||
t.Fatalf("%q %v", img, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEPUBNoCover(t *testing.T) {
|
||||
r := zipEntries(t, map[string]string{
|
||||
"META-INF/container.xml": containerXML,
|
||||
"OEBPS/content.opf": opf(`<item id="t" href="t.xhtml" media-type="application/xhtml+xml"/>`, ""),
|
||||
})
|
||||
if _, _, err := EPUBCover(r, int64(r.Len())); err == nil {
|
||||
t.Fatal("want no-cover error")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user