Skip to content
Snippets Groups Projects
Select Git revision
  • abf367e3b9e3c2f9a051fb0282cf955087b15a6c
  • master default protected
  • renovate/github.com-blevesearch-bleve-v2-2.x
  • renovate/golang-1.x
  • sql
  • batch
  • debian
  • search_api
  • 0.2.1e
  • 0.2.1d
  • 0.2.1c
  • 0.2.1b
  • 0.2.1a
  • 0.2.1
14 results

refine.go

Blame
  • 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 {