mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 16:22:37 +02:00
Compare commits
4 Commits
07e04e514b
...
d0d9af7f9b
Author | SHA1 | Date | |
---|---|---|---|
d0d9af7f9b | |||
8a1eeb400d | |||
01d1670edd | |||
7eebd18538 |
@ -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)
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user