This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
pdf.go
89 lines (72 loc) · 1.72 KB
/
pdf.go
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
86
87
88
89
package pdf
import (
"fmt"
"image"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime/debug"
"time"
"github.com/beevik/etree"
"github.com/geek1011/BookBrowser/formats"
"github.com/geek1011/BookBrowser/models"
"github.com/geek1011/BookBrowser/modules/util"
)
func indexer(filename string) (book *models.Book, cover image.Image, err error) {
defer func() {
if r := recover(); r != nil {
book = nil
cover = nil
err = fmt.Errorf("Unknown error parsing book. Skipping. Error: %s", r)
}
}()
var title string
var author string
var modtime time.Time
if file, err := os.Stat(filename); err == nil {
modtime = file.ModTime()
}
c, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
s := string(c)
c = []byte{}
s = util.StringBetween(s, "<?xpacket begin", "</x:xmpmeta>")
s = util.StringAfter(s, ">")
xmp := etree.NewDocument()
err = xmp.ReadFromString(s)
if err != nil {
return nil, nil, err
}
title = filepath.Base(filename)
for _, e := range xmp.FindElements("//format") {
// Make sure it is a pdf, not another piece of embedded RDF metadata
if e.Text() != "application/pdf" {
s = ""
debug.FreeOSMemory()
return models.NewBook(title, author, "", "", 0, "", filename, false, modtime, "pdf"), nil, nil
}
break
}
for _, e := range xmp.FindElements("//title/Alt/li") {
title = e.Text()
break
}
for _, e := range xmp.FindElements("//creator/Seq/li") {
author = e.Text()
break
}
s = ""
debug.FreeOSMemory()
var coverTmp image.Image
return models.NewBook(title, author, "", "", 0, "", filename, false, modtime, "pdf"), coverTmp, nil
}
func init() {
formats.RegisterFormat(&formats.Format{
Glob: "**/*.pdf",
Extension: ".pdf",
Indexer: indexer,
})
}