Select Git revision
refine.go 3.22 KiB
package liber
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"log"
"os"
)
func (db *Database) RefineFunc(dir string, chooser MetadataChooserFunc) func(*Book) error {
storage := NewFileStorage(dir)
refiners := defaultMetadataRefiners
return func(book *Book) error {
if err := refineMetadata(book, "", storage, refiners, chooser); err == nil {
log.Printf("%s: updated metadata", book.Id)
return db.PutBook(book)
}
return nil
}
}
// ListBooks writes IDs of books that match any of a series of
// functions to an io.Writer.
func (db *Database) ListBooks(w io.Writer, matchFuncs ...func(book *Book) bool) error {
return db.onAllBooks(func(book *Book) error {
ok := false
for _, f := range matchFuncs {
if f(book) {
ok = true
break
}
}
if ok {
fmt.Fprintf(w, "%s\n", book.Id)
}
return nil
})
}
// WithBookIDs calls a function on books whose IDs are read from the
// specified io.Reader.
func (db *Database) WithBookIDs(r io.Reader, f func(book *Book) error) error {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
id := ParseID(string(bytes.TrimSpace(scanner.Bytes())))
book, err := db.GetBook(id)
if err != nil {
log.Printf("error: no such book %s", id)
continue
}
if err := f(book); err != nil {
log.Printf("error: %s: %v", id, err)
// Stop?
}
}
return scanner.Err()
}
// Local path is a hint for generating the cover image.
func findCoverImage(book *Book, path string, refiners []MetadataRefiner, storage *FileStorage) error {
if book.CoverPath != "" {
return nil
}
localCoverPath := path + ".cover.png"
for _, refiner := range refiners {