This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/store.go

129 lines
2.8 KiB
Go
Raw Normal View History

package main
import (
2013-04-12 01:43:23 +02:00
"bytes"
2013-04-01 14:05:04 +02:00
"git.gitorious.org/go-pkg/epubgo.git"
"io"
2013-04-12 01:43:23 +02:00
"io/ioutil"
2013-04-12 01:05:40 +02:00
"labix.org/v2/mgo/bson"
"regexp"
"strings"
)
2013-04-12 01:05:40 +02:00
func OpenBook(id bson.ObjectId) (*epubgo.Epub, error) {
fs := db.GetFS(FS_BOOKS)
2013-04-12 01:43:23 +02:00
f, err := fs.OpenId(id)
2013-04-12 01:05:40 +02:00
if err != nil {
return nil, err
}
2013-04-12 01:43:23 +02:00
defer f.Close()
2013-04-12 01:05:40 +02:00
2013-04-12 01:43:23 +02:00
buff, err := ioutil.ReadAll(f)
reader := bytes.NewReader(buff)
2013-04-12 01:05:40 +02:00
2013-04-12 01:43:23 +02:00
return epubgo.Load(reader, int64(len(buff)))
2013-04-12 01:05:40 +02:00
}
func StoreNewFile(name string, file io.Reader) (bson.ObjectId, error) {
fs := db.GetFS(FS_BOOKS)
fw, err := fs.Create(name)
if err != nil {
return "", err
}
defer fw.Close()
2013-04-01 14:05:04 +02:00
_, err = io.Copy(fw, file)
2013-04-12 01:05:40 +02:00
id, _ := fw.Id().(bson.ObjectId)
return id, err
}
2013-04-12 01:05:40 +02:00
func DeleteFile(id bson.ObjectId) error {
fs := db.GetFS(FS_BOOKS)
return fs.RemoveId(id)
2012-10-28 17:22:23 +01:00
}
2013-04-15 22:10:48 +02:00
func DeleteCover(id bson.ObjectId) error {
fs := db.GetFS(FS_IMGS)
return fs.RemoveId(id)
}
2012-10-28 17:27:01 +01:00
func DeleteBook(book Book) {
if book.Cover != "" {
2013-04-15 22:10:48 +02:00
DeleteCover(book.Cover)
2012-10-28 17:27:01 +01:00
}
if book.CoverSmall != "" {
2013-04-15 22:10:48 +02:00
DeleteCover(book.CoverSmall)
2012-10-28 17:27:01 +01:00
}
2013-04-12 01:05:40 +02:00
DeleteFile(book.File)
2012-10-28 17:27:01 +01:00
}
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 {
2013-05-31 00:34:11 +02:00
str := cleanStr(strings.Join(description, "\n"))
str = strings.Replace(str, "</p>", "\n", -1)
exp, _ := regexp.Compile("<[^>]*>")
str = exp.ReplaceAllString(str, "")
str = strings.Replace(str, "&amp;", "&", -1)
str = strings.Replace(str, "&lt;", "<", -1)
str = strings.Replace(str, "&gt;", ">", -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
}