Compare commits

...

4 Commits

Author SHA1 Message Date
d0d9af7f9b
specify type for write file 2023-04-25 22:10:35 +02:00
8a1eeb400d
factor error code 2023-04-25 21:41:19 +02:00
01d1670edd
fix method 2023-04-25 21:02:24 +02:00
7eebd18538
fix autosplit double page
only check source width > height
2023-04-25 20:07:55 +02:00
5 changed files with 27 additions and 39 deletions

View File

@ -269,7 +269,7 @@ func (c *Converter) Validate() error {
return nil return nil
} }
func (c Converter) Fatal(err error) { func (c *Converter) Fatal(err error) {
c.Cmd.Usage() c.Cmd.Usage()
fmt.Fprintf(os.Stderr, "\nError: %s\n", err) fmt.Fprintf(os.Stderr, "\nError: %s\n", err)
os.Exit(1) 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 { func (e *ePub) writeImage(wz *epubzip.EpubZip, img *epubimage.Image) error {
err := wz.WriteFile( err := wz.WriteFile(
fmt.Sprintf("OEBPS/%s", img.TextPath()), fmt.Sprintf("OEBPS/%s", img.TextPath()),
e.render(epubtemplates.Text, map[string]any{ []byte(e.render(epubtemplates.Text, map[string]any{
"Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part), "Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight), "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight),
"ImagePath": img.ImgPath(), "ImagePath": img.ImgPath(),
"ImageStyle": img.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga), "ImageStyle": img.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga),
}), })),
) )
if err == nil { 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 { func (e *ePub) writeBlank(wz *epubzip.EpubZip, img *epubimage.Image) error {
return wz.WriteFile( return wz.WriteFile(
fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id), fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id),
e.render(epubtemplates.Blank, map[string]any{ []byte(e.render(epubtemplates.Blank, map[string]any{
"Title": fmt.Sprintf("Blank Page %d", img.Id), "Title": fmt.Sprintf("Blank Page %d", img.Id),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight), "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 { func (e *ePub) Write() error {
type zipContent struct { type zipContent struct {
Name string Name string
Content any Content string
} }
epubParts, err := e.getParts() epubParts, err := e.getParts()
@ -280,8 +280,8 @@ func (e *ePub) Write() error {
if err = wz.WriteMagic(); err != nil { if err = wz.WriteMagic(); err != nil {
return err return err
} }
for _, content := range content { for _, c := range content {
if err := wz.WriteFile(content.Name, content.Content); err != nil { if err := wz.WriteFile(c.Name, []byte(c.Content)); err != nil {
return err return err
} }
} }

View File

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

View File

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

View File

@ -2,7 +2,6 @@ package epubzip
import ( import (
"archive/zip" "archive/zip"
"fmt"
"os" "os"
"time" "time"
@ -61,17 +60,7 @@ func (e *EpubZip) WriteImage(image *epubimagedata.ImageData) error {
return err return err
} }
func (e *EpubZip) WriteFile(file string, data any) error { func (e *EpubZip) WriteFile(file string, content []byte) 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{ m, err := e.wz.CreateHeader(&zip.FileHeader{
Name: file, Name: file,
Modified: time.Now(), Modified: time.Now(),