128 lines
2.9 KiB
Go
128 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"git.gitorious.org/go-pkg/epubgo.git"
|
|
"io"
|
|
"io/ioutil"
|
|
"labix.org/v2/mgo/bson"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
func OpenBook(id bson.ObjectId, db *DB) (*epubgo.Epub, error) {
|
|
fs := db.GetFS(FS_BOOKS)
|
|
f, err := fs.OpenId(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
buff, err := ioutil.ReadAll(f)
|
|
reader := bytes.NewReader(buff)
|
|
|
|
return epubgo.Load(reader, int64(len(buff)))
|
|
}
|
|
|
|
func StoreNewFile(name string, file io.Reader, db *DB) (bson.ObjectId, int64, error) {
|
|
fs := db.GetFS(FS_BOOKS)
|
|
fw, err := fs.Create(name)
|
|
if err != nil {
|
|
return "", 0, err
|
|
}
|
|
defer fw.Close()
|
|
|
|
size, err := io.Copy(fw, file)
|
|
id, _ := fw.Id().(bson.ObjectId)
|
|
return id, size, err
|
|
}
|
|
|
|
func DeleteFile(id bson.ObjectId, db *DB) error {
|
|
fs := db.GetFS(FS_BOOKS)
|
|
return fs.RemoveId(id)
|
|
}
|
|
|
|
func DeleteCover(id bson.ObjectId, db *DB) error {
|
|
fs := db.GetFS(FS_IMGS)
|
|
return fs.RemoveId(id)
|
|
}
|
|
|
|
func DeleteBook(book Book, db *DB) {
|
|
if book.Cover != "" {
|
|
DeleteCover(book.Cover, db)
|
|
}
|
|
if book.CoverSmall != "" {
|
|
DeleteCover(book.CoverSmall, db)
|
|
}
|
|
DeleteFile(book.File, db)
|
|
}
|
|
|
|
func cleanStr(str string) string {
|
|
str = strings.Replace(str, "'", "'", -1)
|
|
exp, _ := regexp.Compile("&[^;]*;")
|
|
str = exp.ReplaceAllString(str, "")
|
|
exp, _ = regexp.Compile("[ ,]*$")
|
|
str = exp.ReplaceAllString(str, "")
|
|
return str
|
|
}
|
|
|
|
func parseAuthr(creator []string) []string {
|
|
exp1, _ := regexp.Compile("^(.*\\( *([^\\)]*) *\\))*$")
|
|
exp2, _ := regexp.Compile("^[^:]*: *(.*)$")
|
|
res := make([]string, len(creator))
|
|
for i, s := range creator {
|
|
auth := exp1.FindStringSubmatch(s)
|
|
if auth != nil {
|
|
res[i] = cleanStr(strings.Join(auth[2:], ", "))
|
|
} else {
|
|
auth := exp2.FindStringSubmatch(s)
|
|
if auth != nil {
|
|
res[i] = cleanStr(auth[1])
|
|
} else {
|
|
res[i] = cleanStr(s)
|
|
}
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
func parseDescription(description []string) string {
|
|
str := cleanStr(strings.Join(description, "\n"))
|
|
str = strings.Replace(str, "</p>", "\n", -1)
|
|
exp, _ := regexp.Compile("<[^>]*>")
|
|
str = exp.ReplaceAllString(str, "")
|
|
str = strings.Replace(str, "&", "&", -1)
|
|
str = strings.Replace(str, "<", "<", -1)
|
|
str = strings.Replace(str, ">", ">", -1)
|
|
str = strings.Replace(str, "\\n", "\n", -1)
|
|
return str
|
|
}
|
|
|
|
func parseSubject(subject []string) []string {
|
|
var res []string
|
|
for _, s := range subject {
|
|
res = append(res, strings.Split(s, " / ")...)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func parseDate(date []string) string {
|
|
if len(date) == 0 {
|
|
return ""
|
|
}
|
|
return strings.Replace(date[0], "Unspecified: ", "", -1)
|
|
}
|
|
|
|
func keywords(b map[string]interface{}) (k []string) {
|
|
title, _ := b["title"].(string)
|
|
k = strings.Split(title, " ")
|
|
author, _ := b["author"].([]string)
|
|
for _, a := range author {
|
|
k = append(k, strings.Split(a, " ")...)
|
|
}
|
|
publisher, _ := b["publisher"].(string)
|
|
k = append(k, strings.Split(publisher, " ")...)
|
|
subject, _ := b["subject"].([]string)
|
|
k = append(k, subject...)
|
|
return
|
|
}
|