Created
June 30, 2016 16:06
-
-
Save uchida/cdefa45f0c0797fa887b48f85139df7b to your computer and use it in GitHub Desktop.
date sieve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"errors" | |
"log" | |
"os" | |
"time" | |
"strings" | |
"path/filepath" | |
"gopkg.in/fsnotify.v1" | |
"github.com/mitchellh/go-homedir" | |
) | |
func isValidFile(filename string) bool { | |
base := filepath.Base(filename) | |
ignores := []string {".DS_Store", ".AppleDouble"} | |
for _, ignore := range(ignores) { | |
if base == ignore { | |
return false | |
} | |
} | |
prefixes := []string {"."} | |
for _, prefix := range(prefixes) { | |
if strings.HasPrefix(base, prefix) { | |
return false | |
} | |
} | |
suffixes := []string {".part", ".download", ".crdownload", "~", ".swp", ".partial"} | |
for _, suffix := range(suffixes) { | |
if strings.HasSuffix(base, suffix) { | |
return false | |
} | |
} | |
f, err := os.Open(filename) | |
if err != nil { | |
log.Println(err) | |
return false | |
} | |
fi, err := f.Stat() | |
if err != nil { | |
log.Println(err) | |
return false | |
} | |
if fi.IsDir() || fi.Size() == 0 { | |
return false | |
} | |
return true | |
} | |
func exists(filename string) bool { | |
_, err := os.Stat(filename) | |
log.Println(err) | |
return err == nil | |
} | |
func move(fpath string, destdir string) error { | |
time.Sleep(100 * time.Millisecond) | |
if isValidFile(fpath) { | |
base := filepath.Base(fpath) | |
i, dest := 0, filepath.Join(destdir, base) | |
for exists(dest) { | |
if i == 10 { | |
log.Println("aaa") | |
return errors.New("") | |
} | |
ext := filepath.Ext(dest) | |
dest = strings.TrimSuffix(dest, ext) + fmt.Sprintf("-%d", i) + ext | |
i++ | |
} | |
if err := os.Rename(fpath, dest); err != nil { | |
log.Println(err) | |
return err | |
} | |
} | |
return nil | |
} | |
func createDir(destdir string) error { | |
if ! exists(destdir) { | |
if err := os.Mkdir(destdir, 0700); err != nil { | |
log.Println(err) | |
return err | |
} | |
return nil | |
} | |
return nil | |
} | |
func main() { | |
watcher, err := fsnotify.NewWatcher() | |
if err != nil { | |
os.Exit(1) | |
} | |
defer watcher.Close() | |
done := make(chan bool) | |
go func() { | |
for { | |
select { | |
case event := <-watcher.Events: | |
switch { | |
case event.Op & fsnotify.Create == fsnotify.Create: | |
fpath := event.Name | |
destdir := filepath.Join(filepath.Dir(fpath), time.Now().Format("2006-01-02")) | |
log.Println(filepath.Dir(event.Name)) | |
log.Println(destdir) | |
if createDir(destdir) == nil { | |
move(fpath, destdir) | |
} | |
case event.Op & fsnotify.Rename == fsnotify.Rename: | |
if strings.HasSuffix(event.Name, ".part") { | |
fpath := strings.TrimSuffix(event.Name, ".part") | |
destdir := filepath.Join(filepath.Dir(fpath), time.Now().Format("2006-01-02")) | |
log.Println(destdir) | |
if createDir(destdir) == nil { | |
move(fpath, destdir) | |
} | |
} | |
} | |
} | |
} | |
}() | |
dir, err := homedir.Expand("~/Downloads") | |
if err != nil { | |
os.Exit(1) | |
} | |
if watcher.Add(dir) != nil { | |
os.Exit(1) | |
} | |
<-done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment