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

Commit

Permalink
Do not regenerate cover if already cached (fixes #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Dec 30, 2017
1 parent 62c360f commit 3b51b18
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 58 deletions.
2 changes: 1 addition & 1 deletion bookbrowser.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func main() {
}
}

if fi, err := os.Stat(tempdir); err == nil || fi.IsDir() {
if fi, err := os.Stat(tempdir); err == nil || (fi != nil && fi.IsDir()) {
noRemoveTempDir = true
if tempdir == deftempdir {
noRemoveTempDir = false
Expand Down
117 changes: 60 additions & 57 deletions modules/booklist/booklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/bamiaux/rez"
"github.com/geek1011/BookBrowser/formats"
"github.com/geek1011/BookBrowser/models"
"github.com/geek1011/BookBrowser/modules/util"
zglob "github.com/mattn/go-zglob"
)

Expand Down Expand Up @@ -83,63 +84,65 @@ func NewBookListFromDir(dir, coverOutDir string, verbose, nocovers bool) (*BookL
coverPath := filepath.Join(coverOutDir, book.ID+".jpg")
thumbPath := filepath.Join(coverOutDir, book.ID+"_thumb.jpg")

coverFile, err := os.Create(coverPath)
if err != nil {
continue
}
defer coverFile.Close()

err = jpeg.Encode(coverFile, cover, nil)
if err != nil {
continue
}

coverBounds := cover.Bounds()
coverWidth := coverBounds.Dx()
coverHeight := coverBounds.Dy()

if coverWidth <= 200 {
continue
}

// Scale to fit in 200x900
scale := math.Min(float64(200.0/float64(coverWidth)), float64(900.0/float64(coverHeight)))

// Scale and round down
coverWidth = int(float64(coverWidth) * scale)
coverHeight = int(float64(coverHeight) * scale)

r := image.Rect(0, 0, coverWidth, coverHeight)
var thumb image.Image
switch t := cover.(type) {
case *image.YCbCr:
thumb = image.NewYCbCr(r, t.SubsampleRatio)
case *image.RGBA:
thumb = image.NewRGBA(r)
case *image.NRGBA:
thumb = image.NewNRGBA(r)
case *image.Gray:
thumb = image.NewGray(r)
default:
continue
}

// rez.NewLanczos(2.0) is faster, but slower
err = rez.Convert(thumb, cover, rez.NewBicubicFilter())
if err != nil {
fmt.Println(coverWidth, coverHeight, scale, err)
continue
}

thumbFile, err := os.Create(thumbPath)
if err != nil {
continue
}
defer thumbFile.Close()

err = jpeg.Encode(thumbFile, thumb, nil)
if err != nil {
continue
if !(util.Exists(coverPath) && util.Exists(thumbPath)) {
coverFile, err := os.Create(coverPath)
if err != nil {
continue
}
defer coverFile.Close()

err = jpeg.Encode(coverFile, cover, nil)
if err != nil {
continue
}

coverBounds := cover.Bounds()
coverWidth := coverBounds.Dx()
coverHeight := coverBounds.Dy()

if coverWidth <= 200 {
continue
}

// Scale to fit in 200x900
scale := math.Min(float64(200.0/float64(coverWidth)), float64(900.0/float64(coverHeight)))

// Scale and round down
coverWidth = int(float64(coverWidth) * scale)
coverHeight = int(float64(coverHeight) * scale)

r := image.Rect(0, 0, coverWidth, coverHeight)
var thumb image.Image
switch t := cover.(type) {
case *image.YCbCr:
thumb = image.NewYCbCr(r, t.SubsampleRatio)
case *image.RGBA:
thumb = image.NewRGBA(r)
case *image.NRGBA:
thumb = image.NewNRGBA(r)
case *image.Gray:
thumb = image.NewGray(r)
default:
continue
}

// rez.NewLanczos(2.0) is faster, but slower
err = rez.Convert(thumb, cover, rez.NewBicubicFilter())
if err != nil {
fmt.Println(coverWidth, coverHeight, scale, err)
continue
}

thumbFile, err := os.Create(thumbPath)
if err != nil {
continue
}
defer thumbFile.Close()

err = jpeg.Encode(thumbFile, thumb, nil)
if err != nil {
continue
}
}

book.HasCover = true
Expand Down
7 changes: 7 additions & 0 deletions modules/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"net"
"os"
"strings"
)

Expand Down Expand Up @@ -51,3 +52,9 @@ func GetIP() net.IP {

return localAddr.IP
}

// Exists checks whether a path exists
func Exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
5 changes: 5 additions & 0 deletions modules/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ func TestStringAfter(t *testing.T) {
func TestFixString(t *testing.T) {
assert.Equal(t, `""""''`, FixString(`“‹”›‘’`))
}

func TestExists(t *testing.T) {
assert.True(t, Exists("."))
assert.False(t, Exists("sdfsdfsdf"))
}

0 comments on commit 3b51b18

Please sign in to comment.