diff --git a/bookbrowser.go b/bookbrowser.go index 5efffdeb..494dc60c 100644 --- a/bookbrowser.go +++ b/bookbrowser.go @@ -10,6 +10,7 @@ import ( "syscall" _ "github.com/geek1011/BookBrowser/formats/epub" + _ "github.com/geek1011/BookBrowser/formats/mobi" _ "github.com/geek1011/BookBrowser/formats/pdf" "github.com/geek1011/BookBrowser/modules/server" "github.com/geek1011/BookBrowser/modules/sigusr" diff --git a/formats/mobi/mobi.go b/formats/mobi/mobi.go new file mode 100644 index 00000000..39bd9348 --- /dev/null +++ b/formats/mobi/mobi.go @@ -0,0 +1,76 @@ +package mobi + +import ( + "crypto/sha1" + "fmt" + "image" + "io" + "os" + "path/filepath" + + "github.com/geek1011/BookBrowser/booklist" + "github.com/geek1011/BookBrowser/formats" + "github.com/pkg/errors" +) + +type mobi struct { + book *booklist.Book +} + +func (e *mobi) Book() *booklist.Book { + return e.book +} + +func (e *mobi) HasCover() bool { + return false +} + +func (e *mobi) GetCover() (i image.Image, err error) { + return nil, errors.New("no cover") +} + +func load(filename string) (bi formats.BookInfo, ferr error) { + defer func() { + if r := recover(); r != nil { + bi = nil + ferr = fmt.Errorf("unknown error: %s", r) + } + }() + + m := &mobi{book: &booklist.Book{}} + + f, err := os.Open(filename) + if err != nil { + return nil, err + } + + fi, err := f.Stat() + if err != nil { + f.Close() + return nil, errors.Wrapf(err, "could not stat book") + } + m.book.FilePath = filename + m.book.FileSize = fi.Size() + m.book.ModTime = fi.ModTime() + + s := sha1.New() + i, err := io.Copy(s, f) + if err == nil && i != fi.Size() { + err = errors.New("could not read whole file") + } + if err != nil { + f.Close() + return nil, errors.Wrap(err, "could not hash book") + } + m.book.Hash = fmt.Sprintf("%x", s.Sum(nil)) + + f.Close() + + m.book.Title = filepath.Base(filename) + + return m, nil +} + +func init() { + formats.Register("mobi", load) +} diff --git a/modules/server/server.go b/modules/server/server.go index ef8e81af..c5b4da87 100644 --- a/modules/server/server.go +++ b/modules/server/server.go @@ -87,6 +87,8 @@ func (s *Server) RefreshBookIndex() error { if s.Verbose { log.Printf("Indexing finished with %v errors", len(errs)) } + } else { + log.Printf("Indexing finished") } debug.FreeOSMemory()