From ad614d09b4378496834ec9d1c7b4dacbc4152530 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 16 Feb 2025 12:50:25 +0100 Subject: [PATCH] passthrough, support for dir --- .../pkg/epubimageprocessor/passthrough.go | 175 +++++++++++++++++- 1 file changed, 172 insertions(+), 3 deletions(-) diff --git a/internal/pkg/epubimageprocessor/passthrough.go b/internal/pkg/epubimageprocessor/passthrough.go index 672769a..21aea68 100644 --- a/internal/pkg/epubimageprocessor/passthrough.go +++ b/internal/pkg/epubimageprocessor/passthrough.go @@ -1,8 +1,177 @@ package epubimageprocessor -import "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimage" +import ( + "bytes" + "fmt" + "image" + "image/jpeg" + "io" + "io/fs" + "os" + "path/filepath" + "slices" + "strings" + + "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimage" + "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubprogress" + "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubzip" +) func (e EPUBImageProcessor) PassThrough() (images []epubimage.EPUBImage, err error) { - images = make([]epubimage.EPUBImage, 0) - return images, nil + fi, err := os.Stat(e.Input) + if err != nil { + return + } + + if fi.IsDir() { + return e.passThroughDir() + } else { + switch ext := strings.ToLower(filepath.Ext(e.Input)); ext { + case ".cbz", ".zip": + return e.passThroughCbz() + case ".cbr", ".rar": + return e.passThroughCbr() + default: + return nil, fmt.Errorf("unknown file format (%s): support .cbz, .zip, .cbr, .rar", ext) + } + } + +} + +func (e EPUBImageProcessor) passThroughDir() (images []epubimage.EPUBImage, err error) { + imagesPath := make([]string, 0) + + input := filepath.Clean(e.Input) + err = filepath.WalkDir(input, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + // skip hidden files + if strings.HasPrefix(filepath.Base(path), ".") { + return nil + } + + if d.IsDir() { + return nil + } + + if slices.Contains([]string{".jpeg", ".jpg"}, strings.ToLower(filepath.Ext(path))) { + imagesPath = append(imagesPath, path) + } + return nil + }) + + if err != nil { + return + } + + if len(imagesPath) == 0 { + err = errNoImagesFound + return + } + + var imgStorage epubzip.StorageImageWriter + imgStorage, err = epubzip.NewStorageImageWriter(e.ImgStorage(), e.Image.Format) + if err != nil { + return + } + + // processing + bar := epubprogress.New(epubprogress.Options{ + Quiet: e.Quiet, + Json: e.Json, + Max: len(imagesPath), + Description: "Copying", + CurrentJob: 1, + TotalJob: 2, + }) + + for i, imgPath := range imagesPath { + var f *os.File + f, err = os.Open(imgPath) + if err != nil { + return + } + + var uncompressedData []byte + uncompressedData, err = io.ReadAll(f) + if err != nil { + return + } + + err = f.Close() + if err != nil { + return + } + + var config image.Config + config, err = jpeg.DecodeConfig(bytes.NewReader(uncompressedData)) + if err != nil { + return + } + + var rawImage image.Image + if i == 0 { + rawImage, err = jpeg.Decode(bytes.NewReader(uncompressedData)) + if err != nil { + return + } + } + + p, fn := filepath.Split(imgPath) + if p == input { + p = "" + } else { + p = p[len(input)+1:] + } + + img := epubimage.EPUBImage{ + Id: i, + Part: 0, + Raw: rawImage, + Width: config.Width, + Height: config.Height, + IsBlank: false, + DoublePage: config.Width > config.Height, + Path: p, + Name: fn, + Format: "jpeg", + OriginalAspectRatio: float64(config.Height) / float64(config.Width), + } + + err = imgStorage.AddRaw(img.EPUBImgPath(), uncompressedData) + if err != nil { + return + } + + images = append(images, img) + _ = bar.Add(1) + } + + err = imgStorage.Close() + if err != nil { + return + } + + _ = bar.Close() + + if len(images) == 0 { + err = errNoImagesFound + } + + return + +} + +func (e EPUBImageProcessor) passThroughCbz() (images []epubimage.EPUBImage, err error) { + images = make([]epubimage.EPUBImage, 0) + err = errNoImagesFound + return +} + +func (e EPUBImageProcessor) passThroughCbr() (images []epubimage.EPUBImage, err error) { + images = make([]epubimage.EPUBImage, 0) + err = errNoImagesFound + return }