From 02f86eb55ed09c4ee40193b0d3ee6edc6b51a360 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Thu, 27 Apr 2023 13:46:00 +0200 Subject: [PATCH] move load to options as an helper --- .../imageprocessing/epub_image_processing.go | 28 +------------------ .../epub_image_processing_loader.go | 25 +++++++++++++++++ 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/internal/epub/imageprocessing/epub_image_processing.go b/internal/epub/imageprocessing/epub_image_processing.go index bedc149..301b825 100644 --- a/internal/epub/imageprocessing/epub_image_processing.go +++ b/internal/epub/imageprocessing/epub_image_processing.go @@ -4,9 +4,7 @@ Extract and transform image into a compressed jpeg. package epubimageprocessing import ( - "fmt" "image" - "os" "path/filepath" "strings" "sync" @@ -33,31 +31,7 @@ func isSupportedImage(path string) bool { func LoadImages(o *Options) ([]*epubimage.Image, error) { images := make([]*epubimage.Image, 0) - fi, err := os.Stat(o.Input) - if err != nil { - return nil, err - } - - var ( - imageCount int - imageInput chan *tasks - ) - - // get all images though a channel of bytes - if fi.IsDir() { - imageCount, imageInput, err = o.loadDir() - } else { - switch ext := strings.ToLower(filepath.Ext(o.Input)); ext { - case ".cbz", ".zip": - imageCount, imageInput, err = o.loadCbz() - case ".cbr", ".rar": - imageCount, imageInput, err = o.loadCbr() - case ".pdf": - imageCount, imageInput, err = o.loadPdf() - default: - err = fmt.Errorf("unknown file format (%s): support .cbz, .zip, .cbr, .rar, .pdf", ext) - } - } + imageCount, imageInput, err := o.Load() if err != nil { return nil, err } diff --git a/internal/epub/imageprocessing/epub_image_processing_loader.go b/internal/epub/imageprocessing/epub_image_processing_loader.go index 24db36f..3306fe0 100644 --- a/internal/epub/imageprocessing/epub_image_processing_loader.go +++ b/internal/epub/imageprocessing/epub_image_processing_loader.go @@ -13,6 +13,7 @@ import ( "os" "path/filepath" "sort" + "strings" "sync" _ "golang.org/x/image/webp" @@ -42,6 +43,30 @@ type Options struct { var errNoImagesFound = errors.New("no images found") +func (o *Options) Load() (totalImages int, output chan *tasks, err error) { + fi, err := os.Stat(o.Input) + if err != nil { + return + } + + // get all images though a channel of bytes + if fi.IsDir() { + return o.loadDir() + } else { + switch ext := strings.ToLower(filepath.Ext(o.Input)); ext { + case ".cbz", ".zip": + return o.loadCbz() + case ".cbr", ".rar": + return o.loadCbr() + case ".pdf": + return o.loadPdf() + default: + err = fmt.Errorf("unknown file format (%s): support .cbz, .zip, .cbr, .rar, .pdf", ext) + return + } + } +} + // load a directory of images func (o *Options) loadDir() (totalImages int, output chan *tasks, err error) { images := make([]string, 0)