/
main.go
181 lines (150 loc) · 3.6 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"fmt"
"image"
"log"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/disintegration/imaging"
)
// Pipeline
// walkfile ----------> processImage -----------> saveImage
// (paths) (results)
type result struct {
srcImagePath string
thumbnailImage *image.NRGBA
err error
}
// Image processing - Pipeline
// Input - directory with images.
// output - thumbnail images
func main() {
if len(os.Args) < 2 {
log.Fatal("need to send directory path of images")
}
start := time.Now()
err := setupPipeLine(os.Args[1])
if err != nil {
log.Fatal(err)
}
fmt.Printf("Time taken: %s\n", time.Since(start))
}
func setupPipeLine(root string) error {
done := make(chan struct{})
defer close(done)
// do the file walk
paths, errc := walkFiles(done, root)
// process the image
results := processImage(done, paths)
// save thumbnail images
for r := range results {
if r.err != nil {
return r.err
}
saveThumbnail(r.srcImagePath, r.thumbnailImage)
}
// check for error on the channel, from walkfiles stage.
if err := <-errc; err != nil {
return err
}
return nil
}
func walkFiles(done <-chan struct{}, root string) (<-chan string, <-chan error) {
// create output channels
paths := make(chan string)
errc := make(chan error, 1)
go func() {
defer close(paths)
errc <- filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
// filter out error
if err != nil {
return err
}
// check if it is file
if !info.Mode().IsRegular() {
return nil
}
// check if it is image/jpeg
contentType, _ := getFileContentType(path)
if contentType != "image/jpeg" {
return nil
}
// send file path to next stage
select {
case paths <- path:
case <-done:
return fmt.Errorf("walk cancelled")
}
return nil
})
}()
return paths, errc
}
func processImage(done <-chan struct{}, paths <-chan string) <-chan result {
results := make(chan result)
var wg sync.WaitGroup
thumbnailer := func() {
for srcImagePath := range paths {
srcImage, err := imaging.Open(srcImagePath)
if err != nil {
select {
case results <- result{srcImagePath, nil, err}:
case <-done:
return
}
}
thumbnailImage := imaging.Thumbnail(srcImage, 100, 100, imaging.Lanczos)
select {
case results <- result{srcImagePath, thumbnailImage, err}:
case <-done:
return
}
}
}
const numThumbnailer = 5
for i := 0; i < numThumbnailer; i++ {
wg.Add(1)
go func() {
thumbnailer()
wg.Done()
}()
}
go func() {
wg.Wait()
close(results)
}()
return results
}
// saveThumbnail - save the thumnail image to folder
func saveThumbnail(srcImagePath string, thumbnailImage *image.NRGBA) error {
filename := filepath.Base(srcImagePath)
dstImagePath := "thumbnail/" + filename
// save the image in the thumbnail folder.
err := imaging.Save(thumbnailImage, dstImagePath)
if err != nil {
return err
}
fmt.Printf("%s -> %s\n", srcImagePath, dstImagePath)
return nil
}
// getFileContentType - return content type and error status
func getFileContentType(file string) (string, error) {
out, err := os.Open(file)
if err != nil {
return "", err
}
defer out.Close()
// Only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512)
_, err = out.Read(buffer)
if err != nil {
return "", err
}
// Use the net/http package's handy DectectContentType function. Always returns a valid
// content-type by returning "application/octet-stream" if no others seemed to match.
contentType := http.DetectContentType(buffer)
return contentType, nil
}