From 5d87cd59b66b8fc35aa6c29e3c5fc8fdfa5c094e Mon Sep 17 00:00:00 2001 From: Milan Nikolic Date: Tue, 25 Aug 2026 12:05:24 +0200 Subject: [PATCH] Copy non-image files out of a directory too --- cbconvert_convert.go | 22 ++++++++++++++-------- cbconvert_func.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/cbconvert_convert.go b/cbconvert_convert.go index b889325..797db48 100644 --- a/cbconvert_convert.go +++ b/cbconvert_convert.go @@ -187,7 +187,7 @@ func (c *Converter) convertArchive(ctx context.Context, fileName string) error { // convertDirectory converts directory to CBZ. func (c *Converter) convertDirectory(ctx context.Context, dirPath string) error { - contents, err := imagesFromPath(dirPath) + contents, err := filesFromPath(dirPath) if err != nil { return fmt.Errorf("convertDirectory: %w", err) } @@ -215,13 +215,7 @@ func (c *Converter) convertDirectory(ctx context.Context, dirPath string) error } defer file.Close() - if isNonImage(img) && c.prefix == "" && !c.Opts.NoNonImage { - if err = copyFile(file, c.workPath(flatName(rel))); err != nil { - return fmt.Errorf("convertDirectory: %w", err) - } - - return nil - } else if isImage(img) { + if isImage(img) { if c.Opts.NoConvert { if err = copyFile(file, c.workPath(flatName(rel))); err != nil { return fmt.Errorf("convertDirectory: %w", err) @@ -249,6 +243,18 @@ func (c *Converter) convertDirectory(ctx context.Context, dirPath string) error return c.imageConvert(ctx, i, index, rel) }) } + + return nil + } + + if filepath.Ext(img) == ".DS_Store" || strings.Contains(img, "__MACOSX") { + return nil + } + + if c.prefix == "" && !c.Opts.NoNonImage { + if err = copyFile(file, c.workPath(flatName(rel))); err != nil { + return fmt.Errorf("convertDirectory: %w", err) + } } return nil diff --git a/cbconvert_func.go b/cbconvert_func.go index 1e63a0c..0ebc37a 100644 --- a/cbconvert_func.go +++ b/cbconvert_func.go @@ -59,6 +59,37 @@ func imagesFromSlice(files []string) []string { return images } +// filesFromPath returns list of all found files for given directory. +func filesFromPath(path string) ([]string, error) { + var files []string + + walkFiles := func(fp string, f os.FileInfo, err error) error { + if !f.IsDir() && f.Mode()&os.ModeType == 0 && f.Size() > 0 { + files = append(files, fp) + } + + return nil + } + + f, err := filepath.Abs(path) + if err != nil { + return files, fmt.Errorf("filesFromPath: %w", err) + } + + stat, err := os.Stat(f) + if err != nil { + return files, fmt.Errorf("filesFromPath: %w", err) + } + + if !stat.IsDir() && stat.Mode()&os.ModeType == 0 { + files = append(files, f) + } else if err = filepath.Walk(f, walkFiles); err != nil { + return files, fmt.Errorf("filesFromPath: %w", err) + } + + return files, nil +} + // isArchive checks if a file is archive. func isArchive(f string) bool { var types = []string{".rar", ".zip", ".7z", ".tar", ".cbr", ".cbz", ".cb7", ".cbt"}