move options to common package

This commit is contained in:
Celogeek 2023-04-27 18:08:32 +02:00
parent ba538b431b
commit 42eba9cb3b
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
7 changed files with 102 additions and 105 deletions

View File

@ -15,6 +15,7 @@ import (
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image" epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
epubimageprocessing "github.com/celogeek/go-comic-converter/v2/internal/epub/imageprocessing" epubimageprocessing "github.com/celogeek/go-comic-converter/v2/internal/epub/imageprocessing"
epuboptions "github.com/celogeek/go-comic-converter/v2/internal/epub/options"
epubprogress "github.com/celogeek/go-comic-converter/v2/internal/epub/progress" epubprogress "github.com/celogeek/go-comic-converter/v2/internal/epub/progress"
epubtemplates "github.com/celogeek/go-comic-converter/v2/internal/epub/templates" epubtemplates "github.com/celogeek/go-comic-converter/v2/internal/epub/templates"
epubtree "github.com/celogeek/go-comic-converter/v2/internal/epub/tree" epubtree "github.com/celogeek/go-comic-converter/v2/internal/epub/tree"
@ -22,23 +23,8 @@ import (
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
) )
type Options struct {
Input string
Output string
Title string
Author string
LimitMb int
StripFirstDirectoryFromToc bool
Dry bool
DryVerbose bool
SortPathMode int
Quiet bool
Workers int
Image *epubimage.Options
}
type ePub struct { type ePub struct {
*Options *epuboptions.Options
UID string UID string
Publisher string Publisher string
UpdatedAt string UpdatedAt string
@ -52,7 +38,7 @@ type epubPart struct {
} }
// initialize epub // initialize epub
func New(options *Options) *ePub { func New(options *epuboptions.Options) *ePub {
uid := uuid.Must(uuid.NewV4()) uid := uuid.Must(uuid.NewV4())
tmpl := template.New("parser") tmpl := template.New("parser")
tmpl.Funcs(template.FuncMap{ tmpl.Funcs(template.FuncMap{
@ -85,9 +71,9 @@ func (e *ePub) writeImage(wz *epubzip.EpubZip, img *epubimageprocessing.LoadedIm
fmt.Sprintf("OEBPS/%s", img.Image.TextPath()), fmt.Sprintf("OEBPS/%s", img.Image.TextPath()),
[]byte(e.render(epubtemplates.Text, map[string]any{ []byte(e.render(epubtemplates.Text, map[string]any{
"Title": fmt.Sprintf("Image %d Part %d", img.Image.Id, img.Image.Part), "Title": fmt.Sprintf("Image %d Part %d", img.Image.Id, img.Image.Part),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight), "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height),
"ImagePath": img.Image.ImgPath(), "ImagePath": img.Image.ImgPath(),
"ImageStyle": img.Image.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga), "ImageStyle": img.Image.ImgStyle(e.Image.View.Width, e.Image.View.Height, e.Image.Manga),
})), })),
) )
@ -104,21 +90,14 @@ func (e *ePub) writeBlank(wz *epubzip.EpubZip, img *epubimage.Image) error {
fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id), fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id),
[]byte(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.View.Width, e.Image.View.Height),
})), })),
) )
} }
// extract image and split it into part // extract image and split it into part
func (e *ePub) getParts() ([]*epubPart, error) { func (e *ePub) getParts() ([]*epubPart, error) {
loadedImages, err := epubimageprocessing.LoadImages(&epubimageprocessing.Options{ loadedImages, err := epubimageprocessing.LoadImages(e.Options)
Input: e.Input,
SortPathMode: e.SortPathMode,
Quiet: e.Quiet,
Dry: e.Dry,
Workers: e.Workers,
Image: e.Image,
})
if err != nil { if err != nil {
return nil, err return nil, err
@ -277,14 +256,14 @@ func (e *ePub) Write() error {
})}, })},
{"OEBPS/toc.xhtml", epubtemplates.Toc(title, e.StripFirstDirectoryFromToc, part.LoadedImages.Images())}, {"OEBPS/toc.xhtml", epubtemplates.Toc(title, e.StripFirstDirectoryFromToc, part.LoadedImages.Images())},
{"OEBPS/Text/style.css", e.render(epubtemplates.Style, map[string]any{ {"OEBPS/Text/style.css", e.render(epubtemplates.Style, map[string]any{
"PageWidth": e.Image.ViewWidth, "PageWidth": e.Image.View.Width,
"PageHeight": e.Image.ViewHeight, "PageHeight": e.Image.View.Height,
})}, })},
{"OEBPS/Text/title.xhtml", e.render(epubtemplates.Text, map[string]any{ {"OEBPS/Text/title.xhtml", e.render(epubtemplates.Text, map[string]any{
"Title": title, "Title": title,
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight), "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height),
"ImagePath": "Images/title.jpg", "ImagePath": "Images/title.jpg",
"ImageStyle": part.Cover.Image.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga), "ImageStyle": part.Cover.Image.ImgStyle(e.Image.View.Width, e.Image.View.Height, e.Image.Manga),
})}, })},
} }

View File

@ -1,20 +0,0 @@
package epubimage
// options for image transformation
type Options struct {
Crop bool
CropRatioLeft int
CropRatioUp int
CropRatioRight int
CropRatioBottom int
ViewWidth int
ViewHeight int
Quality int
Brightness int
Contrast int
AutoRotate bool
AutoSplitDoublePage bool
NoBlankPage bool
Manga bool
HasCover bool
}

View File

@ -12,6 +12,7 @@ import (
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image" epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
epubimagefilters "github.com/celogeek/go-comic-converter/v2/internal/epub/imagefilters" epubimagefilters "github.com/celogeek/go-comic-converter/v2/internal/epub/imagefilters"
epuboptions "github.com/celogeek/go-comic-converter/v2/internal/epub/options"
epubprogress "github.com/celogeek/go-comic-converter/v2/internal/epub/progress" epubprogress "github.com/celogeek/go-comic-converter/v2/internal/epub/progress"
epubzip "github.com/celogeek/go-comic-converter/v2/internal/epub/zip" epubzip "github.com/celogeek/go-comic-converter/v2/internal/epub/zip"
"github.com/disintegration/gift" "github.com/disintegration/gift"
@ -44,10 +45,10 @@ func isSupportedImage(path string) bool {
} }
// extract and convert images // extract and convert images
func LoadImages(o *Options) (LoadedImages, error) { func LoadImages(o *epuboptions.Options) (LoadedImages, error) {
images := make(LoadedImages, 0) images := make(LoadedImages, 0)
imageCount, imageInput, err := o.Load() imageCount, imageInput, err := Load(o)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -147,17 +148,17 @@ func CoverTitleData(img image.Image, title string, quality int) *epubzip.ZipImag
// transform image into 1 or 3 images // transform image into 1 or 3 images
// only doublepage with autosplit has 3 versions // only doublepage with autosplit has 3 versions
func TransformImage(src image.Image, srcId int, o *epubimage.Options) []image.Image { func TransformImage(src image.Image, srcId int, o *epuboptions.Image) []image.Image {
var filters, splitFilter []gift.Filter var filters, splitFilter []gift.Filter
var images []image.Image var images []image.Image
if o.Crop { if o.Crop.Enabled {
f := epubimagefilters.AutoCrop( f := epubimagefilters.AutoCrop(
src, src,
o.CropRatioLeft, o.Crop.Left,
o.CropRatioUp, o.Crop.Up,
o.CropRatioRight, o.Crop.Right,
o.CropRatioBottom, o.Crop.Bottom,
) )
filters = append(filters, f) filters = append(filters, f)
splitFilter = append(splitFilter, f) splitFilter = append(splitFilter, f)
@ -180,7 +181,7 @@ func TransformImage(src image.Image, srcId int, o *epubimage.Options) []image.Im
} }
filters = append(filters, filters = append(filters,
epubimagefilters.Resize(o.ViewWidth, o.ViewHeight, gift.LanczosResampling), epubimagefilters.Resize(o.View.Width, o.View.Height, gift.LanczosResampling),
epubimagefilters.Pixel(), epubimagefilters.Pixel(),
) )
@ -212,7 +213,7 @@ func TransformImage(src image.Image, srcId int, o *epubimage.Options) []image.Im
g := gift.New(splitFilter...) g := gift.New(splitFilter...)
g.Add( g.Add(
epubimagefilters.CropSplitDoublePage(b), epubimagefilters.CropSplitDoublePage(b),
epubimagefilters.Resize(o.ViewWidth, o.ViewHeight, gift.LanczosResampling), epubimagefilters.Resize(o.View.Width, o.View.Height, gift.LanczosResampling),
) )
dst := image.NewGray(g.Bounds(src.Bounds())) dst := image.NewGray(g.Bounds(src.Bounds()))
g.Draw(dst, src) g.Draw(dst, src)

View File

@ -18,7 +18,7 @@ import (
_ "golang.org/x/image/webp" _ "golang.org/x/image/webp"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image" epuboptions "github.com/celogeek/go-comic-converter/v2/internal/epub/options"
"github.com/celogeek/go-comic-converter/v2/internal/sortpath" "github.com/celogeek/go-comic-converter/v2/internal/sortpath"
"github.com/nwaples/rardecode/v2" "github.com/nwaples/rardecode/v2"
pdfimage "github.com/raff/pdfreader/image" pdfimage "github.com/raff/pdfreader/image"
@ -32,27 +32,10 @@ type tasks struct {
Name string Name string
} }
type Options struct {
Input string
SortPathMode int
Quiet bool
Dry bool
Workers int
Image *epubimage.Options
}
var errNoImagesFound = errors.New("no images found") var errNoImagesFound = errors.New("no images found")
func (o *Options) WorkersRatio(pct int) (nbWorkers int) {
nbWorkers = o.Workers * pct / 100
if nbWorkers < 1 {
nbWorkers = 1
}
return
}
// Load images from input // Load images from input
func (o *Options) Load() (totalImages int, output chan *tasks, err error) { func Load(o *epuboptions.Options) (totalImages int, output chan *tasks, err error) {
fi, err := os.Stat(o.Input) fi, err := os.Stat(o.Input)
if err != nil { if err != nil {
return return
@ -60,15 +43,15 @@ func (o *Options) Load() (totalImages int, output chan *tasks, err error) {
// get all images though a channel of bytes // get all images though a channel of bytes
if fi.IsDir() { if fi.IsDir() {
return o.loadDir() return loadDir(o)
} else { } else {
switch ext := strings.ToLower(filepath.Ext(o.Input)); ext { switch ext := strings.ToLower(filepath.Ext(o.Input)); ext {
case ".cbz", ".zip": case ".cbz", ".zip":
return o.loadCbz() return loadCbz(o)
case ".cbr", ".rar": case ".cbr", ".rar":
return o.loadCbr() return loadCbr(o)
case ".pdf": case ".pdf":
return o.loadPdf() return loadPdf(o)
default: default:
err = fmt.Errorf("unknown file format (%s): support .cbz, .zip, .cbr, .rar, .pdf", ext) err = fmt.Errorf("unknown file format (%s): support .cbz, .zip, .cbr, .rar, .pdf", ext)
return return
@ -77,7 +60,7 @@ func (o *Options) Load() (totalImages int, output chan *tasks, err error) {
} }
// load a directory of images // load a directory of images
func (o *Options) loadDir() (totalImages int, output chan *tasks, err error) { func loadDir(o *epuboptions.Options) (totalImages int, output chan *tasks, err error) {
images := make([]string, 0) images := make([]string, 0)
input := filepath.Clean(o.Input) input := filepath.Clean(o.Input)
@ -167,7 +150,7 @@ func (o *Options) loadDir() (totalImages int, output chan *tasks, err error) {
} }
// load a zip file that include images // load a zip file that include images
func (o *Options) loadCbz() (totalImages int, output chan *tasks, err error) { func loadCbz(o *epuboptions.Options) (totalImages int, output chan *tasks, err error) {
r, err := zip.OpenReader(o.Input) r, err := zip.OpenReader(o.Input)
if err != nil { if err != nil {
return return
@ -253,7 +236,7 @@ func (o *Options) loadCbz() (totalImages int, output chan *tasks, err error) {
} }
// load a rar file that include images // load a rar file that include images
func (o *Options) loadCbr() (totalImages int, output chan *tasks, err error) { func loadCbr(o *epuboptions.Options) (totalImages int, output chan *tasks, err error) {
var isSolid bool var isSolid bool
files, err := rardecode.List(o.Input) files, err := rardecode.List(o.Input)
if err != nil { if err != nil {
@ -370,7 +353,7 @@ func (o *Options) loadCbr() (totalImages int, output chan *tasks, err error) {
} }
// extract image from a pdf // extract image from a pdf
func (o *Options) loadPdf() (totalImages int, output chan *tasks, err error) { func loadPdf(o *epuboptions.Options) (totalImages int, output chan *tasks, err error) {
pdf := pdfread.Load(o.Input) pdf := pdfread.Load(o.Input)
if pdf == nil { if pdf == nil {
err = fmt.Errorf("can't read pdf") err = fmt.Errorf("can't read pdf")

View File

@ -0,0 +1,49 @@
/*
Options for epub creation.
*/
package epuboptions
type Crop struct {
Enabled bool
Left, Up, Right, Bottom int
}
type View struct {
Width, Height int
}
type Image struct {
Crop *Crop
Quality int
Brightness int
Contrast int
AutoRotate bool
AutoSplitDoublePage bool
NoBlankPage bool
Manga bool
HasCover bool
View *View
}
type Options struct {
Input string
Output string
Title string
Author string
LimitMb int
StripFirstDirectoryFromToc bool
Dry bool
DryVerbose bool
SortPathMode int
Quiet bool
Workers int
Image *Image
}
func (o *Options) WorkersRatio(pct int) (nbWorkers int) {
nbWorkers = o.Workers * pct / 100
if nbWorkers < 1 {
nbWorkers = 1
}
return
}

View File

@ -5,6 +5,7 @@ import (
"github.com/beevik/etree" "github.com/beevik/etree"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image" epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
epuboptions "github.com/celogeek/go-comic-converter/v2/internal/epub/options"
) )
type ContentOptions struct { type ContentOptions struct {
@ -13,7 +14,7 @@ type ContentOptions struct {
Author string Author string
Publisher string Publisher string
UpdatedAt string UpdatedAt string
ImageOptions *epubimage.Options ImageOptions *epuboptions.Image
Cover *epubimage.Image Cover *epubimage.Image
Images []*epubimage.Image Images []*epubimage.Image
Current int Current int
@ -91,7 +92,7 @@ func getMeta(o *ContentOptions) []tag {
{"meta", tagAttrs{"property": "schema:accessibilityHazard"}, "noSoundHazard"}, {"meta", tagAttrs{"property": "schema:accessibilityHazard"}, "noSoundHazard"},
{"meta", tagAttrs{"name": "book-type", "content": "comic"}, ""}, {"meta", tagAttrs{"name": "book-type", "content": "comic"}, ""},
{"opf:meta", tagAttrs{"name": "fixed-layout", "content": "true"}, ""}, {"opf:meta", tagAttrs{"name": "fixed-layout", "content": "true"}, ""},
{"opf:meta", tagAttrs{"name": "original-resolution", "content": fmt.Sprintf("%dx%d", o.ImageOptions.ViewWidth, o.ImageOptions.ViewHeight)}, ""}, {"opf:meta", tagAttrs{"name": "original-resolution", "content": fmt.Sprintf("%dx%d", o.ImageOptions.View.Width, o.ImageOptions.View.Height)}, ""},
{"dc:title", tagAttrs{}, o.Title}, {"dc:title", tagAttrs{}, o.Title},
{"dc:identifier", tagAttrs{"id": "ean"}, fmt.Sprintf("urn:uuid:%s", o.UID)}, {"dc:identifier", tagAttrs{"id": "ean"}, fmt.Sprintf("urn:uuid:%s", o.UID)},
{"dc:language", tagAttrs{}, "en"}, {"dc:language", tagAttrs{}, "en"},

32
main.go
View File

@ -14,7 +14,7 @@ import (
"github.com/celogeek/go-comic-converter/v2/internal/converter" "github.com/celogeek/go-comic-converter/v2/internal/converter"
"github.com/celogeek/go-comic-converter/v2/internal/epub" "github.com/celogeek/go-comic-converter/v2/internal/epub"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image" epuboptions "github.com/celogeek/go-comic-converter/v2/internal/epub/options"
"github.com/tcnksm/go-latest" "github.com/tcnksm/go-latest"
) )
@ -101,7 +101,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
profile := cmd.Options.GetProfile() profile := cmd.Options.GetProfile()
perfectWidth, perfectHeight := profile.PerfectDim() perfectWidth, perfectHeight := profile.PerfectDim()
if err := epub.New(&epub.Options{ if err := epub.New(&epuboptions.Options{
Input: cmd.Options.Input, Input: cmd.Options.Input,
Output: cmd.Options.Output, Output: cmd.Options.Output,
LimitMb: cmd.Options.LimitMb, LimitMb: cmd.Options.LimitMb,
@ -109,15 +109,19 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
Author: cmd.Options.Author, Author: cmd.Options.Author,
StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc, StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc,
SortPathMode: cmd.Options.SortPathMode, SortPathMode: cmd.Options.SortPathMode,
Image: &epubimage.Options{ Workers: cmd.Options.Workers,
ViewWidth: perfectWidth, Dry: cmd.Options.Dry,
ViewHeight: perfectHeight, DryVerbose: cmd.Options.DryVerbose,
Quiet: cmd.Options.Quiet,
Image: &epuboptions.Image{
Crop: &epuboptions.Crop{
Enabled: cmd.Options.Crop,
Left: cmd.Options.CropRatioLeft,
Up: cmd.Options.CropRatioUp,
Right: cmd.Options.CropRatioRight,
Bottom: cmd.Options.CropRatioBottom,
},
Quality: cmd.Options.Quality, Quality: cmd.Options.Quality,
Crop: cmd.Options.Crop,
CropRatioLeft: cmd.Options.CropRatioLeft,
CropRatioUp: cmd.Options.CropRatioUp,
CropRatioRight: cmd.Options.CropRatioRight,
CropRatioBottom: cmd.Options.CropRatioBottom,
Brightness: cmd.Options.Brightness, Brightness: cmd.Options.Brightness,
Contrast: cmd.Options.Contrast, Contrast: cmd.Options.Contrast,
AutoRotate: cmd.Options.AutoRotate, AutoRotate: cmd.Options.AutoRotate,
@ -125,11 +129,11 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
NoBlankPage: cmd.Options.NoBlankPage, NoBlankPage: cmd.Options.NoBlankPage,
Manga: cmd.Options.Manga, Manga: cmd.Options.Manga,
HasCover: cmd.Options.HasCover, HasCover: cmd.Options.HasCover,
View: &epuboptions.View{
Width: perfectWidth,
Height: perfectHeight,
},
}, },
Workers: cmd.Options.Workers,
Dry: cmd.Options.Dry,
DryVerbose: cmd.Options.DryVerbose,
Quiet: cmd.Options.Quiet,
}).Write(); err != nil { }).Write(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err) fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1) os.Exit(1)