-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconvert.go
164 lines (146 loc) · 2.96 KB
/
convert.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
package main
import (
"errors"
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"sync"
"strings"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
"github.com/chai2010/webp"
)
type FileType int
const (
PNG FileType = iota
JPG
GIF
WEBP
BMP
TIFF
ERR
)
func getFileType(input string) FileType {
switch input {
case "jpg":
fallthrough
case "jpeg":
return JPG
case "png":
return PNG
case "gif":
return GIF
case "bmp":
return BMP
case "webp":
return WEBP
case "tiff":
return TIFF
default:
return ERR
}
}
func getFileExtension(input FileType) string {
switch input {
case JPG:
return "jpg"
case PNG:
return "png"
case GIF:
return "gif"
case BMP:
return "bmp"
case WEBP:
return "webp"
case TIFF:
return "tiff"
default:
return ""
}
}
func convert(files []string, outputDir string, fileType FileType) {
var wg sync.WaitGroup
for _, currPath := range files {
wg.Add(1)
go convertFile(&wg, currPath, outputDir, fileType)
}
wg.Wait()
}
func convertFile(wg *sync.WaitGroup, currPath string, outputDir string, fileType FileType) {
// call done when finished
defer wg.Done()
ext := strings.ToLower(filepath.Ext(currPath))
newExt := getFileExtension(fileType)
_, filename := filepath.Split(currPath)
filenameNoExt := filename[0 : len(filename)-len(ext)]
newFileName := filenameNoExt + "." + newExt
newFilePath := outputDir + "/" + newFileName
// validate file type
startType := getFileType(ext[1:])
if startType == ERR {
logAndExit(errors.New("input file type not valid"))
}
// open files
file, err := os.Open(currPath)
if err != nil {
logAndExit(err)
}
defer file.Close()
outFile := openOrCreate(newFilePath)
defer outFile.Close()
// decode
imageData, _, err := image.Decode(file)
if err != nil {
logAndExit(errors.New("error decoding image"))
}
// encode in new type
switch fileType {
case JPG:
err := jpeg.Encode(outFile, imageData, nil)
if err != nil {
logAndExit(errors.New("error converting to jpeg"))
}
case PNG:
err := png.Encode(outFile, imageData)
if err != nil {
logAndExit(err)
}
case WEBP:
if err := webp.Encode(outFile, imageData, nil); err != nil {
logAndExit(errors.New("error converting to webp"))
}
case GIF:
err := gif.Encode(outFile, imageData, nil)
if err != nil {
logAndExit(errors.New("error converting to gif"))
}
case BMP:
err := bmp.Encode(outFile, imageData)
if err != nil {
logAndExit(errors.New("error converting to bmp"))
}
case TIFF:
err := tiff.Encode(outFile, imageData, nil)
if err != nil {
logAndExit(errors.New("error converting to tiff"))
}
}
}
func openOrCreate(filename string) *os.File {
if _, err := os.Stat(filename); os.IsNotExist(err) {
file, err := os.Create(filename)
if err != nil {
logAndExit(errors.New("error creating output file"))
}
return file
} else {
file, err := os.Open(filename)
if err != nil {
logAndExit(errors.New("error opening output file"))
}
return file
}
}