-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathautothumb.go
More file actions
62 lines (58 loc) · 1.35 KB
/
autothumb.go
File metadata and controls
62 lines (58 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
/**
Auto thumbnail based on file system notification(fsnotify) and imagemagic with golang
*/
import (
"log"
"os/exec"
"strings"
"flag"
"github.com/howeyc/fsnotify"
"time"
)
func main() {
log.Println("start autothumb")
folder := flag.String("folder", "/var/www/images", "the folder to monitor")
target := flag.String("target", "/var/www/images/thumbnail", "target thumbs path")
size := flag.String("size", "341x267", "thumb image size")
wait := flag.Int64("wait", 10, "Time to wait the image written done before generating thumb image")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
done := make(chan bool)
// Process events
go func() {
for {
select {
case ev := <-watcher.Event:
if ev.IsCreate() {
time.Sleep(time.Duration(*wait)*time.Second)
f := ev.Name
i := strings.LastIndexAny(f, ".")
log.Println(f)
if i != -1 {
format := f[i + 1:]
//log.Println(format, f)
err := exec.Command("mogrify", format, "jpg", "-path", *target, "-thumbnail", *size, f).Run()
if err != nil {
log.Println(err)
}
}
}
case err := <-watcher.Error:
log.Println("error:", err)
}
}
done<-true
}()
err = watcher.Watch(*folder)
if err != nil {
log.Fatal(err)
}
select {
case <-done:
watcher.Close()
log.Println("done,exit autothumb")
}
}