Compare commits

..

No commits in common. "d0d9af7f9beb8fb40d17b8f809cdf08a8c9a854c" and "07e04e514b1b588f7c3f885efb59d0c2db7e5450" have entirely different histories.

5 changed files with 39 additions and 27 deletions

View File

@ -269,7 +269,7 @@ func (c *Converter) Validate() error {
return nil
}
func (c *Converter) Fatal(err error) {
func (c Converter) Fatal(err error) {
c.Cmd.Usage()
fmt.Fprintf(os.Stderr, "\nError: %s\n", err)
os.Exit(1)

View File

@ -77,12 +77,12 @@ func (e *ePub) render(templateString string, data any) string {
func (e *ePub) writeImage(wz *epubzip.EpubZip, img *epubimage.Image) error {
err := wz.WriteFile(
fmt.Sprintf("OEBPS/%s", img.TextPath()),
[]byte(e.render(epubtemplates.Text, map[string]any{
e.render(epubtemplates.Text, map[string]any{
"Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight),
"ImagePath": img.ImgPath(),
"ImageStyle": img.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga),
})),
}),
)
if err == nil {
@ -95,10 +95,10 @@ func (e *ePub) writeImage(wz *epubzip.EpubZip, img *epubimage.Image) error {
func (e *ePub) writeBlank(wz *epubzip.EpubZip, img *epubimage.Image) error {
return wz.WriteFile(
fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id),
[]byte(e.render(epubtemplates.Blank, map[string]any{
e.render(epubtemplates.Blank, map[string]any{
"Title": fmt.Sprintf("Blank Page %d", img.Id),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight),
})),
}),
)
}
@ -200,7 +200,7 @@ func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string {
func (e *ePub) Write() error {
type zipContent struct {
Name string
Content string
Content any
}
epubParts, err := e.getParts()
@ -280,8 +280,8 @@ func (e *ePub) Write() error {
if err = wz.WriteMagic(); err != nil {
return err
}
for _, c := range content {
if err := wz.WriteFile(c.Name, []byte(c.Content)); err != nil {
for _, content := range content {
if err := wz.WriteFile(content.Name, content.Content); err != nil {
return err
}
}

View File

@ -114,16 +114,18 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
}
imageOutput <- &epubimage.Image{
Id: img.Id,
Part: 0,
Raw: raw,
Data: epubimagedata.New(img.Id, 0, dst, o.Image.Quality),
Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(),
IsCover: img.Id == 0,
DoublePage: src.Bounds().Dx() > src.Bounds().Dy(),
Path: img.Path,
Name: img.Name,
Id: img.Id,
Part: 0,
Raw: raw,
Data: epubimagedata.New(img.Id, 0, dst, o.Image.Quality),
Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(),
IsCover: img.Id == 0,
DoublePage: src.Bounds().Dx() > src.Bounds().Dy() &&
src.Bounds().Dx() > o.Image.ViewHeight &&
src.Bounds().Dy() > o.Image.ViewWidth,
Path: img.Path,
Name: img.Name,
}
// Auto split double page
@ -131,7 +133,9 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
// Only if the src image have width > height and is bigger than the view
if (!o.Image.HasCover || img.Id > 0) &&
o.Image.AutoSplitDoublePage &&
src.Bounds().Dx() > src.Bounds().Dy() {
src.Bounds().Dx() > src.Bounds().Dy() &&
src.Bounds().Dx() > o.Image.ViewHeight &&
src.Bounds().Dy() > o.Image.ViewWidth {
gifts := epubimage.NewGiftSplitDoublePage(o.Image)
for i, g := range gifts {
part := i + 1
@ -172,7 +176,7 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
bar.Close()
if len(images) == 0 {
return nil, errNoImagesFound
return nil, fmt.Errorf("image not found")
}
return images, nil

View File

@ -3,7 +3,6 @@ package epubimageprocessing
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"io/fs"
@ -28,8 +27,6 @@ type Options struct {
Image *epubimage.Options
}
var errNoImagesFound = errors.New("no images found")
func (o *Options) mustExtractImage(imageOpener func() (io.ReadCloser, error)) *bytes.Buffer {
var b bytes.Buffer
if o.Dry {
@ -73,7 +70,7 @@ func (o *Options) loadDir() (totalImages int, output chan *tasks, err error) {
totalImages = len(images)
if totalImages == 0 {
err = errNoImagesFound
err = fmt.Errorf("image not found")
return
}
@ -118,7 +115,7 @@ func (o *Options) loadCbz() (totalImages int, output chan *tasks, err error) {
if totalImages == 0 {
r.Close()
err = errNoImagesFound
err = fmt.Errorf("no images found")
return
}
@ -179,7 +176,7 @@ func (o *Options) loadCbr() (totalImages int, output chan *tasks, err error) {
totalImages = len(names)
if totalImages == 0 {
err = errNoImagesFound
err = fmt.Errorf("no images found")
return
}

View File

@ -2,6 +2,7 @@ package epubzip
import (
"archive/zip"
"fmt"
"os"
"time"
@ -60,7 +61,17 @@ func (e *EpubZip) WriteImage(image *epubimagedata.ImageData) error {
return err
}
func (e *EpubZip) WriteFile(file string, content []byte) error {
func (e *EpubZip) WriteFile(file string, data any) error {
var content []byte
switch b := data.(type) {
case string:
content = []byte(b)
case []byte:
content = b
default:
return fmt.Errorf("support string of []byte")
}
m, err := e.wz.CreateHeader(&zip.FileHeader{
Name: file,
Modified: time.Now(),