move load to options as an helper

This commit is contained in:
Celogeek 2023-04-27 13:46:00 +02:00
parent 4553e1e673
commit 02f86eb55e
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
2 changed files with 26 additions and 27 deletions

View File

@ -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
}

View File

@ -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)