Use io.readerAt to open the books without loading everything in memory
This commit is contained in:
parent
965205adae
commit
e732909773
2 changed files with 45 additions and 27 deletions
64
reader.go
64
reader.go
|
@ -3,16 +3,13 @@ package main
|
||||||
import (
|
import (
|
||||||
log "github.com/cihub/seelog"
|
log "github.com/cihub/seelog"
|
||||||
|
|
||||||
"bytes"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.gitorious.org/go-pkg/epubgo.git"
|
"git.gitorious.org/go-pkg/epubgo.git"
|
||||||
"git.gitorious.org/trantor/trantor.git/database"
|
"git.gitorious.org/trantor/trantor.git/database"
|
||||||
"git.gitorious.org/trantor/trantor.git/storage"
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -183,13 +180,24 @@ func openReadEpub(h handler) (*epubgo.Epub, database.Book) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, book
|
return nil, book
|
||||||
}
|
}
|
||||||
|
|
||||||
if !book.Active {
|
if !book.Active {
|
||||||
if !h.sess.IsAdmin() {
|
if !h.sess.IsAdmin() {
|
||||||
return nil, book
|
return nil, book
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
e, err := openBook(book.Id, h.store)
|
|
||||||
|
f, err := h.store.Get(book.Id, EPUB_FILE)
|
||||||
|
if err != nil {
|
||||||
|
return nil, book
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
info, err := f.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return nil, book
|
||||||
|
}
|
||||||
|
|
||||||
|
e, err := epubgo.Load(f, info.Size())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, book
|
return nil, book
|
||||||
}
|
}
|
||||||
|
@ -205,42 +213,46 @@ func contentHandler(h handler) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
book, err := h.db.GetBookId(id)
|
err := openEpubFile(h, id, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
notFound(h)
|
notFound(h)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func openEpubFile(h handler, id string, file string) error {
|
||||||
|
book, err := h.db.GetBookId(id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if !book.Active {
|
if !book.Active {
|
||||||
if !h.sess.IsAdmin() {
|
if !h.sess.IsAdmin() {
|
||||||
notFound(h)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
e, err := openBook(book.Id, h.store)
|
|
||||||
|
f, err := h.store.Get(id, EPUB_FILE)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
notFound(h)
|
return err
|
||||||
return
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
info, err := f.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
e, err := epubgo.Load(f, info.Size())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
defer e.Close()
|
defer e.Close()
|
||||||
|
|
||||||
html, err := e.OpenFile(file)
|
html, err := e.OpenFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
notFound(h)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer html.Close()
|
defer html.Close()
|
||||||
io.Copy(h.w, html)
|
io.Copy(h.w, html)
|
||||||
}
|
return nil
|
||||||
|
|
||||||
func openBook(id string, store *storage.Store) (*epubgo.Epub, error) {
|
|
||||||
f, err := store.Get(id, EPUB_FILE)
|
|
||||||
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)))
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,12 @@ type Store struct {
|
||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type File interface {
|
||||||
|
io.ReadCloser
|
||||||
|
io.ReaderAt
|
||||||
|
Stat() (fi os.FileInfo, err error)
|
||||||
|
}
|
||||||
|
|
||||||
func Init(path string) (*Store, error) {
|
func Init(path string) (*Store, error) {
|
||||||
st := new(Store)
|
st := new(Store)
|
||||||
st.path = path
|
st.path = path
|
||||||
|
@ -42,7 +48,7 @@ func (st *Store) Store(id string, file io.Reader, name string) (size int64, err
|
||||||
return io.Copy(dest, file)
|
return io.Copy(dest, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (st *Store) Get(id string, name string) (io.ReadCloser, error) {
|
func (st *Store) Get(id string, name string) (File, error) {
|
||||||
path := idPath(st.path, id)
|
path := idPath(st.path, id)
|
||||||
return os.Open(p.Join(path, name))
|
return os.Open(p.Join(path, name))
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue