-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (111 loc) · 2.76 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
package main
import (
"flag"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"os"
"strings"
"sync"
"time"
)
var (
toType *string
converters = map[string]func(io.Writer, io.Reader) error{
"png": convertToPNG,
"jpeg": convertToJPEG,
"gif": convertToGIF,
}
)
type Elf struct{}
func (e Elf) ConvertImage(imagePath string, orderErr chan error, wg *sync.WaitGroup) {
defer func() {
time.Sleep(400 * time.Millisecond)
wg.Done()
}()
fi, err := os.Open(imagePath)
if err != nil {
orderErr <- err
return
}
z := strings.SplitN(imagePath, ".", 2)
fo, err := os.Create(fmt.Sprintf("%v.%v", z[0], *toType))
if err != nil {
orderErr <- err
return
}
if err := converters[*toType](fo, fi); err != nil {
orderErr <- err
return
}
}
func main() {
// flag definition and parsing
toType = flag.String("to", "png", "To defines what type you'd like your images to be.")
flag.Parse()
// Elven feedback
fmt.Println("The elves are processing your request...")
time.Sleep(400 * time.Millisecond)
// Processing
// check parse errors
if _, ok := converters[*toType]; !ok {
fmt.Printf("Papa Elf: Sorry we don't convert to that type of image format. \nType entered: %v\n", *toType)
return
}
// Elven feedback
fmt.Println("Papa Elf is distributing your images...")
time.Sleep(400 * time.Millisecond)
elves := make([]Elf, len(flag.Args()))
orderErr := make(chan error)
var wg sync.WaitGroup
for i, arg := range flag.Args() {
wg.Add(1)
filename := arg
go elves[i].ConvertImage(filename, orderErr, &wg)
}
errorList := []error{}
go func() {
for err := range orderErr {
errorList = append(errorList, err)
}
}()
wg.Wait()
if len(errorList) == 0 {
// Elven feedback
fmt.Printf("Papa Elf: Here's your order for %v %v images...\n", len(flag.Args()), *toType)
} else {
// Elven feedback
fmt.Printf("Papa Elf: There were some hiccups with your order, we've delivered the %v images that we were able to complete...\n", *toType)
fmt.Println("Here's the list of problems we encountered:")
for _, err := range errorList {
fmt.Println("\t- ", err)
}
}
}
// convertToGIF converts from any recognized format to GIF.
func convertToGIF(w io.Writer, r io.Reader) error {
img, _, err := image.Decode(r)
if err != nil {
return err
}
return gif.Encode(w, img, &gif.Options{NumColors: 256})
}
// convertToJPEG converts from any recognized format to JPEG.
func convertToJPEG(w io.Writer, r io.Reader) error {
img, _, err := image.Decode(r)
if err != nil {
return err
}
return jpeg.Encode(w, img, &jpeg.Options{Quality: 100})
}
// convertToPNG converts from any recognized format to PNG.
func convertToPNG(w io.Writer, r io.Reader) error {
img, _, err := image.Decode(r)
if err != nil {
return err
}
return png.Encode(w, img)
}