Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Basic mobi support (closes #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Aug 14, 2018
1 parent 6f6bf15 commit 8edf92d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions bookbrowser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
76 changes: 76 additions & 0 deletions formats/mobi/mobi.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions modules/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8edf92d

Please sign in to comment.