-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (96 loc) · 2.43 KB
/
main.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/disintegration/imaging"
)
var thumbDir string
var thumbWidth int
var thumbHeight int
func parsePath(pth string) {
file, err := os.Open(pth)
if err != nil {
log.Printf("[ERR] Cannot open path/file: %v\n", err)
os.Exit(1)
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
log.Printf("[ERR] Cannot get path/file info: %v\n", err)
os.Exit(1)
}
switch mode := fi.Mode(); {
case mode.IsDir():
files := listDirContents(pth)
for _, file := range files {
fullPath := path.Join(pth, file.Name())
parseFile(fullPath)
}
case mode.IsRegular():
parseFile(pth)
}
}
func parseFile(fullPath string) {
if checkExt(fullPath) {
createThumbs(fullPath)
} else {
log.Printf("[INF] Not Image: %s\n", fullPath)
}
}
func checkExt(pth string) bool {
supportedTypes := []string{".jpg", ".jepg", ".png", ".gif"}
for _, typ := range supportedTypes {
if path.Ext(pth) == typ {
return true
}
}
return false
}
func createThumbs(pth string) {
img, err := imaging.Open(pth)
if err != nil {
log.Printf("[ERR] Unalbe to open image: %v\n", err)
return
}
thumb := imaging.Resize(img, thumbWidth, thumbHeight, imaging.Box)
splitPath := strings.Split(pth, "/")
thumbPath := path.Join(thumbDir, splitPath[len(splitPath)-1])
if err = imaging.Save(thumb, thumbPath); err != nil {
log.Printf("[ERR] Unable to save thumbnail image: %v\n", err)
return
}
log.Printf("[INF] Created Thumbnail: %s\n", thumbPath)
}
func listDirContents(path string) []os.FileInfo {
files, err := ioutil.ReadDir(path)
if err != nil {
log.Printf("[ERR] Unable to read directory contents: %v\n", err)
os.Exit(1)
}
return files
}
func parseFlags() (inp, out string, w, h int) {
inputPath := flag.String("i", "", "Path to image directory or single file")
outPath := flag.String("o", "", "Path to thumbnail output directory (Needs to exist)")
width := flag.Int("w", 0, "Thumbnail Width. If 0 then will keep aspect ratio based on the height")
height := flag.Int("t", 0, "Thumbnail Height. If 0 then will keep aspect ratio based on the width")
flag.Parse()
return *inputPath, *outPath, *width, *height
}
func checkTumbDir(pth string) {
if _, err := os.Stat(pth); os.IsNotExist(err) {
log.Printf("Unable to get thumbnail directory: %v\n", err)
os.Exit(1)
}
}
func main() {
in, d, w, h := parseFlags()
thumbDir = d
thumbWidth = w
thumbHeight = h
parsePath(in)
}