Using master pg library

This commit is contained in:
Las Zenow 2017-05-18 22:16:16 +00:00
parent d9a95a997c
commit e0854aa001
8 changed files with 43 additions and 76 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/gorilla/mux"
"github.com/meskio/epubgo"
"gitlab.com/trantor/trantor/lib/database"
"gitlab.com/trantor/trantor/lib/storage"
)
type chapter struct {
@ -105,7 +106,7 @@ func getChapters(e *epubgo.Epub, file string, id string, base string) []chapter
func listChapters(nav *epubgo.NavigationIterator, depth int) []chapter {
var chapters []chapter
var err error = nil
var err error
for err == nil {
var c chapter
c.Label = nav.Title()
@ -128,7 +129,7 @@ func listChapters(nav *epubgo.NavigationIterator, depth int) []chapter {
func readStartHandler(h handler) {
id := mux.Vars(h.r)["id"]
e, _ := openReadEpub(h)
e := openReadEpub(id, h.store)
if e == nil {
log.Warn("Open epub returns an empty file")
notFound(h)
@ -148,7 +149,7 @@ func readStartHandler(h handler) {
func readHandler(h handler) {
id := mux.Vars(h.r)["id"]
file := mux.Vars(h.r)["file"]
e, book := openReadEpub(h)
e := openReadEpub(id, h.store)
if e == nil {
notFound(h)
return
@ -158,6 +159,11 @@ func readHandler(h handler) {
var data readData
data.S = GetStatus(h)
book, err := h.db.GetBookID(id)
if err != nil {
notFound(h)
return
}
author := ""
if len(book.Authors) > 0 {
author = " by " + book.Authors[0]
@ -177,38 +183,23 @@ func readHandler(h handler) {
h.template.load(h, "read", data)
}
func openReadEpub(h handler) (*epubgo.Epub, database.Book) {
var book database.Book
id := mux.Vars(h.r)["id"]
if id == "" {
return nil, book
}
book, err := h.db.GetBookID(id)
func openReadEpub(id string, store storage.Store) *epubgo.Epub {
f, err := store.Get(id, epubFile)
if err != nil {
return nil, book
}
if !book.Active {
if !h.sess.IsModerator() {
return nil, book
}
}
f, err := h.store.Get(book.ID, epubFile)
if err != nil {
return nil, book
return nil
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return nil, book
return nil
}
e, err := epubgo.Load(f, info.Size())
if err != nil {
return nil, book
return nil
}
return e, book
return e
}
func contentHandler(h handler) {
@ -228,16 +219,6 @@ func contentHandler(h handler) {
}
func openEpubFile(h handler, id string, file string) error {
book, err := h.db.GetBookID(id)
if err != nil {
return err
}
if !book.Active {
if !h.sess.IsModerator() {
return err
}
}
f, err := h.store.Get(id, epubFile)
if err != nil {
return err