Skip to content

Commit

Permalink
[minor] Rearranging thumbnails handling:
Browse files Browse the repository at this point in the history
* Added KFX reader
* Restructure MOBI reader so both KFX and MOBI readers have similar behavior.
* Removing "stretch" configuration for MOBI thumbnails - always false.
  • Loading branch information
rupor-github committed Feb 2, 2025
1 parent 6c61b81 commit 8a7abb6
Show file tree
Hide file tree
Showing 39 changed files with 12,000 additions and 117 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,4 @@ incorporate your changes.

### TODO

- Add thumbnail support for KFX files
- Expand history reports with some useful statistics
3 changes: 0 additions & 3 deletions config/config.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ thumbnails:
#---- when thumbnail is prepared it will be scaled to following dimensions
width: 330
height: 470
#---- when true - instead of using prepared thumbnail from book, its cover image (if available) will be
#---- scaled to requested dimensions
stretch: false

logging:
#---- controls terminal (stdout, stderr) output
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module sync2kindle

go 1.23.5
go 1.23

require (
github.com/amazon-ion/ion-go v1.5.0
github.com/disintegration/imaging v1.6.2
github.com/dustin/go-humanize v1.0.1
github.com/go-ole/go-ole v1.3.0
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/amazon-ion/ion-go v1.5.0 h1:fxsAyFda8N9HsM2xYbQSxJ3Qi/oLn0xzLoiXWG3bseg=
github.com/amazon-ion/ion-go v1.5.0/go.mod h1:3ZEje8i20TiIPVZlN+KE3B2ppZ1B8d9F/KaT7Dtec+k=
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
Expand All @@ -22,6 +25,7 @@ github.com/go-playground/validator/v10 v10.21.0 h1:4fZA11ovvtkdgaeev9RGWPgc1uj3H
github.com/go-playground/validator/v10 v10.21.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand All @@ -40,6 +44,8 @@ github.com/rupor-github/gencfg v1.0.2 h1:LJnOoOL7ob9PBjikL4eHEhVruAhl6zCOFvFheuE
github.com/rupor-github/gencfg v1.0.2/go.mod h1:41OMdMXM3iYYcjxrPwh3WYkud3+0Pf+RM3P2FovXG80=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
Expand Down Expand Up @@ -76,8 +82,11 @@ golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.21.1-0.20240531212143-b6235391adb3 h1:SHq4Rl+B7WvyM4XODon1LXtP7gcG49+7Jubt1gWWswY=
golang.org/x/tools v0.21.1-0.20240531212143-b6235391adb3/go.mod h1:bqv7PJ/TtlrzgJKhOAGdDUkUltQapRik/UEHubLVBWo=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.5.1 h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I=
Expand Down
5 changes: 2 additions & 3 deletions thumbs/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package thumbs

type ThumbnailsConfig struct {
Width int `yaml:"width" validate:"required,gt=0"`
Height int `yaml:"height" validate:"required,gt=0"`
Stretch bool `yaml:"stretch"`
Width int `yaml:"width" validate:"required,gt=0"`
Height int `yaml:"height" validate:"required,gt=0"`

Dir string `yaml:"-"` // internal use only
}
48 changes: 48 additions & 0 deletions thumbs/imgutils/jfif.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package imgutils

import (
"bytes"
"encoding/binary"
)

// This is specific to go - when encoding jpeg standard encoder does not create JFIF APP0 segment and Kindle does not like it.

// JpegDPIType specifyes type of the DPI units
type JpegDPIType uint8

// DPI units type values
const (
DpiNoUnits JpegDPIType = iota
DpiPxPerInch
DpiPxPerSm
)

var (
marker = []byte{0xFF, 0xE0} // APP0 segment marker
jfif = []byte{0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x02} // jfif + version
)

// SetJpegDPI creates JFIF APP0 with provided DPI if segment is missing in image.
func SetJpegDPI(buf *bytes.Buffer, dpit JpegDPIType, xdensity, ydensity int16) (*bytes.Buffer, bool) {

data := buf.Bytes()

// If JFIF APP0 segment is there - do not do anything
if bytes.Equal(data[2:4], marker) {
return buf, false
}

var newbuf = new(bytes.Buffer)

newbuf.Write(data[:2])
newbuf.Write(marker)
binary.Write(newbuf, binary.BigEndian, uint16(0x10)) // length
newbuf.Write(jfif)
binary.Write(newbuf, binary.BigEndian, uint8(dpit))
binary.Write(newbuf, binary.BigEndian, uint16(xdensity))
binary.Write(newbuf, binary.BigEndian, uint16(ydensity))
binary.Write(newbuf, binary.BigEndian, uint16(0)) // no thumbnail segment
newbuf.Write(data[2:])

return newbuf, true
}
57 changes: 57 additions & 0 deletions thumbs/kfx/ion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package kfx

import (
"bytes"
"fmt"

"github.com/amazon-ion/ion-go/ion"
)

const (
largestKnownSymbol = 834
//
symBookMetadata = 490
symCategorizedMetadata = 491
symExternalResource = 164
symThumbnails = 214
symRawMedia = 417
)

var (
ionBVM = []byte{0xE0, 1, 0, 0xEA} // binary version marker
sharedSymbolTable = createSST(largestKnownSymbol)
)

// Actual names for symbols could be obtained by looking at EpubToKFXConverter-4.0.jar from Kindle Previewer 3
// with enum of interest in class file "com.amazon.kaf.c/B.class" presently.
func createSST(maxID uint64) ion.SharedSymbolTable {
symbols := make([]string, 0, maxID)
for i := len(ion.V1SystemSymbolTable.Symbols()) + 1; i <= len(ion.V1SystemSymbolTable.Symbols())+int(maxID); i++ {
symbols = append(symbols, fmt.Sprintf("$%d", i))
}
return ion.NewSharedSymbolTable("YJ_symbols", 10, symbols)
}

func createProlog() []byte {
buf := bytes.Buffer{}
if err := ion.NewBinaryWriter(&buf, sharedSymbolTable).Finish(); err != nil {
panic(err)
}
return buf.Bytes()
}

func decodeData(prolog, data []byte, v any) error {
if err := ion.Unmarshal(append(prolog, data[len(ionBVM):]...), v, sharedSymbolTable); err != nil {
return err
}
return nil
}

func decodeST(data []byte) (ion.SymbolTable, error) {
r := ion.NewReaderCat(bytes.NewReader(data), ion.NewCatalog(sharedSymbolTable))
r.Next() // we are not interested in the actual values and in most cases this will return false anyways
if err := r.Err(); err != nil {
return nil, err
}
return r.SymbolTable(), nil
}
Loading

0 comments on commit 8a7abb6

Please sign in to comment.