choose what to arrange: -kinds, -exclude, am survey

-kinds picks any of image, raw, video (default all). -exclude skips
files and whole directories by name glob, and can be repeated. am
survey stats media per directory (counts by kind, videos over 1G,
sizes) so things like ripped DVDs stand out before arranging. It
never opens files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sm
2026-10-26 15:57:23 -07:00
co-authored by Claude Opus 5.5
parent 6d971118b7
commit f63b855179
6 changed files with 270 additions and 15 deletions
+65 -10
View File
@@ -25,11 +25,39 @@ const (
kindJPEG kind = iota
kindPNG
kindGIF
kindStill // HEIC, camera RAW, etc.: dated by exif, no decode check
kindStill // HEIC, TIFF, etc.: dated by exif, no decode check
kindRaw // camera RAW: like kindStill, but its own Class
kindQuickTime // mov/mp4 family: dated by the mvhd atom
kindVideo // everything else: dated by mtime
)
// Class is the coarse sort of media a file is, for choosing what to arrange.
type Class string
// The classes of media.
const (
Image Class = "image"
Raw Class = "raw"
Video Class = "video"
)
func (k kind) class() Class {
switch k {
case kindRaw:
return Raw
case kindQuickTime, kindVideo:
return Video
}
return Image
}
// ClassOf reports the Class of path by its extension, and whether it is
// media at all.
func ClassOf(path string) (Class, bool) {
k, ok := exts[strings.ToLower(filepath.Ext(path))]
return k.class(), ok
}
var exts = map[string]kind{
// images
".jpg": kindJPEG,
@@ -41,11 +69,11 @@ var exts = map[string]kind{
".avif": kindStill,
".tif": kindStill,
".tiff": kindStill,
".dng": kindStill,
".cr2": kindStill,
".cr3": kindStill,
".nef": kindStill,
".arw": kindStill,
".dng": kindRaw,
".cr2": kindRaw,
".cr3": kindRaw,
".nef": kindRaw,
".arw": kindRaw,
// videos
".mov": kindQuickTime,
@@ -101,9 +129,36 @@ func PrepOutput(root string) error {
return nil
}
// Filter narrows what Source sends.
type Filter struct {
// Classes to include; empty means all.
Classes map[Class]bool
// Exclude skips files and whole directories whose name matches any of
// these filepath.Match patterns, e.g. "VIDEO_TS" or "*.vob".
Exclude []string
}
func (f Filter) excluded(name string) bool {
for _, p := range f.Exclude {
if ok, _ := filepath.Match(p, name); ok {
return true
}
}
return false
}
func (f Filter) wants(c Class) bool {
return len(f.Classes) == 0 || f.Classes[c]
}
// Source sends all files that match known extensions. Unreadable paths are
// logged and skipped rather than ending the crawl.
func Source(root string) <-chan string {
return SourceFiltered(root, Filter{})
}
// SourceFiltered is Source, sending only the files f lets through.
func SourceFiltered(root string, f Filter) <-chan string {
out := make(chan string)
go func() {
err := filepath.WalkDir(
@@ -119,16 +174,16 @@ func Source(root string) <-chan string {
name := d.Name()
if d.IsDir() {
// QNAP thumbnail caches
if name == ".@__thumb" {
if name == ".@__thumb" || (path != root && f.excluded(name)) {
return filepath.SkipDir
}
return nil
}
// AppleDouble resource forks
if strings.HasPrefix(name, "._") {
if strings.HasPrefix(name, "._") || f.excluded(name) {
return nil
}
if _, ok := exts[strings.ToLower(filepath.Ext(path))]; ok {
if c, ok := ClassOf(path); ok && f.wants(c) {
out <- path
}
return nil
@@ -261,7 +316,7 @@ func ParseFile(path string) (Media, error) {
// try a few things for a time value, falling back to mtime
var t time.Time
switch k {
case kindJPEG, kindPNG, kindStill:
case kindJPEG, kindPNG, kindStill, kindRaw:
t = exifTime(f, k == kindJPEG)
case kindQuickTime:
if _, err := f.Seek(0, io.SeekStart); err == nil {