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"
)
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 {
Move(root string) error
}
@ -30,7 +47,7 @@ func PrepOutput(root string) error {
return nil
}
func Source(root string, exts map[string]bool) <-chan string {
func Source(root string) <-chan string {
out := make(chan string)
go func() {
err := filepath.Walk(
@ -42,8 +59,6 @@ func Source(root string, exts map[string]bool) <-chan string {
ext := strings.ToLower(filepath.Ext(path))
if _, ok := exts[ext]; ok {
out <- path
} else {
log.Printf("ignoring: %q", path)
}
return nil
},
@ -118,7 +133,6 @@ func _parse(path string) (File, error) {
success = true
}
if !success {
log.Printf("no exif for %q: %+v", path, err)
t, err = mtime(path)
}
if err != nil {
@ -136,9 +150,7 @@ func _parse(path string) (File, error) {
r = Image{
Path: path,
Hash: fmt.Sprintf("%x", hash.Sum(nil)),
Year: fmt.Sprintf("%04d", t.Year()),
Month: fmt.Sprintf("%02d", t.Month()),
Time: fmt.Sprintf("%d", t.UnixNano()),
Time: t,
}
case ".png":
return nil, fmt.Errorf("NYI: %q", path)

View File

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

View File

@ -22,9 +22,7 @@ func (m Media) Move(root string) error {
type Image struct {
Path string
Hash string
Year string
Month string
Time string
Time time.Time
}
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 {
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)
}
date := filepath.Join(root, "date", im.Year, im.Month, im.Time)
date := filepath.Join(root, "date", year, month, ts)
name := date + ".jpg"
for i := 0; i < 10000; i++ {
if _, err := os.Stat(name); os.IsNotExist(err) {