From 42eba9cb3b2410f3c38c622d7f52b8e4865a8d33 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Thu, 27 Apr 2023 18:08:32 +0200 Subject: [PATCH] move options to common package --- internal/epub/epub.go | 43 +++++----------- internal/epub/image/epub_image_options.go | 20 -------- .../imageprocessing/epub_image_processing.go | 21 ++++---- .../epub_image_processing_loader.go | 37 ++++---------- internal/epub/options/epub_options.go | 49 +++++++++++++++++++ .../epub/templates/epub_templates_content.go | 5 +- main.go | 32 ++++++------ 7 files changed, 102 insertions(+), 105 deletions(-) delete mode 100644 internal/epub/image/epub_image_options.go create mode 100644 internal/epub/options/epub_options.go diff --git a/internal/epub/epub.go b/internal/epub/epub.go index acb5286..14d50fd 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -15,6 +15,7 @@ import ( epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image" 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" epubtemplates "github.com/celogeek/go-comic-converter/v2/internal/epub/templates" epubtree "github.com/celogeek/go-comic-converter/v2/internal/epub/tree" @@ -22,23 +23,8 @@ import ( "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 { - *Options + *epuboptions.Options UID string Publisher string UpdatedAt string @@ -52,7 +38,7 @@ type epubPart struct { } // initialize epub -func New(options *Options) *ePub { +func New(options *epuboptions.Options) *ePub { uid := uuid.Must(uuid.NewV4()) tmpl := template.New("parser") 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()), []byte(e.render(epubtemplates.Text, map[string]any{ "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(), - "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), []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), + "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height), })), ) } // extract image and split it into part func (e *ePub) getParts() ([]*epubPart, error) { - loadedImages, err := epubimageprocessing.LoadImages(&epubimageprocessing.Options{ - Input: e.Input, - SortPathMode: e.SortPathMode, - Quiet: e.Quiet, - Dry: e.Dry, - Workers: e.Workers, - Image: e.Image, - }) + loadedImages, err := epubimageprocessing.LoadImages(e.Options) if err != nil { return nil, err @@ -277,14 +256,14 @@ func (e *ePub) Write() error { })}, {"OEBPS/toc.xhtml", epubtemplates.Toc(title, e.StripFirstDirectoryFromToc, part.LoadedImages.Images())}, {"OEBPS/Text/style.css", e.render(epubtemplates.Style, map[string]any{ - "PageWidth": e.Image.ViewWidth, - "PageHeight": e.Image.ViewHeight, + "PageWidth": e.Image.View.Width, + "PageHeight": e.Image.View.Height, })}, {"OEBPS/Text/title.xhtml", e.render(epubtemplates.Text, map[string]any{ "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", - "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), })}, } diff --git a/internal/epub/image/epub_image_options.go b/internal/epub/image/epub_image_options.go deleted file mode 100644 index ff0fd0c..0000000 --- a/internal/epub/image/epub_image_options.go +++ /dev/null @@ -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 -} diff --git a/internal/epub/imageprocessing/epub_image_processing.go b/internal/epub/imageprocessing/epub_image_processing.go index 605f3a9..f7fd975 100644 --- a/internal/epub/imageprocessing/epub_image_processing.go +++ b/internal/epub/imageprocessing/epub_image_processing.go @@ -12,6 +12,7 @@ import ( epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image" 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" epubzip "github.com/celogeek/go-comic-converter/v2/internal/epub/zip" "github.com/disintegration/gift" @@ -44,10 +45,10 @@ func isSupportedImage(path string) bool { } // extract and convert images -func LoadImages(o *Options) (LoadedImages, error) { +func LoadImages(o *epuboptions.Options) (LoadedImages, error) { images := make(LoadedImages, 0) - imageCount, imageInput, err := o.Load() + imageCount, imageInput, err := Load(o) if err != nil { 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 // 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 images []image.Image - if o.Crop { + if o.Crop.Enabled { f := epubimagefilters.AutoCrop( src, - o.CropRatioLeft, - o.CropRatioUp, - o.CropRatioRight, - o.CropRatioBottom, + o.Crop.Left, + o.Crop.Up, + o.Crop.Right, + o.Crop.Bottom, ) filters = append(filters, f) splitFilter = append(splitFilter, f) @@ -180,7 +181,7 @@ func TransformImage(src image.Image, srcId int, o *epubimage.Options) []image.Im } filters = append(filters, - epubimagefilters.Resize(o.ViewWidth, o.ViewHeight, gift.LanczosResampling), + epubimagefilters.Resize(o.View.Width, o.View.Height, gift.LanczosResampling), epubimagefilters.Pixel(), ) @@ -212,7 +213,7 @@ func TransformImage(src image.Image, srcId int, o *epubimage.Options) []image.Im g := gift.New(splitFilter...) g.Add( 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())) g.Draw(dst, src) diff --git a/internal/epub/imageprocessing/epub_image_processing_loader.go b/internal/epub/imageprocessing/epub_image_processing_loader.go index b3139d9..0148a81 100644 --- a/internal/epub/imageprocessing/epub_image_processing_loader.go +++ b/internal/epub/imageprocessing/epub_image_processing_loader.go @@ -18,7 +18,7 @@ import ( _ "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/nwaples/rardecode/v2" pdfimage "github.com/raff/pdfreader/image" @@ -32,27 +32,10 @@ type tasks struct { 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") -func (o *Options) WorkersRatio(pct int) (nbWorkers int) { - nbWorkers = o.Workers * pct / 100 - if nbWorkers < 1 { - nbWorkers = 1 - } - return -} - // 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) if err != nil { return @@ -60,15 +43,15 @@ func (o *Options) Load() (totalImages int, output chan *tasks, err error) { // get all images though a channel of bytes if fi.IsDir() { - return o.loadDir() + return loadDir(o) } else { switch ext := strings.ToLower(filepath.Ext(o.Input)); ext { case ".cbz", ".zip": - return o.loadCbz() + return loadCbz(o) case ".cbr", ".rar": - return o.loadCbr() + return loadCbr(o) case ".pdf": - return o.loadPdf() + return loadPdf(o) default: err = fmt.Errorf("unknown file format (%s): support .cbz, .zip, .cbr, .rar, .pdf", ext) return @@ -77,7 +60,7 @@ func (o *Options) Load() (totalImages int, output chan *tasks, err error) { } // 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) 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 -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) if err != nil { return @@ -253,7 +236,7 @@ func (o *Options) loadCbz() (totalImages int, output chan *tasks, err error) { } // 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 files, err := rardecode.List(o.Input) if err != nil { @@ -370,7 +353,7 @@ func (o *Options) loadCbr() (totalImages int, output chan *tasks, err error) { } // 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) if pdf == nil { err = fmt.Errorf("can't read pdf") diff --git a/internal/epub/options/epub_options.go b/internal/epub/options/epub_options.go new file mode 100644 index 0000000..d806297 --- /dev/null +++ b/internal/epub/options/epub_options.go @@ -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 +} diff --git a/internal/epub/templates/epub_templates_content.go b/internal/epub/templates/epub_templates_content.go index 6c4589f..ff594c5 100644 --- a/internal/epub/templates/epub_templates_content.go +++ b/internal/epub/templates/epub_templates_content.go @@ -5,6 +5,7 @@ import ( "github.com/beevik/etree" 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 { @@ -13,7 +14,7 @@ type ContentOptions struct { Author string Publisher string UpdatedAt string - ImageOptions *epubimage.Options + ImageOptions *epuboptions.Image Cover *epubimage.Image Images []*epubimage.Image Current int @@ -91,7 +92,7 @@ func getMeta(o *ContentOptions) []tag { {"meta", tagAttrs{"property": "schema:accessibilityHazard"}, "noSoundHazard"}, {"meta", tagAttrs{"name": "book-type", "content": "comic"}, ""}, {"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:identifier", tagAttrs{"id": "ean"}, fmt.Sprintf("urn:uuid:%s", o.UID)}, {"dc:language", tagAttrs{}, "en"}, diff --git a/main.go b/main.go index 6078b8c..94cde4f 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ import ( "github.com/celogeek/go-comic-converter/v2/internal/converter" "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" ) @@ -101,7 +101,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s profile := cmd.Options.GetProfile() perfectWidth, perfectHeight := profile.PerfectDim() - if err := epub.New(&epub.Options{ + if err := epub.New(&epuboptions.Options{ Input: cmd.Options.Input, Output: cmd.Options.Output, LimitMb: cmd.Options.LimitMb, @@ -109,15 +109,19 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s Author: cmd.Options.Author, StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc, SortPathMode: cmd.Options.SortPathMode, - Image: &epubimage.Options{ - ViewWidth: perfectWidth, - ViewHeight: perfectHeight, + Workers: cmd.Options.Workers, + Dry: cmd.Options.Dry, + 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, - Crop: cmd.Options.Crop, - CropRatioLeft: cmd.Options.CropRatioLeft, - CropRatioUp: cmd.Options.CropRatioUp, - CropRatioRight: cmd.Options.CropRatioRight, - CropRatioBottom: cmd.Options.CropRatioBottom, Brightness: cmd.Options.Brightness, Contrast: cmd.Options.Contrast, AutoRotate: cmd.Options.AutoRotate, @@ -125,11 +129,11 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s NoBlankPage: cmd.Options.NoBlankPage, Manga: cmd.Options.Manga, 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 { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1)