You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.2 KiB
66 lines
1.2 KiB
package core
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// CollectImageFiles walks root and returns all supported image files,
|
|
// optionally filtered by a glob pattern applied to the bare filename.
|
|
func CollectImageFiles(root, glob string) (*ImageSummary, error) {
|
|
validExts := map[string]bool{
|
|
".jpg": true,
|
|
".jpeg": true,
|
|
".png": true,
|
|
".webp": true,
|
|
".tif": true,
|
|
".tiff": true,
|
|
".bmp": true,
|
|
}
|
|
|
|
readOnlyExts := map[string]bool{
|
|
".webp": true,
|
|
".bmp": true,
|
|
}
|
|
|
|
summary := &ImageSummary{
|
|
ByExt: make(map[string]int),
|
|
}
|
|
|
|
seen := map[string]bool{}
|
|
|
|
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
ext := strings.ToLower(filepath.Ext(path))
|
|
if !validExts[ext] {
|
|
return nil
|
|
}
|
|
|
|
if glob != "" {
|
|
matched, merr := filepath.Match(glob, filepath.Base(path))
|
|
if merr != nil || !matched {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
summary.Files = append(summary.Files, path)
|
|
summary.ByExt[ext]++
|
|
|
|
if readOnlyExts[ext] && !seen[ext] {
|
|
summary.ReadOnlyExts = append(summary.ReadOnlyExts, ext)
|
|
seen[ext] = true
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return summary, err
|
|
}
|