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
}
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()),
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),
"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),
e.render(epubtemplates.Blank, map[string]any{
[]byte(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 any
Content string
}
epubParts, err := e.getParts()
@ -280,8 +280,8 @@ func (e *ePub) Write() error {
if err = wz.WriteMagic(); err != nil {
return err
}
for _, content := range content {
if err := wz.WriteFile(content.Name, content.Content); err != nil {
for _, c := range content {
if err := wz.WriteFile(c.Name, []byte(c.Content)); err != nil {
return err
}
}

View File

@ -114,18 +114,16 @@ 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() &&
src.Bounds().Dx() > o.Image.ViewHeight &&
src.Bounds().Dy() > o.Image.ViewWidth,
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(),
Path: img.Path,
Name: img.Name,
}
// Auto split double page
@ -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
if (!o.Image.HasCover || img.Id > 0) &&
o.Image.AutoSplitDoublePage &&
src.Bounds().Dx() > src.Bounds().Dy() &&
src.Bounds().Dx() > o.Image.ViewHeight &&
src.Bounds().Dy() > o.Image.ViewWidth {
src.Bounds().Dx() > src.Bounds().Dy() {
gifts := epubimage.NewGiftSplitDoublePage(o.Image)
for i, g := range gifts {
part := i + 1
@ -176,7 +172,7 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
bar.Close()
if len(images) == 0 {
return nil, fmt.Errorf("image not found")
return nil, errNoImagesFound
}
return images, nil

View File

@ -3,6 +3,7 @@ package epubimageprocessing
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"io/fs"
@ -27,6 +28,8 @@ 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 {
@ -70,7 +73,7 @@ func (o *Options) loadDir() (totalImages int, output chan *tasks, err error) {
totalImages = len(images)
if totalImages == 0 {
err = fmt.Errorf("image not found")
err = errNoImagesFound
return
}
@ -115,7 +118,7 @@ func (o *Options) loadCbz() (totalImages int, output chan *tasks, err error) {
if totalImages == 0 {
r.Close()
err = fmt.Errorf("no images found")
err = errNoImagesFound
return
}
@ -176,7 +179,7 @@ func (o *Options) loadCbr() (totalImages int, output chan *tasks, err error) {
totalImages = len(names)
if totalImages == 0 {
err = fmt.Errorf("no images found")
err = errNoImagesFound
return
}

View File

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