cores flag, move supported extensions

This commit is contained in:
Stephen McQuay 2016-05-18 00:19:39 -07:00
parent 598a574685
commit a37d88f99f
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D
3 changed files with 45 additions and 33 deletions

View File

@ -13,6 +13,23 @@ import (
"time" "time"
) )
var exts map[string]bool
func init() {
exts = map[string]bool{
// images
".jpg": true,
".jpeg": true,
".png": true,
".gif": true,
// videos
".mov": true,
".mp4": true,
".m4v": true,
}
}
type File interface { type File interface {
Move(root string) error Move(root string) error
} }
@ -30,7 +47,7 @@ func PrepOutput(root string) error {
return nil return nil
} }
func Source(root string, exts map[string]bool) <-chan string { func Source(root string) <-chan string {
out := make(chan string) out := make(chan string)
go func() { go func() {
err := filepath.Walk( err := filepath.Walk(
@ -42,8 +59,6 @@ func Source(root string, exts map[string]bool) <-chan string {
ext := strings.ToLower(filepath.Ext(path)) ext := strings.ToLower(filepath.Ext(path))
if _, ok := exts[ext]; ok { if _, ok := exts[ext]; ok {
out <- path out <- path
} else {
log.Printf("ignoring: %q", path)
} }
return nil return nil
}, },
@ -118,7 +133,6 @@ func _parse(path string) (File, error) {
success = true success = true
} }
if !success { if !success {
log.Printf("no exif for %q: %+v", path, err)
t, err = mtime(path) t, err = mtime(path)
} }
if err != nil { if err != nil {
@ -134,11 +148,9 @@ func _parse(path string) (File, error) {
return nil, fmt.Errorf("problem calculating checksum on %q: %v", path, err) return nil, fmt.Errorf("problem calculating checksum on %q: %v", path, err)
} }
r = Image{ r = Image{
Path: path, Path: path,
Hash: fmt.Sprintf("%x", hash.Sum(nil)), Hash: fmt.Sprintf("%x", hash.Sum(nil)),
Year: fmt.Sprintf("%04d", t.Year()), Time: t,
Month: fmt.Sprintf("%02d", t.Month()),
Time: fmt.Sprintf("%d", t.UnixNano()),
} }
case ".png": case ".png":
return nil, fmt.Errorf("NYI: %q", path) return nil, fmt.Errorf("NYI: %q", path)

View File

@ -1,9 +1,11 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"log" "log"
"os" "os"
"runtime"
"mcquay.me/arrange" "mcquay.me/arrange"
) )
@ -16,36 +18,31 @@ type stats struct {
moved int moved int
} }
var cores = flag.Int("cores", 0, "how many threads to use")
func main() { func main() {
flag.Parse()
log.SetFlags(log.Lshortfile) log.SetFlags(log.Lshortfile)
if len(os.Args) != 3 { if len(flag.Args()) != 2 {
fmt.Fprintf(os.Stderr, "%s\n", usage) fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1) os.Exit(1)
} }
in, out := os.Args[1], os.Args[2] in, out := flag.Args()[0], flag.Args()[1]
if err := arrange.PrepOutput(out); err != nil { if err := arrange.PrepOutput(out); err != nil {
fmt.Fprintf(os.Stderr, "problem creating directory structure: %v", err) fmt.Fprintf(os.Stderr, "problem creating directory structure: %v", err)
os.Exit(1) os.Exit(1)
} }
exts := map[string]bool{ work := arrange.Source(in)
// images
".jpg": true,
".jpeg": true,
".png": true,
".gif": true,
// videos
".mov": true,
".mp4": true,
".m4v": true,
}
work := arrange.Source(in, exts)
streams := []<-chan arrange.File{} streams := []<-chan arrange.File{}
for w := 0; w < 16; w++ { workers := runtime.NumCPU()
if *cores != 0 {
workers = *cores
}
for w := 0; w < workers; w++ {
streams = append(streams, arrange.Parse(work)) streams = append(streams, arrange.Parse(work))
} }

View File

@ -20,11 +20,9 @@ func (m Media) Move(root string) error {
} }
type Image struct { type Image struct {
Path string Path string
Hash string Hash string
Year string Time time.Time
Month string
Time string
} }
func (im Image) Move(root string) error { func (im Image) Move(root string) error {
@ -49,11 +47,16 @@ func (im Image) Move(root string) error {
if _, err := io.Copy(out, f); err != nil { if _, err := io.Copy(out, f); err != nil {
return fmt.Errorf("trouble copying file: %v", err) return fmt.Errorf("trouble copying file: %v", err)
} }
if err := os.MkdirAll(filepath.Join(root, "date", im.Year, im.Month), 0755); err != nil {
year := fmt.Sprintf("%04d", im.Time.Year())
month := fmt.Sprintf("%02d", im.Time.Month())
ts := fmt.Sprintf("%d", im.Time.UnixNano())
if err := os.MkdirAll(filepath.Join(root, "date", year, month), 0755); err != nil {
return fmt.Errorf("problem creating date directory: %v", err) return fmt.Errorf("problem creating date directory: %v", err)
} }
date := filepath.Join(root, "date", im.Year, im.Month, im.Time) date := filepath.Join(root, "date", year, month, ts)
name := date + ".jpg" name := date + ".jpg"
for i := 0; i < 10000; i++ { for i := 0; i < 10000; i++ {
if _, err := os.Stat(name); os.IsNotExist(err) { if _, err := os.Stat(name); os.IsNotExist(err) {